com.gs.collections.impl.utility.internal.SetIterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2011 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.utility.internal;
import java.util.Set;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.SetIterable;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.set.mutable.SetAdapter;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.LazyIterate;
/**
* Set algebra operations.
*
* Most operations are non-destructive, i.e. no input sets are modified during execution.
* The exception is operations ending in "Into." These accept the target collection of
* the final calculation as the first parameter.
*/
public final class SetIterables
{
private SetIterables()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
public static MutableSet union(
SetIterable extends E> setA,
SetIterable extends E> setB)
{
return SetIterables.unionInto(setA, setB, UnifiedSet.newSet());
}
public static > R unionInto(
SetIterable extends E> setA,
SetIterable extends E> setB,
R targetSet)
{
Iterate.addAllIterable(setA, targetSet);
Iterate.addAllIterable(setB, targetSet);
return targetSet;
}
public static MutableSet intersect(
SetIterable extends E> setA,
SetIterable extends E> setB)
{
return SetIterables.intersectInto(setA, setB, UnifiedSet.newSet());
}
public static > R intersectInto(
SetIterable extends E> setA,
SetIterable extends E> setB,
R targetSet)
{
MutableSet adapted = SetAdapter.adapt(targetSet);
adapted.addAllIterable(setA);
adapted.retainAllIterable(setB);
return targetSet;
}
public static MutableSet difference(
SetIterable extends E> minuendSet,
SetIterable extends E> subtrahendSet)
{
return SetIterables.differenceInto(minuendSet, subtrahendSet, UnifiedSet.newSet());
}
public static > R differenceInto(
SetIterable extends E> minuendSet,
SetIterable extends E> subtrahendSet,
R targetSet)
{
MutableSet adapted = SetAdapter.adapt(targetSet);
adapted.addAllIterable(minuendSet);
adapted.removeAllIterable(subtrahendSet);
return targetSet;
}
public static MutableSet symmetricDifference(
SetIterable extends E> setA,
SetIterable extends E> setB)
{
return SetIterables.symmetricDifferenceInto(setA, setB, UnifiedSet.newSet());
}
public static > R symmetricDifferenceInto(
SetIterable extends E> setA,
SetIterable extends E> setB,
R targetSet)
{
return SetIterables.unionInto(
SetIterables.difference(setA, setB),
SetIterables.difference(setB, setA),
targetSet);
}
public static boolean isSubsetOf(
SetIterable extends E> candidateSubset,
SetIterable extends E> candidateSuperset)
{
return candidateSubset.size() <= candidateSuperset.size()
&& candidateSuperset.containsAllIterable(candidateSubset);
}
public static boolean isProperSubsetOf(
SetIterable extends E> candidateSubset,
SetIterable extends E> candidateSuperset)
{
return candidateSubset.size() < candidateSuperset.size()
&& candidateSuperset.containsAllIterable(candidateSubset);
}
public static MutableSet> powerSet(Set set)
{
MutableSet> seed = UnifiedSet.>newSetWith(UnifiedSet.newSet());
return powerSetWithSeed(set, seed);
}
public static MutableSet> powerSet(UnifiedSetWithHashingStrategy set)
{
MutableSet> seed = UnifiedSet.>newSetWith(set.newEmpty());
return powerSetWithSeed(set, seed);
}
private static MutableSet> powerSetWithSeed(Set set, MutableSet> seed)
{
return Iterate.injectInto(seed, set, new Function2>, T, MutableSet>>()
{
public MutableSet> value(MutableSet> accumulator, final T element)
{
return SetIterables.union(accumulator, accumulator.collect(new Function, MutableSet>()
{
public MutableSet valueOf(MutableSet innerSet)
{
return innerSet.clone().with(element);
}
}));
}
});
}
/**
* Returns an Immutable version of powerset where the inner sets are also immutable.
*/
public static ImmutableSet> immutablePowerSet(Set set)
{
return powerSet(set).collect(new Function, ImmutableSet>()
{
public ImmutableSet valueOf(MutableSet set)
{
return set.toImmutable();
}
}).toImmutable();
}
public static LazyIterable> cartesianProduct(SetIterable set1, final SetIterable set2)
{
return LazyIterate.flatCollect(set1, new Function>>()
{
public LazyIterable> valueOf(final A first)
{
return LazyIterate.collect(set2, new Function>()
{
public Pair valueOf(B second)
{
return Tuples.pair(first, second);
}
});
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy