com.gs.collections.impl.set.mutable.SetAdapter Maven / Gradle / Ivy
Show all versions of gs-collections Show documentation
/*
* Copyright 2015 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.set.mutable;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ExecutorService;
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.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.set.PartitionMutableSet;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.ParallelUnsortedSetIterable;
import com.gs.collections.api.set.SetIterable;
import com.gs.collections.api.set.UnsortedSetIterable;
import com.gs.collections.api.set.primitive.MutableBooleanSet;
import com.gs.collections.api.set.primitive.MutableByteSet;
import com.gs.collections.api.set.primitive.MutableCharSet;
import com.gs.collections.api.set.primitive.MutableDoubleSet;
import com.gs.collections.api.set.primitive.MutableFloatSet;
import com.gs.collections.api.set.primitive.MutableIntSet;
import com.gs.collections.api.set.primitive.MutableLongSet;
import com.gs.collections.api.set.primitive.MutableShortSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
import com.gs.collections.impl.block.procedure.PartitionPredicate2Procedure;
import com.gs.collections.impl.block.procedure.PartitionProcedure;
import com.gs.collections.impl.block.procedure.SelectInstancesOfProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectBooleanProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectByteProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectCharProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectDoubleProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectFloatProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectIntProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectLongProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectShortProcedure;
import com.gs.collections.impl.collection.mutable.AbstractCollectionAdapter;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.lazy.parallel.set.NonParallelUnsortedSetIterable;
import com.gs.collections.impl.multimap.set.UnifiedSetMultimap;
import com.gs.collections.impl.partition.set.PartitionUnifiedSet;
import com.gs.collections.impl.set.mutable.primitive.BooleanHashSet;
import com.gs.collections.impl.set.mutable.primitive.ByteHashSet;
import com.gs.collections.impl.set.mutable.primitive.CharHashSet;
import com.gs.collections.impl.set.mutable.primitive.DoubleHashSet;
import com.gs.collections.impl.set.mutable.primitive.FloatHashSet;
import com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import com.gs.collections.impl.set.mutable.primitive.LongHashSet;
import com.gs.collections.impl.set.mutable.primitive.ShortHashSet;
import com.gs.collections.impl.utility.ArrayIterate;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.internal.SetIterables;
import com.gs.collections.impl.utility.internal.SetIterate;
/**
* This class provides a MutableSet wrapper around a JDK Collections Set interface instance. All of the MutableSet
* interface methods are supported in addition to the JDK Set interface methods.
*
* To create a new wrapper around an existing Set instance, use the {@link #adapt(Set)} factory method.
*/
public final class SetAdapter
extends AbstractCollectionAdapter
implements Serializable, MutableSet
{
private static final long serialVersionUID = 1L;
private final Set delegate;
SetAdapter(Set newDelegate)
{
if (newDelegate == null)
{
throw new NullPointerException("SetAdapter may not wrap null");
}
this.delegate = newDelegate;
}
@Override
protected Set getDelegate()
{
return this.delegate;
}
public MutableSet asUnmodifiable()
{
return UnmodifiableMutableSet.of(this);
}
public MutableSet asSynchronized()
{
return SynchronizedMutableSet.of(this);
}
public ImmutableSet toImmutable()
{
return Sets.immutable.withAll(this.delegate);
}
public static MutableSet adapt(Set set)
{
if (set instanceof MutableSet)
{
return (MutableSet) set;
}
return new SetAdapter(set);
}
@Override
public MutableSet clone()
{
return UnifiedSet.newSet(this.delegate);
}
@Override
public boolean contains(Object o)
{
return this.delegate.contains(o);
}
@Override
public boolean containsAll(Collection> collection)
{
return this.delegate.containsAll(collection);
}
@Override
public boolean equals(Object obj)
{
return this.delegate.equals(obj);
}
@Override
public int hashCode()
{
return this.delegate.hashCode();
}
public SetAdapter with(T element)
{
this.add(element);
return this;
}
public SetAdapter with(T element1, T element2)
{
this.add(element1);
this.add(element2);
return this;
}
public SetAdapter with(T element1, T element2, T element3)
{
this.add(element1);
this.add(element2);
this.add(element3);
return this;
}
public SetAdapter with(T... elements)
{
ArrayIterate.forEach(elements, CollectionAddProcedure.on(this.delegate));
return this;
}
public SetAdapter without(T element)
{
this.remove(element);
return this;
}
public SetAdapter withAll(Iterable extends T> elements)
{
this.addAllIterable(elements);
return this;
}
public SetAdapter withoutAll(Iterable extends T> elements)
{
this.removeAllIterable(elements);
return this;
}
/**
* @deprecated use {@link UnifiedSet#newSet()} instead (inlineable)
*/
@Deprecated
public MutableSet newEmpty()
{
return UnifiedSet.newSet();
}
@Override
public MutableSet tap(Procedure super T> procedure)
{
Iterate.forEach(this.delegate, procedure);
return this;
}
@Override
public MutableSet select(Predicate super T> predicate)
{
return Iterate.select(this.delegate, predicate, UnifiedSet.newSet());
}
@Override
public MutableSet reject(Predicate super T> predicate)
{
return Iterate.reject(this.delegate, predicate, UnifiedSet.newSet());
}
@Override
public PartitionMutableSet partition(Predicate super T> predicate)
{
PartitionMutableSet partitionUnifiedSet = new PartitionUnifiedSet();
this.forEach(new PartitionProcedure(predicate, partitionUnifiedSet));
return partitionUnifiedSet;
}
@Override
public PartitionMutableSet partitionWith(Predicate2 super T, ? super P> predicate, P parameter)
{
PartitionMutableSet partitionUnifiedSet = new PartitionUnifiedSet();
this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionUnifiedSet));
return partitionUnifiedSet;
}
@Override
public MutableSet selectInstancesOf(Class clazz)
{
MutableSet result = UnifiedSet.newSet();
this.forEach(new SelectInstancesOfProcedure(clazz, result));
return result;
}
@Override
public MutableSet collect(Function super T, ? extends V> function)
{
return Iterate.collect(this.delegate, function, UnifiedSet.newSet());
}
@Override
public MutableBooleanSet collectBoolean(BooleanFunction super T> booleanFunction)
{
BooleanHashSet result = new BooleanHashSet();
this.forEach(new CollectBooleanProcedure(booleanFunction, result));
return result;
}
@Override
public MutableByteSet collectByte(ByteFunction super T> byteFunction)
{
ByteHashSet result = new ByteHashSet(this.size());
this.forEach(new CollectByteProcedure(byteFunction, result));
return result;
}
@Override
public MutableCharSet collectChar(CharFunction super T> charFunction)
{
CharHashSet result = new CharHashSet(this.size());
this.forEach(new CollectCharProcedure(charFunction, result));
return result;
}
@Override
public MutableDoubleSet collectDouble(DoubleFunction super T> doubleFunction)
{
DoubleHashSet result = new DoubleHashSet(this.size());
this.forEach(new CollectDoubleProcedure(doubleFunction, result));
return result;
}
@Override
public MutableFloatSet collectFloat(FloatFunction super T> floatFunction)
{
FloatHashSet result = new FloatHashSet(this.size());
this.forEach(new CollectFloatProcedure(floatFunction, result));
return result;
}
@Override
public MutableIntSet collectInt(IntFunction super T> intFunction)
{
IntHashSet result = new IntHashSet(this.size());
this.forEach(new CollectIntProcedure(intFunction, result));
return result;
}
@Override
public MutableLongSet collectLong(LongFunction super T> longFunction)
{
LongHashSet result = new LongHashSet(this.size());
this.forEach(new CollectLongProcedure(longFunction, result));
return result;
}
@Override
public MutableShortSet collectShort(ShortFunction super T> shortFunction)
{
ShortHashSet result = new ShortHashSet(this.size());
this.forEach(new CollectShortProcedure(shortFunction, result));
return result;
}
@Override
public MutableSet flatCollect(Function super T, ? extends Iterable> function)
{
return Iterate.flatCollect(this.delegate, function, UnifiedSet.newSet());
}
@Override
public MutableSet collectIf(
Predicate super T> predicate,
Function super T, ? extends V> function)
{
return Iterate.collectIf(this.delegate, predicate, function, UnifiedSet.newSet());
}
@Override
public UnifiedSetMultimap groupBy(Function super T, ? extends V> function)
{
return Iterate.groupBy(this.delegate, function, UnifiedSetMultimap.newMultimap());
}
@Override
public UnifiedSetMultimap groupByEach(Function super T, ? extends Iterable> function)
{
return Iterate.groupByEach(this.delegate, function, UnifiedSetMultimap.newMultimap());
}
@Override
public MutableSet selectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return Iterate.selectWith(this.delegate, predicate, parameter, UnifiedSet.newSet());
}
@Override
public MutableSet rejectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return Iterate.rejectWith(this.delegate, predicate, parameter, UnifiedSet.newSet());
}
@Override
public MutableSet collectWith(Function2 super T, ? super P, ? extends V> function, P parameter)
{
return Iterate.collectWith(this.delegate, function, parameter, UnifiedSet.newSet());
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Deprecated
@Override
public MutableSet> zip(Iterable that)
{
return Iterate.zip(this, that, UnifiedSet.>newSet());
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Deprecated
@Override
public MutableSet> zipWithIndex()
{
return Iterate.zipWithIndex(this, UnifiedSet.>newSet());
}
@Override
public boolean removeAllIterable(Iterable> iterable)
{
return SetIterate.removeAllIterable(this, iterable);
}
public MutableSet union(SetIterable extends T> set)
{
return SetIterables.union(this, set);
}
public > R unionInto(SetIterable extends T> set, R targetSet)
{
return SetIterables.unionInto(this, set, targetSet);
}
public MutableSet intersect(SetIterable extends T> set)
{
return SetIterables.intersect(this, set);
}
public > R intersectInto(SetIterable extends T> set, R targetSet)
{
return SetIterables.intersectInto(this, set, targetSet);
}
public MutableSet difference(SetIterable extends T> subtrahendSet)
{
return SetIterables.difference(this, subtrahendSet);
}
public > R differenceInto(SetIterable extends T> subtrahendSet, R targetSet)
{
return SetIterables.differenceInto(this, subtrahendSet, targetSet);
}
public MutableSet symmetricDifference(SetIterable extends T> setB)
{
return SetIterables.symmetricDifference(this, setB);
}
public > R symmetricDifferenceInto(SetIterable extends T> set, R targetSet)
{
return SetIterables.symmetricDifferenceInto(this, set, targetSet);
}
public boolean isSubsetOf(SetIterable extends T> candidateSuperset)
{
return SetIterables.isSubsetOf(this, candidateSuperset);
}
public boolean isProperSubsetOf(SetIterable extends T> candidateSuperset)
{
return SetIterables.isProperSubsetOf(this, candidateSuperset);
}
public MutableSet> powerSet()
{
return (MutableSet>) (MutableSet>) SetIterables.powerSet(this);
}
public LazyIterable> cartesianProduct(SetIterable set)
{
return SetIterables.cartesianProduct(this, set);
}
public ParallelUnsortedSetIterable asParallel(ExecutorService executorService, int batchSize)
{
return new NonParallelUnsortedSetIterable(this);
}
}