import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

targetPath() ::= "org/eclipse/collections/impl/stack/mutable/primitive"

fileName(primitive) ::= "Synchronized<primitive.name>Stack"

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

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

package org.eclipse.collections.impl.stack.mutable.primitive;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

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.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>IntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>IntToObjectFunction;
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.primitive.<name>List;
import org.eclipse.collections.api.list.primitive.Mutable<name>List;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.api.stack.MutableStack;
import org.eclipse.collections.api.stack.primitive.Immutable<name>Stack;
import org.eclipse.collections.api.stack.primitive.Mutable<name>Stack;
import org.eclipse.collections.impl.factory.primitive.<name>Stacks;
import org.eclipse.collections.impl.iterator.Unmodifiable<name>Iterator;
import org.eclipse.collections.impl.lazy.primitive.Lazy<name>IterableAdapter;

/**
 * A synchronized view of a {@link Mutable<name>Stack}. It is imperative that the user manually synchronize on the collection when iterating over it using the
 * {@link <name>Iterator}, as per {@link Collections#synchronizedCollection(Collection)}.
 * \<p>
 * This file was automatically generated from template file synchronizedPrimitiveStack.stg.
 *
 * @see Mutable<name>Stack#asSynchronized()
 * @see MutableStack#asSynchronized()
 * @since 3.1.
 */
public class Synchronized<name>Stack
        implements Mutable<name>Stack, Serializable
{
    private static final long serialVersionUID = 1L;
    private final Object lock;
    private final Mutable<name>Stack stack;

    public Synchronized<name>Stack(Mutable<name>Stack stack)
    {
        this(stack, null);
    }

    public Synchronized<name>Stack(Mutable<name>Stack stack, Object newLock)
    {
        this.stack = stack;
        this.lock = newLock == null ? this : newLock;
    }

    @Override
    public void push(<type> item)
    {
        synchronized (this.lock)
        {
            this.stack.push(item);
        }
    }

    @Override
    public <type> pop()
    {
        synchronized (this.lock)
        {
            return this.stack.pop();
        }
    }

    @Override
    public <name>List pop(int count)
    {
        synchronized (this.lock)
        {
            return this.stack.pop(count);
        }
    }

    @Override
    public <type> peek()
    {
        synchronized (this.lock)
        {
            return this.stack.peek();
        }
    }

    @Override
    public <name>List peek(int count)
    {
        synchronized (this.lock)
        {
            return this.stack.peek(count);
        }
    }

    @Override
    public <type> peekAt(int index)
    {
        synchronized (this.lock)
        {
            return this.stack.peekAt(index);
        }
    }

    @Override
    public int size()
    {
        synchronized (this.lock)
        {
            return this.stack.size();
        }
    }

    @Override
    public boolean isEmpty()
    {
        synchronized (this.lock)
        {
            return this.stack.isEmpty();
        }
    }

    @Override
    public boolean notEmpty()
    {
        synchronized (this.lock)
        {
            return this.stack.notEmpty();
        }
    }

    @Override
    public void clear()
    {
        synchronized (this.lock)
        {
            this.stack.clear();
        }
    }

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

    @Override
    public boolean containsAll(<type>... source)
    {
        synchronized (this.lock)
        {
            return this.stack.containsAll(source);
        }
    }

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

    /**
     * Must be called in a synchronized block.
     */
    @Override
    public <name>Iterator <type>Iterator()
    {
        return new Unmodifiable<name>Iterator(this.stack.<type>Iterator());
    }

    @Override
    public void forEach(<name>Procedure procedure)
    {
        this.each(procedure);
    }

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

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

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

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

    @Override
    public boolean noneSatisfy(<name>Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.stack.noneSatisfy(predicate);
        }
    }

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

    @Override
    public Mutable<name>Stack select(<name>Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.stack.select(predicate);
        }
    }

    @Override
    public Mutable<name>Stack reject(<name>Predicate predicate)
    {
        synchronized (this.lock)
        {
            return this.stack.reject(predicate);
        }
    }

    @Override
    public \<V> MutableStack\<V> collect(<name>ToObjectFunction\<? extends V> function)
    {
        synchronized (this.lock)
        {
            return this.stack.collect(function);
        }
    }

    <(arithmeticMethods.(type))(name, type)>
    @Override
    public <type>[] toArray()
    {
        synchronized (this.lock)
        {
            return this.stack.toArray();
        }
    }

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

    @Override
    public String toString()
    {
        synchronized (this.lock)
        {
            return this.stack.toString();
        }
    }

    @Override
    public String makeString()
    {
        synchronized (this.lock)
        {
            return this.stack.makeString();
        }
    }

    @Override
    public String makeString(String separator)
    {
        synchronized (this.lock)
        {
            return this.stack.makeString(separator);
        }
    }

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

    @Override
    public void appendString(Appendable appendable)
    {
        synchronized (this.lock)
        {
            this.stack.appendString(appendable);
        }
    }

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

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

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

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

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

    @Override
    public boolean equals(Object otherStack)
    {
        synchronized (this.lock)
        {
            return this.stack.equals(otherStack);
        }
    }

    @Override
    public int hashCode()
    {
        synchronized (this.lock)
        {
            return this.stack.hashCode();
        }
    }

    @Override
    public Lazy<name>Iterable asLazy()
    {
        synchronized (this.lock)
        {
            return new Lazy<name>IterableAdapter(this);
        }
    }

    @Override
    public Mutable<name>Stack asUnmodifiable()
    {
        synchronized (this.lock)
        {
            return new Unmodifiable<name>Stack(this);
        }
    }

    @Override
    public Mutable<name>Stack asSynchronized()
    {
        return this;
    }

    @Override
    public Immutable<name>Stack toImmutable()
    {
        synchronized (this.lock)
        {
            return <name>Stacks.immutable.withAllReversed(this);
        }
    }

    /**
     * @since 9.2.
     */
    public Mutable<name>Stack newEmpty()
    {
        synchronized (this.lock)
        {
            return this.stack.newEmpty();
        }
    }

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

    @Override
    public RichIterable\<<name>Iterable> chunk(int size)
    {
        synchronized (this.lock)
        {
            return this.stack.chunk(size);
        }
    }

    @Override
    public <type> getFirst()
    {
        synchronized (this.lock)
        {
            return this.stack.getFirst();
        }
    }

    @Override
    public int indexOf(<type> value)
    {
        synchronized (this.lock)
        {
            return this.stack.indexOf(value);
        }
    }

    @Override
    public \<T> T injectIntoWithIndex(T injectedValue, Object<name>IntToObjectFunction\<? super T, ? extends T> function)
    {
        synchronized (this.lock)
        {
            return this.stack.injectIntoWithIndex(injectedValue, function);
        }
    }

    @Override
    public void forEachWithIndex(<name>IntProcedure procedure)
    {
        synchronized (this.lock)
        {
            this.stack.forEachWithIndex(procedure);
        }
    }

    /**
     * Returns a new MutableStack using results obtained by applying the specified function to each element
     * and its corresponding index.
     *
     * @since 9.1.
     */
    @Override
    public \<V> MutableStack\<V> collectWithIndex(<name>IntToObjectFunction\<? extends V> function)
    {
        synchronized (this.lock)
        {
            return this.stack.collectWithIndex(function);
        }
    }

    /**
     * Adds elements to the target Collection using results obtained by applying the specified function to each element
     * and its corresponding index.
     *
     * @since 9.1.
     */
    public \<V, R extends Collection\<V\>> R collectWithIndex(<name>IntToObjectFunction\<? extends V> function, R target)
    {
        synchronized (this.lock)
        {
            return this.stack.collectWithIndex(function, target);
        }
    }
}

>>

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

allMethods(name, type) ::=<<
@Override
public <wideType.(type)> sum()
{
    synchronized (this.lock)
    {
        return this.stack.sum();
    }
}

@Override
public <type> max()
{
    synchronized (this.lock)
    {
        return this.stack.max();
    }
}

@Override
public <type> min()
{
    synchronized (this.lock)
    {
        return this.stack.min();
    }
}

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

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

@Override
public double average()
{
    synchronized (this.lock)
    {
        return this.stack.average();
    }
}

@Override
public double median()
{
    synchronized (this.lock)
    {
        return this.stack.median();
    }
}

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

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

>>

noMethods(name, type) ::= ""
