import "copyright.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

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

fileName(primitive) ::= "Abstract<primitive.name>Iterable"

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

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

package org.eclipse.collections.impl.primitive;

<if(!primitive.booleanPrimitive)>import java.util.Arrays;

<endif>
import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.Lazy<name>Iterable;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
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.lazy.primitive.Lazy<name>IterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.<name>ArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.<name>HashSet;

/**
 * This file was automatically generated from template file abstractPrimitiveIterable.stg.
 * @since 6.0
 */
public abstract class Abstract<name>Iterable implements <name>Iterable
{
    @Override
    public String toString()
    {
        return this.makeString("[", ", ", "]");
    }

    <(arithmeticMethods.(type))()>

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

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

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

    @Override
    public boolean containsAll(<type>... source)
    {
        for (<type> item : source)
        {
            if (!this.contains(item))
            {
                return false;
            }
        }
        return true;
    }

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

>>

arithmeticMethods ::= [
    "boolean": "noMethods",
    "default": "allMethods"
    ]

allMethods() ::= <<

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

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

@Override
public double average()
{
    if (this.isEmpty())
    {
        throw new ArithmeticException();
    }
    return (double) this.sum() / (double) this.size();
}

@Override
public double median()
{
    if (this.isEmpty())
    {
        throw new ArithmeticException();
    }
    <type>[] sortedArray = this.toSortedArray();
    int middleIndex = sortedArray.length >\> 1;
    if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
    {
        <type> first = sortedArray[middleIndex];
        <type> second = sortedArray[middleIndex - 1];
        return ((double) first + (double) second) / 2.0;
    }
    return (double) sortedArray[middleIndex];
}

@Override
public <type>[] toSortedArray()
{
    <type>[] array = this.toArray();
    Arrays.sort(array);
    return array;
}

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

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

noMethods() ::= <<
@Override
public Lazy<name>Iterable asLazy()
{
    return new Lazy<name>IterableAdapter(this);
}
>>
