SimilarityCharacterInput.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.similarity;

  18. import java.util.Objects;

  19. /**
  20.  * Scores the similarity between two {@link CharSequence}s.
  21.  */
  22. final class SimilarityCharacterInput implements SimilarityInput<Character> {

  23.     /**
  24.      * Source.
  25.      */
  26.     private final CharSequence cs;

  27.     SimilarityCharacterInput(final CharSequence cs) {
  28.         if (cs == null) {
  29.             throw new IllegalArgumentException("CharSequence");
  30.         }
  31.         this.cs = cs;
  32.     }

  33.     @Override
  34.     public Character at(final int index) {
  35.         // Character.valueOf caches character <= 127.
  36.         return Character.valueOf(cs.charAt(index));
  37.     }

  38.     @Override
  39.     public boolean equals(final Object obj) {
  40.         if (this == obj) {
  41.             return true;
  42.         }
  43.         if (obj == null) {
  44.             return false;
  45.         }
  46.         if (getClass() != obj.getClass()) {
  47.             return false;
  48.         }
  49.         final SimilarityCharacterInput other = (SimilarityCharacterInput) obj;
  50.         return Objects.equals(cs, other.cs);
  51.     }

  52.     @Override
  53.     public int hashCode() {
  54.         return Objects.hash(cs);
  55.     }

  56.     @Override
  57.     public int length() {
  58.         return cs.length();
  59.     }

  60.     @Override
  61.     public String toString() {
  62.         return cs.toString();
  63.     }
  64. }