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      /**
34       * Edit distance.
35       */
36      private final Integer distance;
37  
38      /**
39       * Insert character count.
40       */
41      private final Integer insertCount;
42  
43      /**
44       * Delete character count.
45       */
46      private final Integer deleteCount;
47  
48      /**
49       * Substitute character count.
50       */
51      private final Integer substituteCount;
52  
53      /**
54       * Constructs the results for a detailed Levenshtein distance.
55       *
56       * @param distance distance between two character sequences.
57       * @param insertCount insert character count
58       * @param deleteCount delete character count
59       * @param substituteCount substitute character count
60       */
61      public LevenshteinResults(final Integer distance, final Integer insertCount, final Integer deleteCount,
62              final Integer substituteCount) {
63          this.distance = distance;
64          this.insertCount = insertCount;
65          this.deleteCount = deleteCount;
66          this.substituteCount = substituteCount;
67      }
68  
69      @Override
70      public boolean equals(final Object o) {
71          if (this == o) {
72              return true;
73          }
74          if (o == null || getClass() != o.getClass()) {
75              return false;
76          }
77          final LevenshteinResults result = (LevenshteinResults) o;
78          return Objects.equals(distance, result.distance) && Objects.equals(insertCount, result.insertCount)
79                  && Objects.equals(deleteCount, result.deleteCount)
80                  && Objects.equals(substituteCount, result.substituteCount);
81      }
82  
83      /**
84       * Gets 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       * Gets the distance between two character sequences.
94       *
95       * @return distance between two character sequence
96       */
97      public Integer getDistance() {
98          return distance;
99      }
100 
101     /**
102      * Gets the number of insertion needed to change one character sequence into another.
103      *
104      * @return insert character count
105      */
106     public Integer getInsertCount() {
107         return insertCount;
108     }
109 
110     /**
111      * Gets the number of character substitution needed to change one character sequence into another.
112      *
113      * @return substitute character count
114      */
115     public Integer getSubstituteCount() {
116         return substituteCount;
117     }
118 
119     @Override
120     public int hashCode() {
121         return Objects.hash(distance, insertCount, deleteCount, substituteCount);
122     }
123 
124     @Override
125     public String toString() {
126         return "Distance: " + distance + ", Insert: " + insertCount + ", Delete: " + deleteCount + ", Substitute: "
127                 + substituteCount;
128     }
129 }