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