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.      * Constructs 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.     @Override
  62.     public boolean equals(final Object o) {
  63.         if (this == o) {
  64.             return true;
  65.         }
  66.         if (o == null || getClass() != o.getClass()) {
  67.             return false;
  68.         }
  69.         final LevenshteinResults result = (LevenshteinResults) o;
  70.         return Objects.equals(distance, result.distance) && Objects.equals(insertCount, result.insertCount)
  71.                 && Objects.equals(deleteCount, result.deleteCount)
  72.                 && Objects.equals(substituteCount, result.substituteCount);
  73.     }

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

  82.     /**
  83.      * Gets the distance between two character sequences.
  84.      *
  85.      * @return distance between two character sequence
  86.      */
  87.     public Integer getDistance() {
  88.         return distance;
  89.     }

  90.     /**
  91.      * Gets the number of insertion needed to change one character sequence into another.
  92.      *
  93.      * @return insert character count
  94.      */
  95.     public Integer getInsertCount() {
  96.         return insertCount;
  97.     }

  98.     /**
  99.      * Gets the number of character substitution needed to change one character sequence into another.
  100.      *
  101.      * @return substitute character count
  102.      */
  103.     public Integer getSubstituteCount() {
  104.         return 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. }