import "copyrightAndOthers.stg"
import "primitiveLiteral.stg"

targetPath() ::= "org/eclipse/collections/impl/bag/immutable/primitive"

fileName(primitive) ::= "Immutable<primitive.name>HashBag"

class(primitive) ::= <<
<body(primitive.type, primitive.name)>
>>

body(type, name) ::= <<
<copyrightAndOthers()>

package org.eclipse.collections.impl.bag.immutable.primitive;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.Lazy<name>Iterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.<name>Bag;
import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
<if(!primitive.intPrimitive)>import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;<endif>
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.Mutable<name>List;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.impl.bag.mutable.primitive.<name>HashBag;
import org.eclipse.collections.impl.block.procedure.checked.primitive.Checked<name>IntProcedure;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.<name>Bags;
import org.eclipse.collections.impl.iterator.Unmodifiable<name>Iterator;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.tuple.primitive.<name>IntPair;
import org.eclipse.collections.api.set.primitive.Immutable<name>Set;
import org.eclipse.collections.impl.factory.primitive.<name>Sets;

/**
 * Immutable<name>HashBag is the non-modifiable equivalent of {@link <name>HashBag}.
 * This file was automatically generated from template file immutablePrimitiveHashBag.stg.
 *
 * @since 4.0.
 */
final class Immutable<name>HashBag implements Immutable<name>Bag, Serializable
{
    private static final long serialVersionUID = 1L;

    private final Mutable<name>Bag delegate;

    private Immutable<name>HashBag(<type>[] newElements)
    {
        this.checkOptimizedSize(newElements.length);
        this.delegate = <name>HashBag.newBagWith(newElements);
    }

    private void checkOptimizedSize(int length)
    {
        if (length \<= 1)
        {
            throw new IllegalArgumentException("Use <name>Bags.immutable.with() to instantiate an optimized collection");
        }
    }

    public static Immutable<name>HashBag newBagWith(<type>... elements)
    {
        return new Immutable<name>HashBag(elements);
    }

    @Override
    public Immutable<name>Bag newWith(<type> element)
    {
        return <name>HashBag.newBag(this.delegate).with(element).toImmutable();
    }

    @Override
    public Immutable<name>Bag newWithout(<type> element)
    {
        <name>HashBag hashBag = <name>HashBag.newBag(this.delegate);
        hashBag.remove(element);
        return hashBag.toImmutable();
    }

    @Override
    public Immutable<name>Bag newWithAll(<name>Iterable elements)
    {
        <name>HashBag bag = <name>HashBag.newBag(this.delegate);
        bag.addAll(elements);
        return bag.toImmutable();
    }

    @Override
    public Immutable<name>Bag newWithoutAll(<name>Iterable elements)
    {
        <name>HashBag bag = <name>HashBag.newBag(this.delegate);
        bag.removeAll(elements);
        return bag.toImmutable();
    }

    @Override
    public int size()
    {
        return this.delegate.size();
    }

    @Override
    public boolean isEmpty()
    {
        return this.delegate.isEmpty();
    }

    @Override
    public boolean notEmpty()
    {
        return this.delegate.notEmpty();
    }

    @Override
    public boolean contains(<type> value)
    {
        return this.delegate.contains(value);
    }

    @Override
    public boolean containsAll(<name>Iterable source)
    {
        return this.delegate.containsAll(source);
    }

    @Override
    public boolean containsAll(<type>... elements)
    {
        return this.delegate.containsAll(elements);
    }

    /**
     * @since 7.0.
     */
    @Override
    public void each(<name>Procedure procedure)
    {
        this.delegate.forEach(procedure);
    }

    @Override
    public Immutable<name>Bag select(<name>Predicate predicate)
    {
        return this.delegate.select(predicate).toImmutable();
    }

    @Override
    public Immutable<name>Bag selectByOccurrences(IntPredicate predicate)
    {
        return this.delegate.selectByOccurrences(predicate).toImmutable();
    }

    @Override
    public Immutable<name>Set selectUnique()
    {
        Mutable<name>Set result = <name>Sets.mutable.empty();
        this.forEachWithOccurrences((each, occurrences) ->
        {
            if (occurrences == 1)
            {
                result.add(each);
            }
        });
        return result.toImmutable();
    }

    @Override
    public ImmutableList\<<name>IntPair> topOccurrences(int count)
    {
        return this.delegate.topOccurrences(count).toImmutable();
    }

    @Override
    public ImmutableList\<<name>IntPair> bottomOccurrences(int count)
    {
        return this.delegate.bottomOccurrences(count).toImmutable();
    }

    @Override
    public Immutable<name>Bag reject(<name>Predicate predicate)
    {
        return this.delegate.reject(predicate).toImmutable();
    }

    @Override
    public \<V> ImmutableBag\<V> collect(<name>ToObjectFunction\<? extends V> function)
    {
        MutableBag\<V> bag = this.delegate.collect(function);
        return bag.toImmutable();
    }

    @Override
    public Mutable<name>List toList()
    {
        return this.delegate.toList();
    }

    @Override
    public int sizeDistinct()
    {
        return this.delegate.sizeDistinct();
    }

    @Override
    public int occurrencesOf(<type> item)
    {
        return this.delegate.occurrencesOf(item);
    }

    @Override
    public void forEachWithOccurrences(<name>IntProcedure <type>IntProcedure)
    {
        this.delegate.forEachWithOccurrences(<type>IntProcedure);
    }

    @Override
    public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
    {
        return this.delegate.detectIfNone(predicate, ifNone);
    }

    @Override
    public int count(<name>Predicate predicate)
    {
        return this.delegate.count(predicate);
    }

    @Override
    public boolean anySatisfy(<name>Predicate predicate)
    {
        return this.delegate.anySatisfy(predicate);
    }

    <(arithmeticMethods.(type))()>
    @Override
    public boolean noneSatisfy(<name>Predicate predicate)
    {
        return this.delegate.noneSatisfy(predicate);
    }

    @Override
    public boolean allSatisfy(<name>Predicate predicate)
    {
        return this.delegate.allSatisfy(predicate);
    }

    @Override
    public \<T> T injectInto(T injectedValue, Object<name>ToObjectFunction\<? super T, ? extends T> function)
    {
        return this.delegate.injectInto(injectedValue, function);
    }

    @Override
    public RichIterable\<<name>Iterable> chunk(int size)
    {
        if (size \<= 0)
        {
            throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
        }

        MutableList\<<name>Iterable> result = Lists.mutable.empty();
        if (this.notEmpty())
        {
            if (this.size() \<= size)
            {
                result.add(this);
            }
            else
            {
                <name>Iterator iterator = this.<type>Iterator();
                while (iterator.hasNext())
                {
                    Mutable<name>Bag batch = <name>Bags.mutable.empty();
                    for (int i = 0; i \< size && iterator.hasNext(); i++)
                    {
                        batch.add(iterator.next());
                    }
                    result.add(batch.toImmutable());
                }
            }
        }
        return result.toImmutable();
    }

    @Override
    public boolean equals(Object obj)
    {
        return this.delegate.equals(obj);
    }

    @Override
    public int hashCode()
    {
        return this.delegate.hashCode();
    }

    @Override
    public Mutable<name>Set toSet()
    {
        return this.delegate.toSet();
    }

    @Override
    public Mutable<name>Bag toBag()
    {
        return this.delegate.toBag();
    }

    @Override
    public Immutable<name>Bag toImmutable()
    {
        return this;
    }

    @Override
    public Lazy<name>Iterable asLazy()
    {
        return this.delegate.asLazy();
    }

    @Override
    public <type>[] toArray()
    {
        return this.delegate.toArray();
    }

    @Override
    public <type>[] toArray(<type>[] target)
    {
        return this.delegate.toArray(target);
    }

    @Override
    public String toString()
    {
        return this.delegate.toString();
    }

    @Override
    public String makeString()
    {
        return this.delegate.makeString();
    }

    @Override
    public String makeString(String separator)
    {
        return this.delegate.makeString(separator);
    }

    @Override
    public String makeString(String start, String separator, String end)
    {
        return this.delegate.makeString(start, separator, end);
    }

    @Override
    public void appendString(Appendable appendable)
    {
        this.delegate.appendString(appendable);
    }

    @Override
    public void appendString(Appendable appendable, String separator)
    {
        this.delegate.appendString(appendable, separator);
    }

    @Override
    public void appendString(Appendable appendable, String start, String separator, String end)
    {
        this.delegate.appendString(appendable, start, separator, end);
    }

    @Override
    public <name>Iterator <type>Iterator()
    {
        return new Unmodifiable<name>Iterator(this.delegate.<type>Iterator());
    }

    private Object writeReplace()
    {
        return new Immutable<name>BagSerializationProxy(this);
    }

    protected static class Immutable<name>BagSerializationProxy implements Externalizable
    {
        private static final long serialVersionUID = 1L;

        private <name>Bag bag;

        public Immutable<name>BagSerializationProxy()
        {
            // Empty constructor for Externalizable class
        }

        protected Immutable<name>BagSerializationProxy(<name>Bag bag)
        {
            this.bag = bag;
        }

        @Override
        public void writeExternal(ObjectOutput out) throws IOException
        {
            out.writeInt(this.bag.sizeDistinct());
            try
            {
                this.bag.forEachWithOccurrences(new Checked<name>IntProcedure()
                {
                    @Override
                    public void safeValue(<type> item, int count) throws IOException
                    {
                        out.write<name>(item);
                        out.writeInt(count);
                    }
                });
            }
            catch (RuntimeException e)
            {
                if (e.getCause() instanceof IOException)
                {
                    throw (IOException) e.getCause();
                }
                throw e;
            }
        }

        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
        {
            int size = in.readInt();
            Mutable<name>Bag deserializedBag = new <name>HashBag();

            for (int i = 0; i \< size; i++)
            {
                deserializedBag.addOccurrences(in.read<name>(), in.readInt());
            }

            this.bag = deserializedBag;
        }

        protected Object readResolve()
        {
            return this.bag.toImmutable();
        }
    }
}

>>

arithmeticMethods ::= [
    "byte": "allMethods",
    "short": "allMethods",
    "char": "allMethods",
    "int": "allMethods",
    "long": "allMethods",
    "float": "allMethods",
    "double": "allMethods",
    "boolean": "noMethods"
    ]

allMethods() ::= <<
@Override
public <wideType.(type)> sum()
{
    return this.delegate.sum();
}

@Override
public <type> min()
{
    return this.delegate.min();
}

@Override
public <type> max()
{
    return this.delegate.max();
}

@Override
public <type> maxIfEmpty(<type> defaultValue)
{
    return this.delegate.maxIfEmpty(defaultValue);
}

@Override
public <type> minIfEmpty(<type> defaultValue)
{
    return this.delegate.minIfEmpty(defaultValue);
}

@Override
public double average()
{
    return this.delegate.average();
}

@Override
public double median()
{
    return this.delegate.median();
}

@Override
public <type>[] toSortedArray()
{
    return this.delegate.toSortedArray();
}

@Override
public Mutable<name>List toSortedList()
{
    return this.delegate.toSortedList();
}

>>

noMethods() ::= ""
