Coverage Report - de.smartics.util.lang.StringFunction
 
Classes in this File Line Coverage Branch Coverage Complexity
StringFunction
0%
0/29
0%
0/24
4
 
 1  
 /*
 2  
  * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package de.smartics.util.lang;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 
 20  
 /**
 21  
  * A collection of useful string functions.
 22  
  */
 23  
 public final class StringFunction
 24  
 {
 25  
   // ********************************* Fields *********************************
 26  
 
 27  
   // --- constants ------------------------------------------------------------
 28  
 
 29  
   // --- members --------------------------------------------------------------
 30  
 
 31  
   // ****************************** Initializer *******************************
 32  
 
 33  
   // ****************************** Constructors ******************************
 34  
 
 35  
   /**
 36  
    * Utility class.
 37  
    */
 38  
   private StringFunction()
 39  0
   {
 40  0
   }
 41  
 
 42  
   // ****************************** Inner Classes *****************************
 43  
 
 44  
   // ********************************* Methods ********************************
 45  
 
 46  
   // --- init -----------------------------------------------------------------
 47  
 
 48  
   // --- get&set --------------------------------------------------------------
 49  
 
 50  
   // --- business -------------------------------------------------------------
 51  
 
 52  
   /**
 53  
    * Chops off the last character of the buffer and returns the string value of
 54  
    * the modified buffer.
 55  
    * <p>
 56  
    * Removes the last character if the buffer has at least one character.
 57  
    * Returns the value as passed in otherwise.
 58  
    * </p>
 59  
    *
 60  
    * @param buffer the buffer to chop.
 61  
    * @return the content of the buffer except the last character.
 62  
    */
 63  
   public static String chop(final StringBuilder buffer)
 64  
   {
 65  0
     if (buffer == null)
 66  
     {
 67  0
       return null;
 68  
     }
 69  0
     final int length = buffer.length();
 70  0
     if (length == 0)
 71  
     {
 72  0
       return "";
 73  
     }
 74  
 
 75  0
     if (length > 1 && buffer.charAt(length - 1) == '\n'
 76  
         && buffer.charAt(length - 2) == '\r')
 77  
     {
 78  0
       buffer.setLength(length - 2);
 79  
     }
 80  
     else
 81  
     {
 82  0
       buffer.setLength(length - 1);
 83  
     }
 84  
 
 85  0
     return buffer.toString();
 86  
   }
 87  
 
 88  
   /**
 89  
    * Returns the {@code camelCasedString} in a hyphen version.
 90  
    * <p>
 91  
    * Each upper case letter is replaces by a dash and the lower case letter.
 92  
    * </p>
 93  
    *
 94  
    * <pre>
 95  
    * thisIsACamleCaseExample -> this-is-a-camle-case-example
 96  
    * </pre>
 97  
    *
 98  
    * @param camelCasedString the input string.
 99  
    * @return the hyphen version or <code>null</code> if <code>null</code> is
 100  
    *         passed as an argument.
 101  
    */
 102  
   public static String toHyphenVersion(final String camelCasedString)
 103  
   {
 104  0
     if (StringUtils.isBlank(camelCasedString)
 105  
         || StringUtils.isAllLowerCase(camelCasedString))
 106  
     {
 107  0
       return camelCasedString;
 108  
     }
 109  
 
 110  0
     final int length = camelCasedString.length();
 111  0
     final StringBuilder buffer = new StringBuilder(length + 16);
 112  0
     for (int i = 0; i < length; i++)
 113  
     {
 114  0
       final char c = camelCasedString.charAt(i);
 115  0
       if (Character.isUpperCase(c))
 116  
       {
 117  0
         if (i != 0)
 118  
         {
 119  0
           buffer.append('-');
 120  
         }
 121  0
         buffer.append(Character.toLowerCase(c));
 122  
       }
 123  
       else
 124  
       {
 125  0
         buffer.append(c);
 126  
       }
 127  
     }
 128  
 
 129  0
     return buffer.toString();
 130  
   }
 131  
 
 132  
   /**
 133  
    * Checks if the last character of the given string matches the given
 134  
    * {@code ch}.
 135  
    *
 136  
    * @param string the string to test.
 137  
    * @param ch the expected last character of {@code string}.
 138  
    * @return <code>true</code> if {@code ch} matches the last character of
 139  
    *         {@code string}, <code>false</code> otherwise.
 140  
    */
 141  
   public static boolean isLastChar(final String string, final char ch)
 142  
   {
 143  0
     if (StringUtils.isEmpty(string))
 144  
     {
 145  0
       return false;
 146  
     }
 147  
 
 148  0
     final int index = string.length() - 1;
 149  0
     final char lastChar = string.charAt(index);
 150  0
     return lastChar == ch;
 151  
   }
 152  
 
 153  
   /**
 154  
    * Replaces all consecutive white spaces by one space and removes white spaces
 155  
    * from the start and end of the string.
 156  
    *
 157  
    * @param string the string to strip.
 158  
    * @return the stripped string.
 159  
    */
 160  
   public static String strip(final String string)
 161  
   {
 162  0
     return string.trim().replaceAll("\\s+", " ");
 163  
   }
 164  
 
 165  
   // --- object basics --------------------------------------------------------
 166  
 
 167  
 }