IntersectionResult.java

  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. import java.util.Objects;

  19. /**
  20.  * Represents the intersection result between two sets.
  21.  *
  22.  * <p>Stores the size of set A, set B and the intersection of A and B
  23.  * ({@code |A &#8745; B|}).</p>
  24.  *
  25.  * <p>This class is immutable.</p>
  26.  *
  27.  * @since 1.7
  28.  * @see <a href="https://en.wikipedia.org/wiki/Intersection_(set_theory)">Intersection</a>
  29.  */
  30. public class IntersectionResult {

  31.     /**
  32.      * The size of set A.
  33.      */
  34.     private final int sizeA;

  35.     /**
  36.      * The size of set B.
  37.      */
  38.     private final int sizeB;

  39.     /**
  40.      * The size of the intersection between set A and B.
  41.      */
  42.     private final int intersection;

  43.     /**
  44.      * Constructs the results for an intersection between two sets.
  45.      *
  46.      * @param sizeA the size of set A ({@code |A|})
  47.      * @param sizeB the size of set B ({@code |B|})
  48.      * @param intersection the size of the intersection of A and B ({@code |A &#8745; B|})
  49.      * @throws IllegalArgumentException if the sizes are negative or the intersection is greater
  50.      * than the minimum of the two set sizes
  51.      */
  52.     public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
  53.         if (sizeA < 0) {
  54.             throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
  55.         }
  56.         if (sizeB < 0) {
  57.             throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
  58.         }
  59.         if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
  60.             throw new IllegalArgumentException("Invalid intersection of A and B: " + intersection);
  61.         }
  62.         this.sizeA = sizeA;
  63.         this.sizeB = sizeB;
  64.         this.intersection = intersection;
  65.     }

  66.     @Override
  67.     public boolean equals(final Object o) {
  68.         if (this == o) {
  69.             return true;
  70.         }
  71.         if (o == null || getClass() != o.getClass()) {
  72.             return false;
  73.         }
  74.         final IntersectionResult result = (IntersectionResult) o;
  75.         return sizeA == result.sizeA && sizeB == result.sizeB && intersection == result.intersection;
  76.     }

  77.     /**
  78.      * Gets the size of the intersection between set A and B.
  79.      *
  80.      * @return {@code |A &#8745; B|}
  81.      */
  82.     public int getIntersection() {
  83.         return intersection;
  84.     }

  85.     /**
  86.      * Gets the size of set A.
  87.      *
  88.      * @return |A|
  89.      */
  90.     public int getSizeA() {
  91.         return sizeA;
  92.     }

  93.     /**
  94.      * Gets the size of set B.
  95.      *
  96.      * @return |B|
  97.      */
  98.     public int getSizeB() {
  99.         return sizeB;
  100.     }

  101.     @Override
  102.     public int hashCode() {
  103.         return Objects.hash(sizeA, sizeB, intersection);
  104.     }

  105.     @Override
  106.     public String toString() {
  107.         return "Size A: " + sizeA + ", Size B: " + sizeB + ", Intersection: " + intersection;
  108.     }
  109. }