Java Collection Contain getContainmentRelation(Collection a, Collection b)

Here you can find the source of getContainmentRelation(Collection a, Collection b)

Description

Assesses all the possible containment relations between collections A and B with one call.
Returns an int with bits set, according to a "Venn Diagram" view of A vs B.
NOT_A_SUPERSET_B: a - b != {}
NOT_A_DISJOINT_B: a * b != {} // * is intersects
NOT_A_SUBSET_B: b - a != {}
Thus the bits can be used to get the following relations:
for A_SUPERSET_B, use (x & CollectionUtilities.NOT_A_SUPERSET_B) == 0
for A_SUBSET_B, use (x & CollectionUtilities.NOT_A_SUBSET_B) == 0
for A_EQUALS_B, use (x & CollectionUtilities.NOT_A_EQUALS_B) == 0
for A_DISJOINT_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) == 0
for A_OVERLAPS_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) != 0

License

Open Source License

Declaration

public static int getContainmentRelation(Collection a, Collection b) 

Method Source Code

//package com.java2s;
// License & terms of use: http://www.unicode.org/copyright.html#License

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**// w w  w.jav  a  2s.c  o  m
     * Used for results of getContainmentRelation
     */
    public static final int ALL_EMPTY = 0, NOT_A_SUPERSET_B = 1,
            NOT_A_DISJOINT_B = 2, NOT_A_SUBSET_B = 4,
            NOT_A_EQUALS_B = NOT_A_SUBSET_B | NOT_A_SUPERSET_B,
            A_PROPER_SUBSET_OF_B = NOT_A_DISJOINT_B | NOT_A_SUPERSET_B,
            A_PROPER_SUPERSET_B = NOT_A_SUBSET_B | NOT_A_DISJOINT_B,
            A_PROPER_OVERLAPS_B = NOT_A_SUBSET_B | NOT_A_DISJOINT_B
                    | NOT_A_SUPERSET_B;

    /**
     * Assesses all the possible containment relations between collections A and B with one call.<br>
     * Returns an int with bits set, according to a "Venn Diagram" view of A vs B.<br>
     * NOT_A_SUPERSET_B: a - b != {}<br>
     * NOT_A_DISJOINT_B: a * b != {}  // * is intersects<br>
     * NOT_A_SUBSET_B: b - a != {}<br>
     * Thus the bits can be used to get the following relations:<br>
     * for A_SUPERSET_B, use (x & CollectionUtilities.NOT_A_SUPERSET_B) == 0<br>
     * for A_SUBSET_B, use (x & CollectionUtilities.NOT_A_SUBSET_B) == 0<br>
     * for A_EQUALS_B, use (x & CollectionUtilities.NOT_A_EQUALS_B) == 0<br>
     * for A_DISJOINT_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) == 0<br>
     * for A_OVERLAPS_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) != 0<br>
     */
    public static int getContainmentRelation(Collection a, Collection b) {
        if (a.size() == 0) {
            return (b.size() == 0) ? ALL_EMPTY : NOT_A_SUPERSET_B;
        } else if (b.size() == 0) {
            return NOT_A_SUBSET_B;
        }
        int result = 0;
        // WARNING: one might think that the following can be short-circuited, by looking at
        // the sizes of a and b. However, this would fail in general, where a different comparator is being
        // used in the two collections. Unfortunately, there is no failsafe way to test for that.
        for (Iterator it = a.iterator(); result != 6 && it.hasNext();) {
            result |= (b.contains(it.next())) ? NOT_A_DISJOINT_B
                    : NOT_A_SUBSET_B;
        }
        for (Iterator it = b.iterator(); (result & 3) != 3 && it.hasNext();) {
            result |= (a.contains(it.next())) ? NOT_A_DISJOINT_B
                    : NOT_A_SUPERSET_B;
        }
        return result;
    }

    /**
     * Get the size of an iterator (number of items in it).
     * @param source
     * @return
     */
    public static int size(Iterator source) {
        int result = 0;
        while (source.hasNext()) {
            source.next();
            ++result;
        }
        return result;
    }
}

Related

  1. containsSome(final Collection c1, final Collection c2)
  2. containsSome(HashSet s1, Collection other)
  3. containsString(Collection coll, String str)
  4. containsString(String stringToCheck, Collection collection)
  5. containsStringIgnoreCase(Collection strings, String toCheck)
  6. intersect(Collection container, Collection contained)
  7. isAnyElementContains(String str, Collection col)
  8. isContained(Collection container, String strSearch)
  9. isNullOrContains(final Collection collection, final T item)