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 than the minimum of the two set sizes.
056     */
057    public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
058        if (sizeA < 0) {
059            throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
060        }
061        if (sizeB < 0) {
062            throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
063        }
064        if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
065            throw new IllegalArgumentException("Invalid intersection of A and B: " + intersection);
066        }
067        this.sizeA = sizeA;
068        this.sizeB = sizeB;
069        this.intersection = intersection;
070    }
071
072    @Override
073    public boolean equals(final Object o) {
074        if (this == o) {
075            return true;
076        }
077        if (o == null || getClass() != o.getClass()) {
078            return false;
079        }
080        final IntersectionResult result = (IntersectionResult) o;
081        return sizeA == result.sizeA && sizeB == result.sizeB && intersection == result.intersection;
082    }
083
084    /**
085     * Gets the size of the intersection between set A and B.
086     *
087     * @return {@code |A &#8745; B|}
088     */
089    public int getIntersection() {
090        return intersection;
091    }
092
093    /**
094     * Gets the size of set A.
095     *
096     * @return {@code |A|}
097     */
098    public int getSizeA() {
099        return sizeA;
100    }
101
102    /**
103     * Gets the size of set B.
104     *
105     * @return {@code |B|}
106     */
107    public int getSizeB() {
108        return sizeB;
109    }
110
111    @Override
112    public int hashCode() {
113        return Objects.hash(sizeA, sizeB, intersection);
114    }
115
116    @Override
117    public String toString() {
118        return "Size A: " + sizeA + ", Size B: " + sizeB + ", Intersection: " + intersection;
119    }
120}