View Javadoc
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    *      http://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 &#8745; 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 &#8745; B|})
55       * @throws IllegalArgumentException if the sizes are negative or the intersection is greater
56       * than the minimum of the two set sizes
57       */
58      public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
59          if (sizeA < 0) {
60              throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
61          }
62          if (sizeB < 0) {
63              throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
64          }
65          if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
66              throw new IllegalArgumentException("Invalid intersection of A and B: " + intersection);
67          }
68          this.sizeA = sizeA;
69          this.sizeB = sizeB;
70          this.intersection = intersection;
71      }
72  
73      @Override
74      public boolean equals(final Object o) {
75          if (this == o) {
76              return true;
77          }
78          if (o == null || getClass() != o.getClass()) {
79              return false;
80          }
81          final IntersectionResult result = (IntersectionResult) o;
82          return sizeA == result.sizeA && sizeB == result.sizeB && intersection == result.intersection;
83      }
84  
85      /**
86       * Gets the size of the intersection between set A and B.
87       *
88       * @return {@code |A &#8745; B|}
89       */
90      public int getIntersection() {
91          return intersection;
92      }
93  
94      /**
95       * Gets the size of set A.
96       *
97       * @return |A|
98       */
99      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 }