Coverage Report - ca.uhn.hl7v2.util.StringUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtil
57%
23/40
50%
20/40
4.286
 
 1  
 package ca.uhn.hl7v2.util;
 2  
 
 3  
 /**
 4  
  * Various string utility methods
 5  
  */
 6  0
 public class StringUtil {
 7  
 
 8  
         /**
 9  
          * Counts the number of lines in a string by counting the number of "\n" or
 10  
          * "\r" or "\r\n" sequences which appear in it
 11  
          */
 12  
         public static int countLines(String theString) {
 13  0
                 int retVal = 1;
 14  
 
 15  0
                 for (int i = 0; i < theString.length(); i++) {
 16  0
                         char nextChar = theString.charAt(i);
 17  0
                         if (i > 0 && nextChar == '\n' && theString.charAt(i - 1) == '\r') {
 18  0
                                 continue;
 19  
                         }
 20  0
                         if (nextChar == '\r' | nextChar == '\n') {
 21  0
                                 retVal++;
 22  
                         }
 23  
                 }
 24  
 
 25  0
                 return retVal;
 26  
         }
 27  
 
 28  
         /**
 29  
          * Removes any line separators (\r ot \n) from the end of a string and
 30  
          * returns that string
 31  
          */
 32  
         public static String chomp(String theLine) {
 33  
                 int i;
 34  40
                 int start = theLine.length() - 1;
 35  80
                 for (i = start; i >= 0; i--) {
 36  80
                         char nextChar = theLine.charAt(i);
 37  80
                         if (nextChar != '\r' && nextChar != '\n') {
 38  40
                                 break;
 39  
                         }
 40  
                 }
 41  
 
 42  40
                 if (i == start) {
 43  10
                         return theLine;
 44  
                 } else {
 45  30
                         return theLine.substring(0, i + 1);
 46  
                 }
 47  
         }
 48  
 
 49  
         /**
 50  
          * Throws an IllegalArgumentException if the value is an empty string or
 51  
          * null
 52  
          */
 53  
         public static void validateNotEmpty(String theValue) {
 54  633500
                 if (theValue == null || theValue.length() == 0) {
 55  0
                         throw new IllegalArgumentException();
 56  
                 }
 57  633500
         }
 58  
 
 59  
         /**
 60  
          * Search within a string and replace one substring with another. Based on
 61  
          * the method within Commons-Lang StringUtils.
 62  
          */
 63  
         public static String replace(String theString, String theMatch, String theReplacement) {
 64  0
                 StringBuffer buf = new StringBuffer(theString.length());
 65  0
                 int start = 0, end = 0;
 66  0
                 while ((end = theString.indexOf(theMatch, start)) != -1) {
 67  0
                         buf.append(theString.substring(start, end)).append(theReplacement);
 68  0
                         start = end + theMatch.length();
 69  
                 }
 70  0
                 buf.append(theString.substring(start));
 71  0
                 return buf.toString();
 72  
         }
 73  
         
 74  
         public static String[] concatenate(String[] array1, String[] array2) {
 75  45
                 if (array1 == null) return array2;
 76  30
                 if (array2 == null) return array1;
 77  20
         String[] result = new String[array1.length + array2.length];
 78  20
         System.arraycopy(array1, 0, result, 0, array1.length);
 79  20
         System.arraycopy(array2, 0, result, array1.length, array2.length);
 80  20
         return result;
 81  
         }
 82  
 
 83  
         /**
 84  
          * Returns <code>true</code> if the string is null, or contains no non-whitespace characters
 85  
          */
 86  
         public static boolean isBlank(String theString) {
 87  4068
                 if (theString == null) {
 88  1655
                         return true;
 89  
                 }
 90  
                 
 91  2413
                 for (int i = 0; i < theString.length(); i++) {
 92  2408
                         if (Character.isWhitespace(theString.charAt(i)) == false) {
 93  2408
                                 return false;
 94  
                         }
 95  
                 }
 96  
                 
 97  5
                 return true;
 98  
         }
 99  
 
 100  
         /**
 101  
          * Inverse of {@link #isBlank(String)}
 102  
          */
 103  
         public static boolean isNotBlank(String theString) {
 104  660
                 return !isBlank(theString);
 105  
         }
 106  
 
 107  
 }