    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     * Edit distance.
    034     */
    035    private final Integer distance;
    036    /**
    037     * Insert character count.
    038     */
    039    private final Integer insertCount;
    040    /**
    041     * Delete character count.
    042     */
    043    private final Integer deleteCount;
    044    /**
    045     * Substitute character count.
    046     */
    047    private final Integer substituteCount;
    048
    049    /**
    050     * Create the results for a detailed Levenshtein distance.
    051     *
    052     * @param distance distance between two character sequences.
    053     * @param insertCount insert character count
    054     * @param deleteCount delete character count
    055     * @param substituteCount substitute character count
    056     */
    057    public LevenshteinResults(final Integer distance, final Integer insertCount, final Integer deleteCount,
    058            final Integer substituteCount) {
    059        this.distance = distance;
    060        this.insertCount = insertCount;
    061        this.deleteCount = deleteCount;
    062        this.substituteCount = substituteCount;
    063    }
    064
    065    /**
    066     * Get the distance between two character sequences.
    067     *
    068     * @return distance between two character sequence
    069     */
    070    public Integer getDistance() {
    071        return distance;
    072    }
    073
    074    /**
    075     * Get the number of insertion needed to change one character sequence into another.
    076     *
    077     * @return insert character count
    078     */
    079    public Integer getInsertCount() {
    080        return insertCount;
    081    }
    082
    083    /**
    084     * Get 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     * Get the number of character substitution needed to change one character sequence into another.
    094     *
    095     * @return substitute character count
    096     */
    097    public Integer getSubstituteCount() {
    098        return substituteCount;
    099    }
    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}