SimilarityInput.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.  *      https://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.similarity;

  18. import java.util.Objects;

  19. /**
  20.  * An ordered input of elements used to compute a similarity score.
  21.  * <p>
  22.  * You can implement a SimilarityInput on a domain object instead of CharSequence where implementing CharSequence does not make sense.
  23.  * </p>
  24.  *
  25.  * @param <E> the type of elements in this input.
  26.  * @since 1.13.0
  27.  */
  28. public interface SimilarityInput<E> {

  29.     /**
  30.      * Creates a new input for a {@link CharSequence}.
  31.      *
  32.      * @param cs input character sequence.
  33.      * @return a new input.
  34.      */
  35.     static SimilarityInput<Character> input(final CharSequence cs) {
  36.         return new SimilarityCharacterInput(cs);
  37.     }

  38.     /**
  39.      * Creates a new input for a {@link CharSequence} or {@link SimilarityInput}.
  40.      *
  41.      * @param <T> The type of similarity score unit.
  42.      * @param input character sequence or similarity input.
  43.      * @return a new input.
  44.      * @throws IllegalArgumentException when the input type is neither {@link CharSequence} or {@link SimilarityInput}.
  45.      */
  46.     @SuppressWarnings("unchecked")
  47.     static <T> SimilarityInput<T> input(final Object input) {
  48.         if (input instanceof SimilarityInput) {
  49.             return (SimilarityInput<T>) input;
  50.         }
  51.         if (input instanceof CharSequence) {
  52.             return (SimilarityInput<T>) input((CharSequence) input);
  53.         }
  54.         throw new IllegalArgumentException(Objects.requireNonNull(input, "input").getClass().getName());
  55.     }

  56.     /**
  57.      * Gets the element in the input at the given 0-based index.
  58.      *
  59.      * @param index a 0-based index.
  60.      * @return the element in the input at the given 0-based index.
  61.      */
  62.     E at(int index);

  63.     /**
  64.      * Gets the length of the input.
  65.      *
  66.      * @return the length of the input.
  67.      */
  68.     int length();

  69. }