All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javafunk.funk.Sets Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-Present Funk committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 */
package org.javafunk.funk;

import com.google.common.collect.Multiset;
import org.javafunk.funk.functors.Predicate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.javafunk.funk.Eagerly.first;
import static org.javafunk.funk.Eagerly.rest;
import static org.javafunk.funk.Iterables.asSet;
import static org.javafunk.funk.Lazily.filter;
import static org.javafunk.funk.Literals.*;

public class Sets {
    private Sets() {}

    public static  Set union(Iterable> iterables) {
        Set unionSet = new HashSet();
        for (Iterable iterable : iterables) {
            unionSet.addAll(collectionFrom(iterable));
        }
        return unionSet;
    }

    public static  Set intersection(Iterable> iterables) {
        Set intersectionSet = new HashSet(collectionFrom(first(iterables).get()));
        for (Iterable iterable : rest(iterables)) {
            intersectionSet.retainAll(collectionFrom(iterable));
        }
        return intersectionSet;
    }

    public static  Set difference(Iterable> iterables) {
        List> arguments = listFrom(iterables);
        if (arguments.isEmpty()) {
            return new HashSet();
        } else {
            Set differenceSet = setBuilderFrom(first(arguments).get()).build(HashSet.class);
            differenceSet.removeAll(union(rest(arguments)));
            return differenceSet;
        }
    }

    public static  Set symmetricDifference(Iterable> iterables) {
        final Multiset unionMultiset = Multisets.concatenate(iterables);
        return asSet(filter(unionMultiset, new Predicate() {
            public boolean evaluate(T element) {
                return isOdd(unionMultiset.count(element));
            }

            private boolean isOdd(Integer value) {
                return value % 2 == 1;
            }
        }));
    }

    public static  Set union(
            Iterable i1, Iterable i2) {
        return union(iterableWith(i1, i2));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3) {
        return union(iterableWith(i1, i2, i3));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4) {
        return union(iterableWith(i1, i2, i3, i4));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5) {
        return union(iterableWith(i1, i2, i3, i4, i5));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6, i7));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10));
    }

    public static  Set union(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10, Iterable... i11on) {
        return union(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11on));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2) {
        return intersection(iterableWith(i1, i2));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3) {
        return intersection(iterableWith(i1, i2, i3));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4) {
        return intersection(iterableWith(i1, i2, i3, i4));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5) {
        return intersection(iterableWith(i1, i2, i3, i4, i5));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6, i7));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10));
    }

    public static  Set intersection(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10, Iterable... i11on) {
        return intersection(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11on));
    }

    public static  Set difference(
            Iterable i1, Iterable i2) {
        return difference(iterableWith(i1, i2));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3) {
        return difference(iterableWith(i1, i2, i3));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4) {
        return difference(iterableWith(i1, i2, i3, i4));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5) {
        return difference(iterableWith(i1, i2, i3, i4, i5));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6, i7));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10));
    }

    public static  Set difference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10, Iterable... i11on) {
        return difference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11on));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2) {
        return symmetricDifference(iterableWith(i1, i2));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3) {
        return symmetricDifference(iterableWith(i1, i2, i3));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6, i7));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10));
    }

    public static  Set symmetricDifference(
            Iterable i1, Iterable i2, Iterable i3, Iterable i4,
            Iterable i5, Iterable i6, Iterable i7, Iterable i8,
            Iterable i9, Iterable i10, Iterable... i11on) {
        return symmetricDifference(iterableWith(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11on));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy