StringMatcherFactory.java

  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.text.matcher;

  18. import org.apache.commons.lang3.ArrayUtils;
  19. import org.apache.commons.lang3.StringUtils;

  20. /**
  21.  * Provides access to matchers defined in this package.
  22.  *
  23.  * @since 1.3
  24.  */
  25. public final class StringMatcherFactory {

  26.     /**
  27.      * Matches the comma character.
  28.      */
  29.     private static final AbstractStringMatcher.CharMatcher COMMA_MATCHER = new AbstractStringMatcher.CharMatcher(',');

  30.     /**
  31.      * Matches the double quote character.
  32.      */
  33.     private static final AbstractStringMatcher.CharMatcher DOUBLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
  34.         '"');

  35.     /**
  36.      * Defines the singleton for this class.
  37.      */
  38.     public static final StringMatcherFactory INSTANCE = new StringMatcherFactory();

  39.     /**
  40.      * Matches no characters.
  41.      */
  42.     private static final AbstractStringMatcher.NoneMatcher NONE_MATCHER = new AbstractStringMatcher.NoneMatcher();

  43.     /**
  44.      * Matches the single or double quote character.
  45.      */
  46.     private static final AbstractStringMatcher.CharSetMatcher QUOTE_MATCHER = new AbstractStringMatcher.CharSetMatcher(
  47.         "'\"".toCharArray());

  48.     /**
  49.      * Matches the double quote character.
  50.      */
  51.     private static final AbstractStringMatcher.CharMatcher SINGLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
  52.         '\'');

  53.     /**
  54.      * Matches the space character.
  55.      */
  56.     private static final AbstractStringMatcher.CharMatcher SPACE_MATCHER = new AbstractStringMatcher.CharMatcher(' ');

  57.     /**
  58.      * Matches the same characters as StringTokenizer, namely space, tab, newline, form feed.
  59.      */
  60.     private static final AbstractStringMatcher.CharSetMatcher SPLIT_MATCHER = new AbstractStringMatcher.CharSetMatcher(
  61.         " \t\n\r\f".toCharArray());

  62.     /**
  63.      * Matches the tab character.
  64.      */
  65.     private static final AbstractStringMatcher.CharMatcher TAB_MATCHER = new AbstractStringMatcher.CharMatcher('\t');

  66.     /**
  67.      * Matches the String trim() whitespace characters.
  68.      */
  69.     private static final AbstractStringMatcher.TrimMatcher TRIM_MATCHER = new AbstractStringMatcher.TrimMatcher();

  70.     /**
  71.      * No need to build instances for now.
  72.      */
  73.     private StringMatcherFactory() {
  74.         // empty
  75.     }

  76.     /**
  77.      * Creates a matcher that matches all of the given matchers in order.
  78.      *
  79.      * @param stringMatchers the matcher
  80.      * @return a matcher that matches all of the given matchers in order.
  81.      * @since 1.9
  82.      */
  83.     public StringMatcher andMatcher(final StringMatcher... stringMatchers) {
  84.         final int len = ArrayUtils.getLength(stringMatchers);
  85.         if (len == 0) {
  86.             return NONE_MATCHER;
  87.         }
  88.         if (len == 1) {
  89.             return stringMatchers[0];
  90.         }
  91.         return new AbstractStringMatcher.AndStringMatcher(stringMatchers);
  92.     }

  93.     /**
  94.      * Constructor that creates a matcher from a character.
  95.      *
  96.      * @param ch the character to match, must not be null
  97.      * @return a new Matcher for the given char
  98.      */
  99.     public StringMatcher charMatcher(final char ch) {
  100.         return new AbstractStringMatcher.CharMatcher(ch);
  101.     }

  102.     /**
  103.      * Constructor that creates a matcher from a set of characters.
  104.      *
  105.      * @param chars the characters to match, null or empty matches nothing
  106.      * @return a new matcher for the given char[]
  107.      */
  108.     public StringMatcher charSetMatcher(final char... chars) {
  109.         final int len = ArrayUtils.getLength(chars);
  110.         if (len == 0) {
  111.             return NONE_MATCHER;
  112.         }
  113.         if (len == 1) {
  114.             return new AbstractStringMatcher.CharMatcher(chars[0]);
  115.         }
  116.         return new AbstractStringMatcher.CharSetMatcher(chars);
  117.     }

  118.     /**
  119.      * Creates a matcher from a string representing a set of characters.
  120.      *
  121.      * @param chars the characters to match, null or empty matches nothing
  122.      * @return a new Matcher for the given characters
  123.      */
  124.     public StringMatcher charSetMatcher(final String chars) {
  125.         final int len = StringUtils.length(chars);
  126.         if (len == 0) {
  127.             return NONE_MATCHER;
  128.         }
  129.         if (len == 1) {
  130.             return new AbstractStringMatcher.CharMatcher(chars.charAt(0));
  131.         }
  132.         return new AbstractStringMatcher.CharSetMatcher(chars.toCharArray());
  133.     }

  134.     /**
  135.      * Returns a matcher which matches the comma character.
  136.      *
  137.      * @return a matcher for a comma
  138.      */
  139.     public StringMatcher commaMatcher() {
  140.         return COMMA_MATCHER;
  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.      * Matches no characters.
  152.      *
  153.      * @return a matcher that matches nothing
  154.      */
  155.     public StringMatcher noneMatcher() {
  156.         return NONE_MATCHER;
  157.     }

  158.     /**
  159.      * Returns a matcher which matches the single or double quote character.
  160.      *
  161.      * @return a matcher for a single or double quote
  162.      */
  163.     public StringMatcher quoteMatcher() {
  164.         return QUOTE_MATCHER;
  165.     }

  166.     /**
  167.      * Returns a matcher which matches the single quote character.
  168.      *
  169.      * @return a matcher for a single quote
  170.      */
  171.     public StringMatcher singleQuoteMatcher() {
  172.         return SINGLE_QUOTE_MATCHER;
  173.     }

  174.     /**
  175.      * Returns a matcher which matches the space character.
  176.      *
  177.      * @return a matcher for a space
  178.      */
  179.     public StringMatcher spaceMatcher() {
  180.         return SPACE_MATCHER;
  181.     }

  182.     /**
  183.      * Matches the same characters as StringTokenizer, namely space, tab, newline and form feed.
  184.      *
  185.      * @return The split matcher
  186.      */
  187.     public StringMatcher splitMatcher() {
  188.         return SPLIT_MATCHER;
  189.     }

  190.     /**
  191.      * Creates a matcher from a string.
  192.      *
  193.      * @param chars the string to match, null or empty matches nothing
  194.      * @return a new Matcher for the given String
  195.      * @since 1.9
  196.      */
  197.     public StringMatcher stringMatcher(final char... chars) {
  198.         final int length = ArrayUtils.getLength(chars);
  199.         return length == 0 ? NONE_MATCHER
  200.             : length == 1 ? new AbstractStringMatcher.CharMatcher(chars[0])
  201.                 : new AbstractStringMatcher.CharArrayMatcher(chars);
  202.     }

  203.     /**
  204.      * Creates a matcher from a string.
  205.      *
  206.      * @param str the string to match, null or empty matches nothing
  207.      * @return a new Matcher for the given String
  208.      */
  209.     public StringMatcher stringMatcher(final String str) {
  210.         return StringUtils.isEmpty(str) ? NONE_MATCHER : stringMatcher(str.toCharArray());
  211.     }

  212.     /**
  213.      * Returns a matcher which matches the tab character.
  214.      *
  215.      * @return a matcher for a tab
  216.      */
  217.     public StringMatcher tabMatcher() {
  218.         return TAB_MATCHER;
  219.     }

  220.     /**
  221.      * Matches the String trim() whitespace characters.
  222.      *
  223.      * @return The trim matcher
  224.      */
  225.     public StringMatcher trimMatcher() {
  226.         return TRIM_MATCHER;
  227.     }

  228. }