StringMatcher.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.CharSequenceUtils;

  19. /**
  20.  * Determines if a character array portion matches.
  21.  *
  22.  * @since 1.3
  23.  */
  24. public interface StringMatcher {

  25.     /**
  26.      * Returns a matcher that matches this matcher followed by the given matcher.
  27.      *
  28.      * @param stringMatcher the next matcher.
  29.      * @return a matcher that matches this matcher followed by the given matcher.
  30.      * @since 1.9
  31.      */
  32.     default StringMatcher andThen(final StringMatcher stringMatcher) {
  33.         return StringMatcherFactory.INSTANCE.andMatcher(this, stringMatcher);
  34.     }

  35.     /**
  36.      * Returns the number of matching characters, zero for no match.
  37.      * <p>
  38.      * This method is called to check for a match. The parameter {@code pos} represents the current position to be
  39.      * checked in the string {@code buffer} (a character array which must not be changed). The API guarantees that
  40.      * {@code pos} is a valid index for {@code buffer}.
  41.      * </p>
  42.      * <p>
  43.      * The matching code may check one character or many. It may check characters preceding {@code pos} as well as those
  44.      * after.
  45.      * </p>
  46.      * <p>
  47.      * It must return zero for no match, or a positive number if a match was found. The number indicates the number of
  48.      * characters that matched.
  49.      * </p>
  50.      *
  51.      * @param buffer the text content to match against, do not change
  52.      * @param pos the starting position for the match, valid for buffer
  53.      * @return The number of matching characters, zero for no match
  54.      * @since 1.9
  55.      */
  56.     default int isMatch(final char[] buffer, final int pos) {
  57.         return isMatch(buffer, pos, 0, buffer.length);
  58.     }

  59.     /**
  60.      * Returns the number of matching characters, {@code 0} if there is no match.
  61.      * <p>
  62.      * This method is called to check for a match against a source {@code buffer}. The parameter {@code start}
  63.      * represents the start position to be checked in the {@code buffer} (a character array which MUST not be changed).
  64.      * The implementation SHOULD guarantees that {@code start} is a valid index in {@code buffer}.
  65.      * </p>
  66.      * <p>
  67.      * The character array may be larger than the active area to be matched. Only values in the buffer between the
  68.      * specified indices may be accessed, in other words: {@code bufferStart <= start < bufferEnd}.
  69.      * </p>
  70.      * <p>
  71.      * The matching code may check one character or many. It may check characters preceding {@code start} as well as
  72.      * those after, so long as no checks exceed the bounds specified.
  73.      * </p>
  74.      * <p>
  75.      * It must return zero for no match, or a positive number if a match was found. The number indicates the number of
  76.      * characters that matched.
  77.      * </p>
  78.      *
  79.      * @param buffer the source text to search, do not change.
  80.      * @param start the starting position for the match, valid in {@code buffer}.
  81.      * @param bufferStart the first active index in the buffer, valid in {@code buffer}.
  82.      * @param bufferEnd the end index (exclusive) of the active buffer, valid in {@code buffer}.
  83.      * @return The number of matching characters, zero if there is no match.
  84.      */
  85.     int isMatch(char[] buffer, int start, int bufferStart, int bufferEnd);

  86.     /**
  87.      * Returns the number of matching characters, zero for no match.
  88.      * <p>
  89.      * This method is called to check for a match. The parameter {@code pos} represents the current position to be
  90.      * checked in the string {@code buffer} (a character array which must not be changed). The API guarantees that
  91.      * {@code pos} is a valid index for {@code buffer}.
  92.      * </p>
  93.      * <p>
  94.      * The matching code may check one character or many. It may check characters preceding {@code pos} as well as those
  95.      * after.
  96.      * </p>
  97.      * <p>
  98.      * It must return zero for no match, or a positive number if a match was found. The number indicates the number of
  99.      * characters that matched.
  100.      * </p>
  101.      *
  102.      * @param buffer the text content to match against, do not change
  103.      * @param pos the starting position for the match, valid for buffer
  104.      * @return The number of matching characters, zero for no match
  105.      * @since 1.9
  106.      */
  107.     default int isMatch(final CharSequence buffer, final int pos) {
  108.         return isMatch(buffer, pos, 0, buffer.length());
  109.     }

  110.     /**
  111.      * Returns the number of matching characters, {@code 0} if there is no match.
  112.      * <p>
  113.      * This method is called to check for a match against a source {@code buffer}. The parameter {@code start}
  114.      * represents the start position to be checked in the {@code buffer} (a character array which MUST not be changed).
  115.      * The implementation SHOULD guarantees that {@code start} is a valid index in {@code buffer}.
  116.      * </p>
  117.      * <p>
  118.      * The character array may be larger than the active area to be matched. Only values in the buffer between the
  119.      * specified indices may be accessed, in other words: {@code bufferStart <= start < bufferEnd}.
  120.      * </p>
  121.      * <p>
  122.      * The matching code may check one character or many. It may check characters preceding {@code start} as well as
  123.      * those after, so long as no checks exceed the bounds specified.
  124.      * </p>
  125.      * <p>
  126.      * It must return zero for no match, or a positive number if a match was found. The number indicates the number of
  127.      * characters that matched.
  128.      * </p>
  129.      *
  130.      * @param buffer the source text to search, do not change.
  131.      * @param start the starting position for the match, valid in {@code buffer}.
  132.      * @param bufferStart the first active index in the buffer, valid in {@code buffer}.
  133.      * @param bufferEnd the end index (exclusive) of the active buffer, valid in {@code buffer}.
  134.      * @return The number of matching characters, zero if there is no match.
  135.      * @since 1.9
  136.      */
  137.     default int isMatch(final CharSequence buffer, final int start, final int bufferStart, final int bufferEnd) {
  138.         return isMatch(CharSequenceUtils.toCharArray(buffer), start, bufferEnd, bufferEnd);
  139.     }

  140.     /**
  141.      * Returns the size of the matching string. Defaults to 0.
  142.      *
  143.      * @return the size of the matching string.
  144.      * @since 1.9
  145.      */
  146.     default int size() {
  147.         return 0;
  148.     }

  149. }