1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.text.similarity;
18
19 import java.util.Objects;
20
21 /**
22 * Represents the intersection result between two sets.
23 *
24 * <p>Stores the size of set A, set B and the intersection of A and B
25 * ({@code |A ∩ B|}).</p>
26 *
27 * <p>This class is immutable.</p>
28 *
29 * @since 1.7
30 * @see <a href="https://en.wikipedia.org/wiki/Intersection_(set_theory)">Intersection</a>
31 */
32 public class IntersectionResult {
33
34 /**
35 * The size of set A.
36 */
37 private final int sizeA;
38
39 /**
40 * The size of set B.
41 */
42 private final int sizeB;
43
44 /**
45 * The size of the intersection between set A and B.
46 */
47 private final int intersection;
48
49 /**
50 * Constructs the results for an intersection between two sets.
51 *
52 * @param sizeA the size of set A ({@code |A|}).
53 * @param sizeB the size of set B ({@code |B|}).
54 * @param intersection the size of the intersection of A and B ({@code |A ∩ B|}).
55 * @throws IllegalArgumentException if the sizes are negative or the intersection is greater than the minimum of the two set sizes.
56 */
57 public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
58 if (sizeA < 0) {
59 throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
60 }
61 if (sizeB < 0) {
62 throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
63 }
64 if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
65 throw new IllegalArgumentException("Invalid intersection of A and B: " + intersection);
66 }
67 this.sizeA = sizeA;
68 this.sizeB = sizeB;
69 this.intersection = intersection;
70 }
71
72 @Override
73 public boolean equals(final Object o) {
74 if (this == o) {
75 return true;
76 }
77 if (o == null || getClass() != o.getClass()) {
78 return false;
79 }
80 final IntersectionResult result = (IntersectionResult) o;
81 return sizeA == result.sizeA && sizeB == result.sizeB && intersection == result.intersection;
82 }
83
84 /**
85 * Gets the size of the intersection between set A and B.
86 *
87 * @return {@code |A ∩ B|}
88 */
89 public int getIntersection() {
90 return intersection;
91 }
92
93 /**
94 * Gets the size of set A.
95 *
96 * @return {@code |A|}
97 */
98 public int getSizeA() {
99 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 }