    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 *      https://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 * Represents the intersection result between two sets.
    023 *
    024 * <p>Stores the size of set A, set B and the intersection of A and B
    025 * ({@code |A &#8745; B|}).</p>
    026 *
    027 * <p>This class is immutable.</p>
    028 *
    029 * @since 1.7
    030 * @see <a href="https://en.wikipedia.org/wiki/Intersection_(set_theory)">Intersection</a>
    031 */
    032public class IntersectionResult {
    033
    034    /**
    035     * The size of set A.
    036     */
    037    private final int sizeA;
    038
    039    /**
    040     * The size of set B.
    041     */
    042    private final int sizeB;
    043
    044    /**
    045     * The size of the intersection between set A and B.
    046     */
    047    private final int intersection;
    048
    049    /**
    050     * Constructs the results for an intersection between two sets.
    051     *
    052     * @param sizeA the size of set A ({@code |A|}).
    053     * @param sizeB the size of set B ({@code |B|}).
    054     * @param intersection the size of the intersection of A and B ({@code |A &#8745; B|}).
    055     * @throws IllegalArgumentException if the sizes are negative or the intersection is greater
    056     * than the minimum of the two set sizes.
    057     */
    058    public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
    059        if (sizeA < 0) {
    060            throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
    061        }
    062        if (sizeB < 0) {
    063            throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
    064        }
    065        if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
    066            throw new IllegalArgumentException("Invalid intersection of A and B: " + intersection);
    067        }
    068        this.sizeA = sizeA;
    069        this.sizeB = sizeB;
    070        this.intersection = intersection;
    071    }
    072
    073    @Override
    074    public boolean equals(final Object o) {
    075        if (this == o) {
    076            return true;
    077        }
    078        if (o == null || getClass() != o.getClass()) {
    079            return false;
    080        }
    081        final IntersectionResult result = (IntersectionResult) o;
    082        return sizeA == result.sizeA && sizeB == result.sizeB && intersection == result.intersection;
    083    }
    084
    085    /**
    086     * Gets the size of the intersection between set A and B.
    087     *
    088     * @return {@code |A &#8745; B|}
    089     */
    090    public int getIntersection() {
    091        return intersection;
    092    }
    093
    094    /**
    095     * Gets the size of set A.
    096     *
    097     * @return |A|
    098     */
    099    public int getSizeA() {
    100        return sizeA;
    101    }
    102
    103    /**
    104     * Gets the size of set B.
    105     *
    106     * @return |B|
    107     */
    108    public int getSizeB() {
    109        return sizeB;
    110    }
    111
    112    @Override
    113    public int hashCode() {
    114        return Objects.hash(sizeA, sizeB, intersection);
    115    }
    116
    117    @Override
    118    public String toString() {
    119        return "Size A: " + sizeA + ", Size B: " + sizeB + ", Intersection: " + intersection;
    120    }
    121}