View Javadoc
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  
19  import java.util.Objects;
20  
21  /**
22   * Container class to store Levenshtein distance between two character sequences.
23   *
24   * <p>Stores the count of insert, deletion and substitute operations needed to
25   * change one character sequence into another.</p>
26   *
27   * <p>This class is immutable.</p>
28   *
29   * @since 1.0
30   */
31  public class LevenshteinResults {
32      /**
33       * Edit distance.
34       */
35      private final Integer distance;
36      /**
37       * Insert character count.
38       */
39      private final Integer insertCount;
40      /**
41       * Delete character count.
42       */
43      private final Integer deleteCount;
44      /**
45       * Substitute character count.
46       */
47      private final Integer substituteCount;
48  
49      /**
50       * Create the results for a detailed Levenshtein distance.
51       *
52       * @param distance distance between two character sequences.
53       * @param insertCount insert character count
54       * @param deleteCount delete character count
55       * @param substituteCount substitute character count
56       */
57      public LevenshteinResults(final Integer distance, final Integer insertCount, final Integer deleteCount,
58              final Integer substituteCount) {
59          this.distance = distance;
60          this.insertCount = insertCount;
61          this.deleteCount = deleteCount;
62          this.substituteCount = substituteCount;
63      }
64  
65      /**
66       * Get the distance between two character sequences.
67       *
68       * @return distance between two character sequence
69       */
70      public Integer getDistance() {
71          return distance;
72      }
73  
74      /**
75       * Get the number of insertion needed to change one character sequence into another.
76       *
77       * @return insert character count
78       */
79      public Integer getInsertCount() {
80          return insertCount;
81      }
82  
83      /**
84       * Get the number of character deletion needed to change one character sequence to other.
85       *
86       * @return delete character count
87       */
88      public Integer getDeleteCount() {
89          return deleteCount;
90      }
91  
92      /**
93       * Get the number of character substitution needed to change one character sequence into another.
94       *
95       * @return substitute character count
96       */
97      public Integer getSubstituteCount() {
98          return substituteCount;
99      }
100 
101     @Override
102     public boolean equals(final Object o) {
103         if (this == o) {
104             return true;
105         }
106         if (o == null || getClass() != o.getClass()) {
107             return false;
108         }
109         final LevenshteinResults result = (LevenshteinResults) o;
110         return Objects.equals(distance, result.distance) && Objects.equals(insertCount, result.insertCount)
111                 && Objects.equals(deleteCount, result.deleteCount)
112                 && Objects.equals(substituteCount, result.substituteCount);
113     }
114 
115     @Override
116     public int hashCode() {
117         return Objects.hash(distance, insertCount, deleteCount, substituteCount);
118     }
119 
120     @Override
121     public String toString() {
122         return "Distance: " + distance + ", Insert: " + insertCount + ", Delete: " + deleteCount + ", Substitute: "
123                 + substituteCount;
124     }
125 }