LevenshteinResults.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.  * Container class to store Levenshtein distance between two character sequences.
  21.  *
  22.  * <p>Stores the count of insert, deletion and substitute operations needed to
  23.  * change one character sequence into another.</p>
  24.  *
  25.  * <p>This class is immutable.</p>
  26.  *
  27.  * @since 1.0
  28.  */
  29. public class LevenshteinResults {
  30.     /**
  31.      * Edit distance.
  32.      */
  33.     private final Integer distance;
  34.     /**
  35.      * Insert character count.
  36.      */
  37.     private final Integer insertCount;
  38.     /**
  39.      * Delete character count.
  40.      */
  41.     private final Integer deleteCount;
  42.     /**
  43.      * Substitute character count.
  44.      */
  45.     private final Integer substituteCount;

  46.     /**
  47.      * Create the results for a detailed Levenshtein distance.
  48.      *
  49.      * @param distance distance between two character sequences.
  50.      * @param insertCount insert character count
  51.      * @param deleteCount delete character count
  52.      * @param substituteCount substitute character count
  53.      */
  54.     public LevenshteinResults(final Integer distance, final Integer insertCount, final Integer deleteCount,
  55.             final Integer substituteCount) {
  56.         this.distance = distance;
  57.         this.insertCount = insertCount;
  58.         this.deleteCount = deleteCount;
  59.         this.substituteCount = substituteCount;
  60.     }

  61.     /**
  62.      * Get the distance between two character sequences.
  63.      *
  64.      * @return distance between two character sequence
  65.      */
  66.     public Integer getDistance() {
  67.         return distance;
  68.     }

  69.     /**
  70.      * Get the number of insertion needed to change one character sequence into another.
  71.      *
  72.      * @return insert character count
  73.      */
  74.     public Integer getInsertCount() {
  75.         return insertCount;
  76.     }

  77.     /**
  78.      * Get the number of character deletion needed to change one character sequence to other.
  79.      *
  80.      * @return delete character count
  81.      */
  82.     public Integer getDeleteCount() {
  83.         return deleteCount;
  84.     }

  85.     /**
  86.      * Get the number of character substitution needed to change one character sequence into another.
  87.      *
  88.      * @return substitute character count
  89.      */
  90.     public Integer getSubstituteCount() {
  91.         return substituteCount;
  92.     }

  93.     @Override
  94.     public boolean equals(final Object o) {
  95.         if (this == o) {
  96.             return true;
  97.         }
  98.         if (o == null || getClass() != o.getClass()) {
  99.             return false;
  100.         }
  101.         final LevenshteinResults result = (LevenshteinResults) o;
  102.         return Objects.equals(distance, result.distance) && Objects.equals(insertCount, result.insertCount)
  103.                 && Objects.equals(deleteCount, result.deleteCount)
  104.                 && Objects.equals(substituteCount, result.substituteCount);
  105.     }

  106.     @Override
  107.     public int hashCode() {
  108.         return Objects.hash(distance, insertCount, deleteCount, substituteCount);
  109.     }

  110.     @Override
  111.     public String toString() {
  112.         return "Distance: " + distance + ", Insert: " + insertCount + ", Delete: " + deleteCount + ", Substitute: "
  113.                 + substituteCount;
  114.     }
  115. }