    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 */
    017package org.apache.commons.lang3;
    018
    019import org.apache.commons.lang3.stream.Streams;
    020
    021/**
    022 * Operations on {@link CharSet} instances.
    023 *
    024 * <p>This class handles {@code null} input gracefully.
    025 * An exception will not be thrown for a {@code null} input.
    026 * Each method documents its behavior in more detail.</p>
    027 *
    028 * <p>#ThreadSafe#</p>
    029 * @see CharSet
    030 * @since 1.0
    031 */
    032public class CharSetUtils {
    033
    034    /**
    035     * Takes an argument in set-syntax, see evaluateSet,
    036     * and identifies whether any of the characters are present in the specified string.
    037     *
    038     * <pre>
    039     * CharSetUtils.containsAny(null, *)        = false
    040     * CharSetUtils.containsAny("", *)          = false
    041     * CharSetUtils.containsAny(*, null)        = false
    042     * CharSetUtils.containsAny(*, "")          = false
    043     * CharSetUtils.containsAny("hello", "k-p") = true
    044     * CharSetUtils.containsAny("hello", "a-d") = false
    045     * </pre>
    046     *
    047     * @see CharSet#getInstance(String...) for set-syntax.
    048     * @param str  String to look for characters in, may be null
    049     * @param set  String[] set of characters to identify, may be null
    050     * @return whether or not the characters in the set are in the primary string
    051     * @since 3.2
    052     */
    053    public static boolean containsAny(final String str, final String... set) {
    054        if (StringUtils.isEmpty(str) || deepEmpty(set)) {
    055            return false;
    056        }
    057        final CharSet chars = CharSet.getInstance(set);
    058        for (final char c : str.toCharArray()) {
    059            if (chars.contains(c)) {
    060                return true;
    061            }
    062        }
    063        return false;
    064    }
    065
    066    /**
    067     * Takes an argument in set-syntax, see evaluateSet,
    068     * and returns the number of characters present in the specified string.
    069     *
    070     * <pre>
    071     * CharSetUtils.count(null, *)        = 0
    072     * CharSetUtils.count("", *)          = 0
    073     * CharSetUtils.count(*, null)        = 0
    074     * CharSetUtils.count(*, "")          = 0
    075     * CharSetUtils.count("hello", "k-p") = 3
    076     * CharSetUtils.count("hello", "a-e") = 1
    077     * </pre>
    078     *
    079     * @see CharSet#getInstance(String...) for set-syntax.
    080     * @param str  String to count characters in, may be null
    081     * @param set  String[] set of characters to count, may be null
    082     * @return the character count, zero if null string input
    083     */
    084    public static int count(final String str, final String... set) {
    085        if (StringUtils.isEmpty(str) || deepEmpty(set)) {
    086            return 0;
    087        }
    088        final CharSet chars = CharSet.getInstance(set);
    089        int count = 0;
    090        for (final char c : str.toCharArray()) {
    091            if (chars.contains(c)) {
    092                count++;
    093            }
    094        }
    095        return count;
    096    }
    097
    098    /**
    099     * Determines whether or not all the Strings in an array are
    100     * empty or not.
    101     *
    102     * @param strings String[] whose elements are being checked for emptiness
    103     * @return whether or not the String is empty
    104     */
    105    private static boolean deepEmpty(final String[] strings) {
    106        return Streams.of(strings).allMatch(StringUtils::isEmpty);
    107    }
    108
    109    /**
    110     * Takes an argument in set-syntax, see evaluateSet,
    111     * and deletes any of characters present in the specified string.
    112     *
    113     * <pre>
    114     * CharSetUtils.delete(null, *)        = null
    115     * CharSetUtils.delete("", *)          = ""
    116     * CharSetUtils.delete(*, null)        = *
    117     * CharSetUtils.delete(*, "")          = *
    118     * CharSetUtils.delete("hello", "hl")  = "eo"
    119     * CharSetUtils.delete("hello", "le")  = "ho"
    120     * </pre>
    121     *
    122     * @see CharSet#getInstance(String...) for set-syntax.
    123     * @param str  String to delete characters from, may be null
    124     * @param set  String[] set of characters to delete, may be null
    125     * @return the modified String, {@code null} if null string input
    126     */
    127    public static String delete(final String str, final String... set) {
    128        if (StringUtils.isEmpty(str) || deepEmpty(set)) {
    129            return str;
    130        }
    131        return modify(str, set, false);
    132    }
    133
    134    /**
    135     * Takes an argument in set-syntax, see evaluateSet,
    136     * and keeps any of characters present in the specified string.
    137     *
    138     * <pre>
    139     * CharSetUtils.keep(null, *)        = null
    140     * CharSetUtils.keep("", *)          = ""
    141     * CharSetUtils.keep(*, null)        = ""
    142     * CharSetUtils.keep(*, "")          = ""
    143     * CharSetUtils.keep("hello", "hl")  = "hll"
    144     * CharSetUtils.keep("hello", "le")  = "ell"
    145     * </pre>
    146     *
    147     * @see CharSet#getInstance(String...) for set-syntax.
    148     * @param str  String to keep characters from, may be null
    149     * @param set  String[] set of characters to keep, may be null
    150     * @return the modified String, {@code null} if null string input
    151     * @since 2.0
    152     */
    153    public static String keep(final String str, final String... set) {
    154        if (str == null) {
    155            return null;
    156        }
    157        if (str.isEmpty() || deepEmpty(set)) {
    158            return StringUtils.EMPTY;
    159        }
    160        return modify(str, set, true);
    161    }
    162
    163    /**
    164     * Implementation of delete and keep
    165     *
    166     * @param str String to modify characters within
    167     * @param set String[] set of characters to modify
    168     * @param expect whether to evaluate on match, or non-match
    169     * @return the modified String, not null
    170     */
    171    private static String modify(final String str, final String[] set, final boolean expect) {
    172        final CharSet chars = CharSet.getInstance(set);
    173        final StringBuilder buffer = new StringBuilder(str.length());
    174        final char[] chrs = str.toCharArray();
    175        for (final char chr : chrs) {
    176            if (chars.contains(chr) == expect) {
    177                buffer.append(chr);
    178            }
    179        }
    180        return buffer.toString();
    181    }
    182
    183    /**
    184     * Squeezes any repetitions of a character that is mentioned in the
    185     * supplied set.
    186     *
    187     * <pre>
    188     * CharSetUtils.squeeze(null, *)        = null
    189     * CharSetUtils.squeeze("", *)          = ""
    190     * CharSetUtils.squeeze(*, null)        = *
    191     * CharSetUtils.squeeze(*, "")          = *
    192     * CharSetUtils.squeeze("hello", "k-p") = "helo"
    193     * CharSetUtils.squeeze("hello", "a-e") = "hello"
    194     * </pre>
    195     *
    196     * @see CharSet#getInstance(String...) for set-syntax.
    197     * @param str  the string to squeeze, may be null
    198     * @param set  the character set to use for manipulation, may be null
    199     * @return the modified String, {@code null} if null string input
    200     */
    201    public static String squeeze(final String str, final String... set) {
    202        if (StringUtils.isEmpty(str) || deepEmpty(set)) {
    203            return str;
    204        }
    205        final CharSet chars = CharSet.getInstance(set);
    206        final StringBuilder buffer = new StringBuilder(str.length());
    207        final char[] chrs = str.toCharArray();
    208        final int sz = chrs.length;
    209        char lastChar = chrs[0];
    210        char ch;
    211        Character inChars = null;
    212        Character notInChars = null;
    213        buffer.append(lastChar);
    214        for (int i = 1; i < sz; i++) {
    215            ch = chrs[i];
    216            if (ch == lastChar) {
    217                if (inChars != null && ch == inChars) {
    218                    continue;
    219                }
    220                if (notInChars == null || ch != notInChars) {
    221                    if (chars.contains(ch)) {
    222                        inChars = ch;
    223                        continue;
    224                    }
    225                    notInChars = ch;
    226                }
    227            }
    228            buffer.append(ch);
    229            lastChar = ch;
    230        }
    231        return buffer.toString();
    232    }
    233
    234    /**
    235     * CharSetUtils instances should NOT be constructed in standard programming.
    236     * Instead, the class should be used as {@code CharSetUtils.evaluateSet(null);}.
    237     *
    238     * <p>This constructor is public to permit tools that require a JavaBean instance
    239     * to operate.</p>
    240     */
    241    public CharSetUtils() {
    242    }
    243}