    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
    018package org.apache.commons.text.matcher;
    019
    020/**
    021 * Provides access to matchers defined in this package.
    022 *
    023 * @since 1.3
    024 */
    025public final class StringMatcherFactory {
    026
    027    /**
    028     * Defines the singleton for this class.
    029     */
    030    public static final StringMatcherFactory INSTANCE = new StringMatcherFactory();
    031
    032    /**
    033     * Matches the same characters as StringTokenizer, namely space, tab, newline, form feed.
    034     */
    035    private static final AbstractStringMatcher.CharSetMatcher SPLIT_MATCHER = new AbstractStringMatcher.CharSetMatcher(
    036            " \t\n\r\f".toCharArray());
    037
    038    /**
    039     * Matches the comma character.
    040     */
    041    private static final AbstractStringMatcher.CharMatcher COMMA_MATCHER = new AbstractStringMatcher.CharMatcher(',');
    042
    043    /**
    044     * Matches the tab character.
    045     */
    046    private static final AbstractStringMatcher.CharMatcher TAB_MATCHER = new AbstractStringMatcher.CharMatcher('\t');
    047
    048    /**
    049     * Matches the space character.
    050     */
    051    private static final AbstractStringMatcher.CharMatcher SPACE_MATCHER = new AbstractStringMatcher.CharMatcher(' ');
    052
    053    /**
    054     * Matches the String trim() whitespace characters.
    055     */
    056    private static final AbstractStringMatcher.TrimMatcher TRIM_MATCHER = new AbstractStringMatcher.TrimMatcher();
    057
    058    /**
    059     * Matches the double quote character.
    060     */
    061    private static final AbstractStringMatcher.CharMatcher SINGLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
    062            '\'');
    063
    064    /**
    065     * Matches the double quote character.
    066     */
    067    private static final AbstractStringMatcher.CharMatcher DOUBLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
    068            '"');
    069
    070    /**
    071     * Matches the single or double quote character.
    072     */
    073    private static final AbstractStringMatcher.CharSetMatcher QUOTE_MATCHER = new AbstractStringMatcher.CharSetMatcher(
    074            "'\"".toCharArray());
    075
    076    /**
    077     * Matches no characters.
    078     */
    079    private static final AbstractStringMatcher.NoMatcher NONE_MATCHER = new AbstractStringMatcher.NoMatcher();
    080
    081    /**
    082     * No need to build instances for now.
    083     */
    084    private StringMatcherFactory() {
    085        // empty
    086    }
    087
    088    /**
    089     * Constructor that creates a matcher from a character.
    090     *
    091     * @param ch
    092     *            the character to match, must not be null
    093     * @return a new Matcher for the given char
    094     */
    095    public StringMatcher charMatcher(final char ch) {
    096        return new AbstractStringMatcher.CharMatcher(ch);
    097    }
    098
    099    /**
    100     * Constructor that creates a matcher from a set of characters.
    101     *
    102     * @param chars
    103     *            the characters to match, null or empty matches nothing
    104     * @return a new matcher for the given char[]
    105     */
    106    public StringMatcher charSetMatcher(final char... chars) {
    107        if (chars == null || chars.length == 0) {
    108            return NONE_MATCHER;
    109        }
    110        if (chars.length == 1) {
    111            return new AbstractStringMatcher.CharMatcher(chars[0]);
    112        }
    113        return new AbstractStringMatcher.CharSetMatcher(chars);
    114    }
    115
    116    /**
    117     * Creates a matcher from a string representing a set of characters.
    118     *
    119     * @param chars
    120     *            the characters to match, null or empty matches nothing
    121     * @return a new Matcher for the given characters
    122     */
    123    public StringMatcher charSetMatcher(final String chars) {
    124        if (chars == null || chars.length() == 0) {
    125            return NONE_MATCHER;
    126        }
    127        if (chars.length() == 1) {
    128            return new AbstractStringMatcher.CharMatcher(chars.charAt(0));
    129        }
    130        return new AbstractStringMatcher.CharSetMatcher(chars.toCharArray());
    131    }
    132
    133    /**
    134     * Returns a matcher which matches the comma character.
    135     *
    136     * @return a matcher for a comma
    137     */
    138    public StringMatcher commaMatcher() {
    139        return COMMA_MATCHER;
    140    }
    141
    142    /**
    143     * Returns a matcher which matches the double quote character.
    144     *
    145     * @return a matcher for a double quote
    146     */
    147    public StringMatcher doubleQuoteMatcher() {
    148        return DOUBLE_QUOTE_MATCHER;
    149    }
    150
    151    /**
    152     * Matches no characters.
    153     *
    154     * @return a matcher that matches nothing
    155     */
    156    public StringMatcher noneMatcher() {
    157        return NONE_MATCHER;
    158    }
    159
    160    /**
    161     * Returns a matcher which matches the single or double quote character.
    162     *
    163     * @return a matcher for a single or double quote
    164     */
    165    public StringMatcher quoteMatcher() {
    166        return QUOTE_MATCHER;
    167    }
    168
    169    /**
    170     * Returns a matcher which matches the single quote character.
    171     *
    172     * @return a matcher for a single quote
    173     */
    174    public StringMatcher singleQuoteMatcher() {
    175        return SINGLE_QUOTE_MATCHER;
    176    }
    177
    178    /**
    179     * Returns a matcher which matches the space character.
    180     *
    181     * @return a matcher for a space
    182     */
    183    public StringMatcher spaceMatcher() {
    184        return SPACE_MATCHER;
    185    }
    186
    187    /**
    188     * Matches the same characters as StringTokenizer, namely space, tab, newline and form feed.
    189     *
    190     * @return the split matcher
    191     */
    192    public StringMatcher splitMatcher() {
    193        return SPLIT_MATCHER;
    194    }
    195
    196    /**
    197     * Creates a matcher from a string.
    198     *
    199     * @param str
    200     *            the string to match, null or empty matches nothing
    201     * @return a new Matcher for the given String
    202     */
    203    public StringMatcher stringMatcher(final String str) {
    204        if (str == null || str.length() == 0) {
    205            return NONE_MATCHER;
    206        }
    207        return new AbstractStringMatcher.StringMatcher(str);
    208    }
    209
    210    /**
    211     * Returns a matcher which matches the tab character.
    212     *
    213     * @return a matcher for a tab
    214     */
    215    public StringMatcher tabMatcher() {
    216        return TAB_MATCHER;
    217    }
    218
    219    /**
    220     * Matches the String trim() whitespace characters.
    221     *
    222     * @return the trim matcher
    223     */
    224    public StringMatcher trimMatcher() {
    225        return TRIM_MATCHER;
    226    }
    227
    228}