package org.eclipse.collections.impl.bag.immutable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.factory.bag.ImmutableBagFactory;
import org.eclipse.collections.impl.utility.Iterate;
public class ImmutableBagFactoryImpl implements ImmutableBagFactory
{
public static final ImmutableBagFactory INSTANCE = new ImmutableBagFactoryImpl();
@Override
public <T> ImmutableBag<T> empty()
{
return (ImmutableBag<T>) ImmutableEmptyBag.INSTANCE;
}
@Override
public <T> ImmutableBag<T> of()
{
return this.empty();
}
@Override
public <T> ImmutableBag<T> with()
{
return this.empty();
}
@Override
public <T> ImmutableBag<T> of(T element)
{
return this.with(element);
}
@Override
public <T> ImmutableBag<T> with(T element)
{
return new ImmutableSingletonBag<>(element);
}
@Override
public <T> ImmutableBag<T> of(T... elements)
{
return this.with(elements);
}
@Override
public <T> ImmutableBag<T> with(T... elements)
{
if (elements == null || elements.length == 0)
{
return this.empty();
}
if (elements.length == 1)
{
return this.of(elements[0]);
}
if (elements.length < ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE)
{
return ImmutableArrayBag.newBagWith(elements);
}
return ImmutableHashBag.newBagWith(elements);
}
@Override
public <T> ImmutableBag<T> ofAll(Iterable<? extends T> items)
{
return this.withAll(items);
}
@Override
public <T> ImmutableBag<T> withAll(Iterable<? extends T> items)
{
if (Iterate.isEmpty(items))
{
return this.empty();
}
if (items instanceof ImmutableBag<?>)
{
return (ImmutableBag<T>) items;
}
if (items instanceof Bag<?>)
{
Bag<T> bag = (Bag<T>) items;
if (bag.size() == 1)
{
return this.with(bag.getFirst());
}
if (bag.sizeDistinct() < ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE)
{
return ImmutableArrayBag.copyFrom(bag);
}
return new ImmutableHashBag<>(bag);
}
return this.of((T[]) Iterate.toArray(items));
}
}