Coverage Report - org.apache.commons.lang3.CharSetUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CharSetUtils
100%
62/62
98%
53/54
5,375
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3;
 18  
 
 19  
 /**
 20  
  * <p>Operations on {@code CharSet} instances.</p>
 21  
  *
 22  
  * <p>This class handles {@code null} input gracefully.
 23  
  * An exception will not be thrown for a {@code null} input.
 24  
  * Each method documents its behaviour in more detail.</p>
 25  
  * 
 26  
  * <p>#ThreadSafe#</p>
 27  
  * @see CharSet
 28  
  * @since 1.0
 29  
  */
 30  
 public class CharSetUtils {
 31  
 
 32  
     /**
 33  
      * <p>CharSetUtils instances should NOT be constructed in standard programming.
 34  
      * Instead, the class should be used as {@code CharSetUtils.evaluateSet(null);}.</p>
 35  
      *
 36  
      * <p>This constructor is public to permit tools that require a JavaBean instance
 37  
      * to operate.</p>
 38  
      */
 39  
     public CharSetUtils() {
 40  1
       super();
 41  1
     }
 42  
 
 43  
     // Squeeze
 44  
     //-----------------------------------------------------------------------
 45  
     /**
 46  
      * <p>Squeezes any repetitions of a character that is mentioned in the
 47  
      * supplied set.</p>
 48  
      *
 49  
      * <pre>
 50  
      * CharSetUtils.squeeze(null, *)        = null
 51  
      * CharSetUtils.squeeze("", *)          = ""
 52  
      * CharSetUtils.squeeze(*, null)        = *
 53  
      * CharSetUtils.squeeze(*, "")          = *
 54  
      * CharSetUtils.squeeze("hello", "k-p") = "helo"
 55  
      * CharSetUtils.squeeze("hello", "a-e") = "hello"
 56  
      * </pre>
 57  
      *
 58  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 59  
      * @param str  the string to squeeze, may be null
 60  
      * @param set  the character set to use for manipulation, may be null
 61  
      * @return the modified String, {@code null} if null string input
 62  
      */
 63  
     public static String squeeze(final String str, final String... set) {
 64  27
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 65  18
             return str;
 66  
         }
 67  9
         final CharSet chars = CharSet.getInstance(set);
 68  9
         final StringBuilder buffer = new StringBuilder(str.length());
 69  9
         final char[] chrs = str.toCharArray();
 70  9
         final int sz = chrs.length;
 71  9
         char lastChar = chrs[0];
 72  9
         char ch = ' ';
 73  9
         Character inChars = null;
 74  9
         Character notInChars = null;
 75  9
         buffer.append(lastChar);
 76  53
         for (int i = 1; i < sz; i++) {
 77  44
             ch = chrs[i];
 78  44
             if (ch == lastChar) {
 79  17
                 if ((inChars != null) && (ch == inChars)) {
 80  2
                     continue;
 81  
                 } else {
 82  15
                     if ((notInChars == null) || (ch != notInChars)) {
 83  15
                         if (chars.contains(ch)) {
 84  10
                             inChars = ch;
 85  10
                             continue;
 86  
                         } else {
 87  5
                             notInChars = ch;
 88  
                         }
 89  
                     }
 90  
                 }
 91  
             }
 92  32
             buffer.append(ch);
 93  32
             lastChar = ch;
 94  
         }
 95  9
         return buffer.toString();
 96  
     }
 97  
 
 98  
     // ContainsAny
 99  
     //-----------------------------------------------------------------------
 100  
     /**
 101  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 102  
      * and identifies whether any of the characters are present in the specified string.</p>
 103  
      *
 104  
      * <pre>
 105  
      * CharSetUtils.containsAny(null, *)        = false
 106  
      * CharSetUtils.containsAny("", *)          = false
 107  
      * CharSetUtils.containsAny(*, null)        = false
 108  
      * CharSetUtils.containsAny(*, "")          = false
 109  
      * CharSetUtils.containsAny("hello", "k-p") = true
 110  
      * CharSetUtils.containsAny("hello", "a-d") = false
 111  
      * </pre>
 112  
      *
 113  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 114  
      * @param str  String to look for characters in, may be null
 115  
      * @param set  String[] set of characters to identify, may be null
 116  
      * @return whether or not the characters in the set are in the primary string
 117  
      * @since 3.2
 118  
      */
 119  
     public static boolean containsAny(final String str, final String... set) {
 120  26
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 121  19
             return false;
 122  
         }
 123  7
         final CharSet chars = CharSet.getInstance(set);
 124  17
         for (final char c : str.toCharArray()) {
 125  16
             if (chars.contains(c)) {
 126  6
                 return true;
 127  
             }
 128  
         }
 129  1
         return false;
 130  
     }
 131  
 
 132  
     // Count
 133  
     //-----------------------------------------------------------------------
 134  
     /**
 135  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 136  
      * and returns the number of characters present in the specified string.</p>
 137  
      *
 138  
      * <pre>
 139  
      * CharSetUtils.count(null, *)        = 0
 140  
      * CharSetUtils.count("", *)          = 0
 141  
      * CharSetUtils.count(*, null)        = 0
 142  
      * CharSetUtils.count(*, "")          = 0
 143  
      * CharSetUtils.count("hello", "k-p") = 3
 144  
      * CharSetUtils.count("hello", "a-e") = 1
 145  
      * </pre>
 146  
      *
 147  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 148  
      * @param str  String to count characters in, may be null
 149  
      * @param set  String[] set of characters to count, may be null
 150  
      * @return the character count, zero if null string input
 151  
      */
 152  
     public static int count(final String str, final String... set) {
 153  26
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 154  19
             return 0;
 155  
         }
 156  7
         final CharSet chars = CharSet.getInstance(set);
 157  7
         int count = 0;
 158  42
         for (final char c : str.toCharArray()) {
 159  35
             if (chars.contains(c)) {
 160  15
                 count++;
 161  
             }
 162  
         }
 163  7
         return count;
 164  
     }
 165  
 
 166  
     // Keep
 167  
     //-----------------------------------------------------------------------
 168  
     /**
 169  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 170  
      * and keeps any of characters present in the specified string.</p>
 171  
      *
 172  
      * <pre>
 173  
      * CharSetUtils.keep(null, *)        = null
 174  
      * CharSetUtils.keep("", *)          = ""
 175  
      * CharSetUtils.keep(*, null)        = ""
 176  
      * CharSetUtils.keep(*, "")          = ""
 177  
      * CharSetUtils.keep("hello", "hl")  = "hll"
 178  
      * CharSetUtils.keep("hello", "le")  = "ell"
 179  
      * </pre>
 180  
      *
 181  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 182  
      * @param str  String to keep characters from, may be null
 183  
      * @param set  String[] set of characters to keep, may be null
 184  
      * @return the modified String, {@code null} if null string input
 185  
      * @since 2.0
 186  
      */
 187  
     public static String keep(final String str, final String... set) {
 188  29
         if (str == null) {
 189  6
             return null;
 190  
         }
 191  23
         if (str.isEmpty() || deepEmpty(set)) {
 192  12
             return StringUtils.EMPTY;
 193  
         }
 194  11
         return modify(str, set, true);
 195  
     }
 196  
 
 197  
     // Delete
 198  
     //-----------------------------------------------------------------------
 199  
     /**
 200  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 201  
      * and deletes any of characters present in the specified string.</p>
 202  
      *
 203  
      * <pre>
 204  
      * CharSetUtils.delete(null, *)        = null
 205  
      * CharSetUtils.delete("", *)          = ""
 206  
      * CharSetUtils.delete(*, null)        = *
 207  
      * CharSetUtils.delete(*, "")          = *
 208  
      * CharSetUtils.delete("hello", "hl")  = "eo"
 209  
      * CharSetUtils.delete("hello", "le")  = "ho"
 210  
      * </pre>
 211  
      *
 212  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 213  
      * @param str  String to delete characters from, may be null
 214  
      * @param set  String[] set of characters to delete, may be null
 215  
      * @return the modified String, {@code null} if null string input
 216  
      */
 217  
     public static String delete(final String str, final String... set) {
 218  29
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 219  20
             return str;
 220  
         }
 221  9
         return modify(str, set, false);
 222  
     }
 223  
 
 224  
     //-----------------------------------------------------------------------
 225  
     /**
 226  
      * Implementation of delete and keep
 227  
      *
 228  
      * @param str String to modify characters within
 229  
      * @param set String[] set of characters to modify
 230  
      * @param expect whether to evaluate on match, or non-match
 231  
      * @return the modified String, not null
 232  
      */
 233  
     private static String modify(final String str, final String[] set, final boolean expect) {
 234  20
         final CharSet chars = CharSet.getInstance(set);
 235  20
         final StringBuilder buffer = new StringBuilder(str.length());
 236  20
         final char[] chrs = str.toCharArray();
 237  20
         final int sz = chrs.length;
 238  118
         for(int i=0; i<sz; i++) {
 239  98
             if(chars.contains(chrs[i]) == expect) {
 240  55
                 buffer.append(chrs[i]);
 241  
             }
 242  
         }
 243  20
         return buffer.toString();
 244  
     }
 245  
 
 246  
     /** 
 247  
      * Determines whether or not all the Strings in an array are 
 248  
      * empty or not.
 249  
      *
 250  
      * @param strings String[] whose elements are being checked for emptiness
 251  
      * @return whether or not the String is empty
 252  
      */
 253  
     private static boolean deepEmpty(final String[] strings) {
 254  72
         if (strings != null) {
 255  86
             for (final String s : strings) {
 256  62
                 if (StringUtils.isNotEmpty(s)) {
 257  43
                     return false;
 258  
                 }
 259  
             }
 260  
         }
 261  29
         return true;
 262  
     }
 263  
 }