001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.lang3;
018    
019    /**
020     * <p>Operations on {@code CharSet} instances.</p>
021     *
022     * <p>This class handles {@code null} input gracefully.
023     * An exception will not be thrown for a {@code null} input.
024     * Each method documents its behaviour in more detail.</p>
025     * 
026     * <p>#ThreadSafe#</p>
027     * @see CharSet
028     * @since 1.0
029     * @version $Id: CharSetUtils.java 1144916 2011-07-10 17:50:21Z ggregory $
030     */
031    public class CharSetUtils {
032    
033        /**
034         * <p>CharSetUtils instances should NOT be constructed in standard programming.
035         * Instead, the class should be used as {@code CharSetUtils.evaluateSet(null);}.</p>
036         *
037         * <p>This constructor is public to permit tools that require a JavaBean instance
038         * to operate.</p>
039         */
040        public CharSetUtils() {
041          super();
042        }
043    
044        // Squeeze
045        //-----------------------------------------------------------------------
046        /**
047         * <p>Squeezes any repetitions of a character that is mentioned in the
048         * supplied set.</p>
049         *
050         * <pre>
051         * CharSetUtils.squeeze(null, *)        = null
052         * CharSetUtils.squeeze("", *)          = ""
053         * CharSetUtils.squeeze(*, null)        = *
054         * CharSetUtils.squeeze(*, "")          = *
055         * CharSetUtils.squeeze("hello", "k-p") = "helo"
056         * CharSetUtils.squeeze("hello", "a-e") = "hello"
057         * </pre>
058         *
059         * @see CharSet#getInstance(java.lang.String...) for set-syntax.
060         * @param str  the string to squeeze, may be null
061         * @param set  the character set to use for manipulation, may be null
062         * @return the modified String, {@code null} if null string input
063         */
064        public static String squeeze(String str, String... set) {
065            if (StringUtils.isEmpty(str) || deepEmpty(set)) {
066                return str;
067            }
068            CharSet chars = CharSet.getInstance(set);
069            StringBuilder buffer = new StringBuilder(str.length());
070            char[] chrs = str.toCharArray();
071            int sz = chrs.length;
072            char lastChar = ' ';
073            char ch = ' ';
074            for (int i = 0; i < sz; i++) {
075                ch = chrs[i];
076                // Compare with contains() last for performance.
077                if (ch == lastChar && i != 0 && chars.contains(ch)) {
078                    continue;
079                }
080                buffer.append(ch);
081                lastChar = ch;
082            }
083            return buffer.toString();
084        }
085    
086        // Count
087        //-----------------------------------------------------------------------
088        /**
089         * <p>Takes an argument in set-syntax, see evaluateSet,
090         * and returns the number of characters present in the specified string.</p>
091         *
092         * <pre>
093         * CharSetUtils.count(null, *)        = 0
094         * CharSetUtils.count("", *)          = 0
095         * CharSetUtils.count(*, null)        = 0
096         * CharSetUtils.count(*, "")          = 0
097         * CharSetUtils.count("hello", "k-p") = 3
098         * CharSetUtils.count("hello", "a-e") = 1
099         * </pre>
100         *
101         * @see CharSet#getInstance(java.lang.String...) for set-syntax.
102         * @param str  String to count characters in, may be null
103         * @param set  String[] set of characters to count, may be null
104         * @return the character count, zero if null string input
105         */
106        public static int count(String str, String... set) {
107            if (StringUtils.isEmpty(str) || deepEmpty(set)) {
108                return 0;
109            }
110            CharSet chars = CharSet.getInstance(set);
111            int count = 0;
112            for (char c : str.toCharArray()) {
113                if (chars.contains(c)) {
114                    count++;
115                }
116            }
117            return count;
118        }
119    
120        // Keep
121        //-----------------------------------------------------------------------
122        /**
123         * <p>Takes an argument in set-syntax, see evaluateSet,
124         * and keeps any of characters present in the specified string.</p>
125         *
126         * <pre>
127         * CharSetUtils.keep(null, *)        = null
128         * CharSetUtils.keep("", *)          = ""
129         * CharSetUtils.keep(*, null)        = ""
130         * CharSetUtils.keep(*, "")          = ""
131         * CharSetUtils.keep("hello", "hl")  = "hll"
132         * CharSetUtils.keep("hello", "le")  = "ell"
133         * </pre>
134         *
135         * @see CharSet#getInstance(java.lang.String...) for set-syntax.
136         * @param str  String to keep characters from, may be null
137         * @param set  String[] set of characters to keep, may be null
138         * @return the modified String, {@code null} if null string input
139         * @since 2.0
140         */
141        public static String keep(String str, String... set) {
142            if (str == null) {
143                return null;
144            }
145            if (str.length() == 0 || deepEmpty(set)) {
146                return "";
147            }
148            return modify(str, set, true);
149        }
150    
151        // Delete
152        //-----------------------------------------------------------------------
153        /**
154         * <p>Takes an argument in set-syntax, see evaluateSet,
155         * and deletes any of characters present in the specified string.</p>
156         *
157         * <pre>
158         * CharSetUtils.delete(null, *)        = null
159         * CharSetUtils.delete("", *)          = ""
160         * CharSetUtils.delete(*, null)        = *
161         * CharSetUtils.delete(*, "")          = *
162         * CharSetUtils.delete("hello", "hl")  = "eo"
163         * CharSetUtils.delete("hello", "le")  = "ho"
164         * </pre>
165         *
166         * @see CharSet#getInstance(java.lang.String...) for set-syntax.
167         * @param str  String to delete characters from, may be null
168         * @param set  String[] set of characters to delete, may be null
169         * @return the modified String, {@code null} if null string input
170         */
171        public static String delete(String str, String... set) {
172            if (StringUtils.isEmpty(str) || deepEmpty(set)) {
173                return str;
174            }
175            return modify(str, set, false);
176        }
177    
178        //-----------------------------------------------------------------------
179        /**
180         * Implementation of delete and keep
181         *
182         * @param str String to modify characters within
183         * @param set String[] set of characters to modify
184         * @param expect whether to evaluate on match, or non-match
185         * @return the modified String, not null
186         */
187        private static String modify(String str, String[] set, boolean expect) {
188            CharSet chars = CharSet.getInstance(set);
189            StringBuilder buffer = new StringBuilder(str.length());
190            char[] chrs = str.toCharArray();
191            int sz = chrs.length;
192            for(int i=0; i<sz; i++) {
193                if(chars.contains(chrs[i]) == expect) {
194                    buffer.append(chrs[i]);
195                }
196            }
197            return buffer.toString();
198        }
199    
200        /** 
201         * Determines whether or not all the Strings in an array are 
202         * empty or not.
203         *
204         * @param strings String[] whose elements are being checked for emptiness
205         * @return whether or not the String is empty
206         */
207        private static boolean deepEmpty(String[] strings) {
208            if (strings != null) {
209                for (String s : strings) {
210                    if (StringUtils.isNotEmpty(s)) {
211                        return false;
212                    }
213                }
214            }
215            return true;
216        }
217    }