package org.eclipse.collections.impl.bag.immutable.primitive;
import java.io.IOException;
import java.io.Serializable;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.LazyBooleanIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.primitive.ImmutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.BooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.block.function.primitive.BooleanToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.BooleanPredicate;
import org.eclipse.collections.api.block.procedure.primitive.BooleanIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure;
import org.eclipse.collections.api.iterator.BooleanIterator;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.set.primitive.MutableBooleanSet;
import org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.BooleanBags;
import org.eclipse.collections.impl.iterator.UnmodifiableBooleanIterator;
import org.eclipse.collections.impl.lazy.primitive.LazyBooleanIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.BooleanHashSet;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.tuple.primitive.BooleanIntPair;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.api.set.primitive.ImmutableBooleanSet;
import org.eclipse.collections.impl.factory.primitive.BooleanSets;
final class ImmutableBooleanSingletonBag implements ImmutableBooleanBag, Serializable
{
private static final long serialVersionUID = 1L;
private final boolean element1;
ImmutableBooleanSingletonBag(boolean element)
{
this.element1 = element;
}
@Override
public ImmutableBooleanBag newWith(boolean element)
{
return BooleanBags.immutable.with(this.element1, element);
}
@Override
public ImmutableBooleanBag newWithout(boolean element)
{
return this.element1 == element ? BooleanBags.immutable.with() : this;
}
@Override
public ImmutableBooleanBag newWithAll(BooleanIterable elements)
{
return BooleanHashBag.newBag(elements).with(this.element1).toImmutable();
}
@Override
public ImmutableBooleanBag newWithoutAll(BooleanIterable elements)
{
return elements.contains(this.element1) ? BooleanBags.immutable.with() : this;
}
@Override
public int size()
{
return 1;
}
@Override
public boolean isEmpty()
{
return false;
}
@Override
public boolean notEmpty()
{
return true;
}
@Override
public boolean contains(boolean value)
{
return this.element1 == value;
}
@Override
public boolean containsAll(BooleanIterable source)
{
for (BooleanIterator iterator = source.booleanIterator(); iterator.hasNext(); )
{
if (this.element1 != iterator.next())
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(boolean... source)
{
for (boolean value : source)
{
if (this.element1 != value)
{
return false;
}
}
return true;
}
@Override
public void forEach(BooleanProcedure procedure)
{
this.each(procedure);
}
@Override
public void each(BooleanProcedure procedure)
{
procedure.value(this.element1);
}
@Override
public ImmutableBooleanBag select(BooleanPredicate predicate)
{
return predicate.accept(this.element1)
? BooleanBags.immutable.with(this.element1)
: BooleanBags.immutable.empty();
}
@Override
public ImmutableBooleanBag selectByOccurrences(IntPredicate predicate)
{
return predicate.accept(1)
? BooleanBags.immutable.with(this.element1)
: BooleanBags.immutable.empty();
}
@Override
public ImmutableBooleanSet selectUnique()
{
return BooleanSets.immutable.of(this.element1);
}
@Override
public ImmutableList<BooleanIntPair> topOccurrences(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Cannot use a value of count < 0");
}
if (count == 0)
{
return Lists.immutable.empty();
}
return Lists.immutable.with(PrimitiveTuples.pair(this.element1, 1));
}
@Override
public ImmutableList<BooleanIntPair> bottomOccurrences(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Cannot use a value of count < 0");
}
if (count == 0)
{
return Lists.immutable.empty();
}
return Lists.immutable.with(PrimitiveTuples.pair(this.element1, 1));
}
@Override
public ImmutableBooleanBag reject(BooleanPredicate predicate)
{
return predicate.accept(this.element1)
? BooleanBags.immutable.empty()
: BooleanBags.immutable.with(this.element1);
}
@Override
public <V> ImmutableBag<V> collect(BooleanToObjectFunction<? extends V> function)
{
return Bags.immutable.with(function.valueOf(this.element1));
}
@Override
public MutableBooleanList toList()
{
return BooleanArrayList.newListWith(this.element1);
}
@Override
public int sizeDistinct()
{
return 1;
}
@Override
public int occurrencesOf(boolean item)
{
return this.element1 == item ? 1 : 0;
}
@Override
public void forEachWithOccurrences(BooleanIntProcedure booleanIntProcedure)
{
booleanIntProcedure.value(this.element1, 1);
}
@Override
public boolean detectIfNone(BooleanPredicate predicate, boolean ifNone)
{
return predicate.accept(this.element1) ? this.element1 : ifNone;
}
@Override
public int count(BooleanPredicate predicate)
{
return predicate.accept(this.element1) ? 1 : 0;
}
@Override
public boolean anySatisfy(BooleanPredicate predicate)
{
return predicate.accept(this.element1);
}
@Override
public boolean noneSatisfy(BooleanPredicate predicate)
{
return !predicate.accept(this.element1);
}
@Override
public boolean allSatisfy(BooleanPredicate predicate)
{
return predicate.accept(this.element1);
}
@Override
public <T> T injectInto(T injectedValue, ObjectBooleanToObjectFunction<? super T, ? extends T> function)
{
return function.valueOf(injectedValue, this.element1);
}
@Override
public RichIterable<BooleanIterable> chunk(int size)
{
if (size <= 0)
{
throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
}
return Lists.immutable.with(this);
}
@Override
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (!(obj instanceof BooleanBag))
{
return false;
}
BooleanBag bag = (BooleanBag) obj;
if (bag.size() != 1)
{
return false;
}
return this.occurrencesOf(this.element1) == bag.occurrencesOf(this.element1);
}
@Override
public int hashCode()
{
return (this.element1 ? 1231 : 1237) ^ 1;
}
@Override
public MutableBooleanSet toSet()
{
return BooleanHashSet.newSetWith(this.element1);
}
@Override
public MutableBooleanBag toBag()
{
return BooleanHashBag.newBagWith(this.element1);
}
@Override
public ImmutableBooleanBag toImmutable()
{
return this;
}
@Override
public LazyBooleanIterable asLazy()
{
return new LazyBooleanIterableAdapter(this);
}
@Override
public boolean[] toArray()
{
return new boolean[]{this.element1};
}
@Override
public boolean[] toArray(boolean[] target)
{
if (target.length < 1)
{
target = new boolean[]{this.element1};
}
else
{
target[0] = this.element1;
}
return target;
}
@Override
public String toString()
{
return '[' + this.makeString() + ']';
}
@Override
public String makeString()
{
return this.makeString(", ");
}
@Override
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
@Override
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
@Override
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
@Override
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
appendable.append(String.valueOf(this.element1));
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public BooleanIterator booleanIterator()
{
return new UnmodifiableBooleanIterator(BooleanHashBag.newBagWith(this.element1).booleanIterator());
}
}