    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.similarity;
    019
    020import java.util.Objects;
    021
    022/**
    023 * An ordered input of elements used to compute a similarity score.
    024 * <p>
    025 * You can implement a SimilarityInput on a domain object instead of CharSequence where implementing CharSequence does not make sense.
    026 * </p>
    027 *
    028 * @param <E> the type of elements in this input.
    029 * @since 1.13.0
    030 */
    031public interface SimilarityInput<E> {
    032
    033    /**
    034     * Creates a new input for a {@link CharSequence}.
    035     *
    036     * @param cs input character sequence.
    037     * @return a new input.
    038     */
    039    static SimilarityInput<Character> input(final CharSequence cs) {
    040        return new SimilarityCharacterInput(cs);
    041    }
    042
    043    /**
    044     * Creates a new input for a {@link CharSequence} or {@link SimilarityInput}.
    045     *
    046     * @param <T> The type of similarity score unit.
    047     * @param input character sequence or similarity input.
    048     * @return a new input.
    049     * @throws IllegalArgumentException when the input type is neither {@link CharSequence} or {@link SimilarityInput}.
    050     */
    051    @SuppressWarnings("unchecked")
    052    static <T> SimilarityInput<T> input(final Object input) {
    053        if (input instanceof SimilarityInput) {
    054            return (SimilarityInput<T>) input;
    055        }
    056        if (input instanceof CharSequence) {
    057            return (SimilarityInput<T>) input((CharSequence) input);
    058        }
    059        throw new IllegalArgumentException(Objects.requireNonNull(input, "input").getClass().getName());
    060    }
    061
    062    /**
    063     * Gets the element in the input at the given 0-based index.
    064     *
    065     * @param index a 0-based index.
    066     * @return the element in the input at the given 0-based index.
    067     */
    068    E at(int index);
    069
    070    /**
    071     * Gets the length of the input.
    072     *
    073     * @return the length of the input.
    074     */
    075    int length();
    076
    077}