Coverage Report - org.apache.commons.lang3.CharSetUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CharSetUtils
100%
53/53
97%
47/48
5
 
 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 = ' ';
 72  9
         char ch = ' ';
 73  62
         for (int i = 0; i < sz; i++) {
 74  53
             ch = chrs[i];
 75  
             // Compare with contains() last for performance.
 76  53
             if (ch == lastChar && i != 0 && chars.contains(ch)) {
 77  12
                 continue;
 78  
             }
 79  41
             buffer.append(ch);
 80  41
             lastChar = ch;
 81  
         }
 82  9
         return buffer.toString();
 83  
     }
 84  
 
 85  
     // ContainsAny
 86  
     //-----------------------------------------------------------------------
 87  
     /**
 88  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 89  
      * and identifies whether any of the characters are present in the specified string.</p>
 90  
      *
 91  
      * <pre>
 92  
      * CharSetUtils.containsAny(null, *)        = false
 93  
      * CharSetUtils.containsAny("", *)          = false
 94  
      * CharSetUtils.containsAny(*, null)        = false
 95  
      * CharSetUtils.containsAny(*, "")          = false
 96  
      * CharSetUtils.containsAny("hello", "k-p") = true
 97  
      * CharSetUtils.containsAny("hello", "a-d") = false
 98  
      * </pre>
 99  
      *
 100  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 101  
      * @param str  String to look for characters in, may be null
 102  
      * @param set  String[] set of characters to identify, may be null
 103  
      * @return whether or not the characters in the set are in the primary string
 104  
      * @since 3.2
 105  
      */
 106  
     public static boolean containsAny(final String str, final String... set) {
 107  26
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 108  19
             return false;
 109  
         }
 110  7
         final CharSet chars = CharSet.getInstance(set);
 111  17
         for (final char c : str.toCharArray()) {
 112  16
             if (chars.contains(c)) {
 113  6
                 return true;
 114  
             }
 115  
         }
 116  1
         return false;
 117  
     }
 118  
 
 119  
     // Count
 120  
     //-----------------------------------------------------------------------
 121  
     /**
 122  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 123  
      * and returns the number of characters present in the specified string.</p>
 124  
      *
 125  
      * <pre>
 126  
      * CharSetUtils.count(null, *)        = 0
 127  
      * CharSetUtils.count("", *)          = 0
 128  
      * CharSetUtils.count(*, null)        = 0
 129  
      * CharSetUtils.count(*, "")          = 0
 130  
      * CharSetUtils.count("hello", "k-p") = 3
 131  
      * CharSetUtils.count("hello", "a-e") = 1
 132  
      * </pre>
 133  
      *
 134  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 135  
      * @param str  String to count characters in, may be null
 136  
      * @param set  String[] set of characters to count, may be null
 137  
      * @return the character count, zero if null string input
 138  
      */
 139  
     public static int count(final String str, final String... set) {
 140  26
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 141  19
             return 0;
 142  
         }
 143  7
         final CharSet chars = CharSet.getInstance(set);
 144  7
         int count = 0;
 145  42
         for (final char c : str.toCharArray()) {
 146  35
             if (chars.contains(c)) {
 147  15
                 count++;
 148  
             }
 149  
         }
 150  7
         return count;
 151  
     }
 152  
 
 153  
     // Keep
 154  
     //-----------------------------------------------------------------------
 155  
     /**
 156  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 157  
      * and keeps any of characters present in the specified string.</p>
 158  
      *
 159  
      * <pre>
 160  
      * CharSetUtils.keep(null, *)        = null
 161  
      * CharSetUtils.keep("", *)          = ""
 162  
      * CharSetUtils.keep(*, null)        = ""
 163  
      * CharSetUtils.keep(*, "")          = ""
 164  
      * CharSetUtils.keep("hello", "hl")  = "hll"
 165  
      * CharSetUtils.keep("hello", "le")  = "ell"
 166  
      * </pre>
 167  
      *
 168  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 169  
      * @param str  String to keep characters from, may be null
 170  
      * @param set  String[] set of characters to keep, may be null
 171  
      * @return the modified String, {@code null} if null string input
 172  
      * @since 2.0
 173  
      */
 174  
     public static String keep(final String str, final String... set) {
 175  29
         if (str == null) {
 176  6
             return null;
 177  
         }
 178  23
         if (str.isEmpty() || deepEmpty(set)) {
 179  12
             return StringUtils.EMPTY;
 180  
         }
 181  11
         return modify(str, set, true);
 182  
     }
 183  
 
 184  
     // Delete
 185  
     //-----------------------------------------------------------------------
 186  
     /**
 187  
      * <p>Takes an argument in set-syntax, see evaluateSet,
 188  
      * and deletes any of characters present in the specified string.</p>
 189  
      *
 190  
      * <pre>
 191  
      * CharSetUtils.delete(null, *)        = null
 192  
      * CharSetUtils.delete("", *)          = ""
 193  
      * CharSetUtils.delete(*, null)        = *
 194  
      * CharSetUtils.delete(*, "")          = *
 195  
      * CharSetUtils.delete("hello", "hl")  = "eo"
 196  
      * CharSetUtils.delete("hello", "le")  = "ho"
 197  
      * </pre>
 198  
      *
 199  
      * @see CharSet#getInstance(java.lang.String...) for set-syntax.
 200  
      * @param str  String to delete characters from, may be null
 201  
      * @param set  String[] set of characters to delete, may be null
 202  
      * @return the modified String, {@code null} if null string input
 203  
      */
 204  
     public static String delete(final String str, final String... set) {
 205  29
         if (StringUtils.isEmpty(str) || deepEmpty(set)) {
 206  20
             return str;
 207  
         }
 208  9
         return modify(str, set, false);
 209  
     }
 210  
 
 211  
     //-----------------------------------------------------------------------
 212  
     /**
 213  
      * Implementation of delete and keep
 214  
      *
 215  
      * @param str String to modify characters within
 216  
      * @param set String[] set of characters to modify
 217  
      * @param expect whether to evaluate on match, or non-match
 218  
      * @return the modified String, not null
 219  
      */
 220  
     private static String modify(final String str, final String[] set, final boolean expect) {
 221  20
         final CharSet chars = CharSet.getInstance(set);
 222  20
         final StringBuilder buffer = new StringBuilder(str.length());
 223  20
         final char[] chrs = str.toCharArray();
 224  20
         final int sz = chrs.length;
 225  118
         for(int i=0; i<sz; i++) {
 226  98
             if(chars.contains(chrs[i]) == expect) {
 227  55
                 buffer.append(chrs[i]);
 228  
             }
 229  
         }
 230  20
         return buffer.toString();
 231  
     }
 232  
 
 233  
     /** 
 234  
      * Determines whether or not all the Strings in an array are 
 235  
      * empty or not.
 236  
      *
 237  
      * @param strings String[] whose elements are being checked for emptiness
 238  
      * @return whether or not the String is empty
 239  
      */
 240  
     private static boolean deepEmpty(final String[] strings) {
 241  72
         if (strings != null) {
 242  86
             for (final String s : strings) {
 243  62
                 if (StringUtils.isNotEmpty(s)) {
 244  43
                     return false;
 245  
                 }
 246  
             }
 247  
         }
 248  29
         return true;
 249  
     }
 250  
 }