001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.text.similarity;
018
019import java.util.Objects;
020
021/**
022 * Container class to store Levenshtein distance between two character sequences.
023 *
024 * <p>Stores the count of insert, deletion and substitute operations needed to
025 * change one character sequence into another.</p>
026 *
027 * <p>This class is immutable.</p>
028 *
029 * @since 1.0
030 */
031public class LevenshteinResults {
032
033    /**
034     * Edit distance.
035     */
036    private final Integer distance;
037
038    /**
039     * Insert character count.
040     */
041    private final Integer insertCount;
042
043    /**
044     * Delete character count.
045     */
046    private final Integer deleteCount;
047
048    /**
049     * Substitute character count.
050     */
051    private final Integer substituteCount;
052
053    /**
054     * Constructs the results for a detailed Levenshtein distance.
055     *
056     * @param distance distance between two character sequences.
057     * @param insertCount insert character count
058     * @param deleteCount delete character count
059     * @param substituteCount substitute character count
060     */
061    public LevenshteinResults(final Integer distance, final Integer insertCount, final Integer deleteCount,
062            final Integer substituteCount) {
063        this.distance = distance;
064        this.insertCount = insertCount;
065        this.deleteCount = deleteCount;
066        this.substituteCount = substituteCount;
067    }
068
069    @Override
070    public boolean equals(final Object o) {
071        if (this == o) {
072            return true;
073        }
074        if (o == null || getClass() != o.getClass()) {
075            return false;
076        }
077        final LevenshteinResults result = (LevenshteinResults) o;
078        return Objects.equals(distance, result.distance) && Objects.equals(insertCount, result.insertCount)
079                && Objects.equals(deleteCount, result.deleteCount)
080                && Objects.equals(substituteCount, result.substituteCount);
081    }
082
083    /**
084     * Gets the number of character deletion needed to change one character sequence to other.
085     *
086     * @return delete character count
087     */
088    public Integer getDeleteCount() {
089        return deleteCount;
090    }
091
092    /**
093     * Gets the distance between two character sequences.
094     *
095     * @return distance between two character sequence
096     */
097    public Integer getDistance() {
098        return distance;
099    }
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}