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

hasTwoPrimitives() ::= "true"

skipBooleanKeys() ::= "true"

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

fileName(primitive1, primitive2, sameTwoPrimitives) ::= "Immutable<primitive1.name><primitive2.name>SingletonMap"

class(primitive1, primitive2, sameTwoPrimitives) ::= <<
<body(primitive1.type, primitive2.type, primitive1.name, primitive2.name)>
>>

body(type1, type2, name1, name2) ::= <<
<copyright()>

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

import java.io.IOException;
import java.io.Serializable;

import org.eclipse.collections.api.<name1>Iterable;
<if(!sameTwoPrimitives)>import org.eclipse.collections.api.<name2>Iterable;<endif>
import org.eclipse.collections.api.Lazy<name2>Iterable;
<if(!sameTwoPrimitives)>import org.eclipse.collections.api.Lazy<name1>Iterable;<endif>
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.primitive.Immutable<name2>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name2>Bag;
import org.eclipse.collections.api.block.function.primitive.<name2>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name2>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.<name2>Predicate;
import org.eclipse.collections.api.block.predicate.primitive.<name1><name2>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name2>Procedure;
import org.eclipse.collections.api.block.procedure.primitive.<name1><name2>Procedure;
<if(!sameTwoPrimitives)>import org.eclipse.collections.api.block.procedure.primitive.<name1>Procedure;<endif>
import org.eclipse.collections.api.collection.primitive.Mutable<name2>Collection;
import org.eclipse.collections.api.iterator.<name2>Iterator;
import org.eclipse.collections.api.list.primitive.Mutable<name2>List;
import org.eclipse.collections.api.map.primitive.<name1><name2>Map;
import org.eclipse.collections.api.map.primitive.Immutable<name1><name2>Map;
<if(!primitive2.booleanPrimitive)><if(!sameTwoPrimitives)>import org.eclipse.collections.api.map.primitive.Immutable<name2><name1>Map;<endif><endif>
<if(!sameTwoPrimitives)>import org.eclipse.collections.api.set.primitive.Mutable<name1>Set;<endif>
import org.eclipse.collections.api.set.primitive.Mutable<name2>Set;
import org.eclipse.collections.api.tuple.primitive.<name1><name2>Pair;
import org.eclipse.collections.impl.bag.mutable.primitive.<name2>HashBag;
import org.eclipse.collections.impl.collection.mutable.primitive.Unmodifiable<name2>Collection;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.<name1><name2>Maps;
<if(!primitive2.booleanPrimitive)><if(!sameTwoPrimitives)>import org.eclipse.collections.impl.factory.primitive.<name2><name1>Maps;<endif><endif>
import org.eclipse.collections.impl.factory.primitive.<name1>Lists;
import org.eclipse.collections.impl.factory.primitive.<name2>Bags;
import org.eclipse.collections.impl.iterator.Unmodifiable<name2>Iterator;
import org.eclipse.collections.impl.lazy.primitive.Lazy<name2>IterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.<name2>ArrayList;
import org.eclipse.collections.impl.map.mutable.primitive.<name1><name2>HashMap;
import org.eclipse.collections.impl.set.mutable.primitive.<name2>HashSet;
<if(!sameTwoPrimitives)>import org.eclipse.collections.impl.set.mutable.primitive.<name1>HashSet;<endif>
import org.eclipse.collections.impl.set.mutable.primitive.Unmodifiable<name1>Set;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;

/**
 * Immutable<name1><name2>SingletonMap is an optimization for {@link Immutable<name1><name2>Map} of size 1.
 * This file was automatically generated from template file immutablePrimitivePrimitiveSingletonMap.stg.
 *
 * @since 4.0.
 */
final class Immutable<name1><name2>SingletonMap implements Immutable<name1><name2>Map, Serializable
{
    private static final long serialVersionUID = 1L;
    private static final <type2> EMPTY_VALUE = <zero.(type2)>;
    private final <type1> key1;
    private final <type2> value1;

    Immutable<name1><name2>SingletonMap(<type1> key1, <type2> value1)
    {
        this.key1 = key1;
        this.value1 = value1;
    }

    @Override
    public <type2> get(<type1> key)
    {
        return this.getIfAbsent(key, EMPTY_VALUE);
    }

    @Override
    public <type2> getIfAbsent(<type1> key, <type2> ifAbsent)
    {
        return <(equals.(type1))("this.key1", "key")> ? this.value1 : ifAbsent;
    }

    @Override
    public <type2> getOrThrow(<type1> key)
    {
        if (<(equals.(type1))("this.key1", "key")>)
        {
            return this.value1;
        }
        throw new IllegalStateException("Key " + key + " not present.");
    }

    @Override
    public boolean containsKey(<type1> key)
    {
        return <(equals.(type1))("this.key1", "key")>;
    }

    @Override
    public boolean containsValue(<type2> value)
    {
        return <(equals.(type2))("this.value1", "value")>;
    }

    @Override
    public void forEachValue(<name2>Procedure procedure)
    {
        procedure.value(this.value1);
    }

    @Override
    public void forEachKey(<name1>Procedure procedure)
    {
        procedure.value(this.key1);
    }

    @Override
    public void forEachKeyValue(<name1><name2>Procedure procedure)
    {
        procedure.value(this.key1, this.value1);
    }

    @Override
    public Lazy<name1>Iterable keysView()
    {
        return <name1>Lists.immutable.of(this.key1).asLazy();
    }

    @Override
    public RichIterable\<<name1><name2>Pair> keyValuesView()
    {
        return Lists.immutable.of(PrimitiveTuples.pair(this.key1, this.value1)).asLazy();
    }

    <if(!primitive2.booleanPrimitive)>
    @Override
    public Immutable<name2><name1>Map flipUniqueValues()
    {
        return <name2><name1>Maps.immutable.with(this.value1, this.key1);
    }
    <endif>

    @Override
    public Immutable<name1><name2>Map select(<name1><name2>Predicate predicate)
    {
        return predicate.accept(this.key1, this.value1) ? <name1><name2>HashMap.newWithKeysValues(this.key1, this.value1).toImmutable()
                : new <name1><name2>HashMap().toImmutable();
    }

    @Override
    public Immutable<name1><name2>Map reject(<name1><name2>Predicate predicate)
    {
        return predicate.accept(this.key1, this.value1) ? new <name1><name2>HashMap().toImmutable()
                : <name1><name2>HashMap.newWithKeysValues(this.key1, this.value1).toImmutable();
    }

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

    @Override
    public RichIterable\<<name2>Iterable> chunk(int size)
    {
        if (size \<= 0)
        {
            throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
        }
        return Lists.mutable.with(<name2>Bags.immutable.with(this.value1));
    }

    @Override
    public Immutable<name1><name2>Map toImmutable()
    {
        return this;
    }

    @Override
    public <name2>Iterator <type2>Iterator()
    {
        return new Unmodifiable<name2>Iterator(<name1><name2>HashMap.newWithKeysValues(this.key1, this.value1).<type2>Iterator());
    }

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

    /**
     * @since 7.0.
     */
    @Override
    public void each(<name2>Procedure procedure)
    {
        procedure.value(this.value1);
    }

    @Override
    public int count(<name2>Predicate predicate)
    {
        return predicate.accept(this.value1) ? 1 : 0;
    }

    <(arithmeticMethods.(type2))()>
    @Override
    public boolean anySatisfy(<name2>Predicate predicate)
    {
        return predicate.accept(this.value1);
    }

    @Override
    public boolean allSatisfy(<name2>Predicate predicate)
    {
        return predicate.accept(this.value1);
    }

    @Override
    public boolean noneSatisfy(<name2>Predicate predicate)
    {
        return !predicate.accept(this.value1);
    }

    @Override
    public Immutable<name2>Bag select(<name2>Predicate predicate)
    {
        return predicate.accept(this.value1) ? <name2>HashBag.newBagWith(this.value1).toImmutable() : <name2>Bags.immutable.empty();
    }

    @Override
    public Immutable<name2>Bag reject(<name2>Predicate predicate)
    {
        return predicate.accept(this.value1) ? <name2>Bags.immutable.empty() : <name2>HashBag.newBagWith(this.value1).toImmutable();
    }

    @Override
    public \<V> ImmutableBag\<V> collect(<name2>ToObjectFunction\<? extends V> function)
    {
        return Bags.immutable.of(function.valueOf(this.value1));
    }

    @Override
    public <type2> detectIfNone(<name2>Predicate predicate, <type2> ifNone)
    {
        return predicate.accept(this.value1) ? this.value1 : ifNone;
    }

    @Override
    public <type2>[] toArray()
    {
        return new <type2>[]{this.value1};
    }

    @Override
    public <type2>[] toArray(<type2>[] target)
    {
        if (target.length \< 1)
        {
            target = new <type2>[]{this.value1};
        }
        else
        {
            target[0] = this.value1;
        }
        return target;
    }

    @Override
    public boolean contains(<type2> value)
    {
        return <(equals.(type2))("this.value1", "value")>;
    }

    @Override
    public boolean containsAll(<type2>... source)
    {
        for (<type2> value : source)
        {
            if (<(notEquals.(type2))("this.value1", "value")>)
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean containsAll(<name2>Iterable source)
    {
        for (<name2>Iterator iterator = source.<type2>Iterator(); iterator.hasNext(); )
        {
            if (<(notEquals.(type2))("this.value1", "iterator.next()")>)
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public Mutable<name2>List toList()
    {
        return <name2>ArrayList.newListWith(this.value1);
    }

    @Override
    public Mutable<name2>Set toSet()
    {
        return <name2>HashSet.newSetWith(this.value1);
    }

    @Override
    public Mutable<name2>Bag toBag()
    {
        return <name2>HashBag.newBagWith(this.value1);
    }

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

    @Override
    public Immutable<name1><name2>Map newWithKeyValue(<type1> key, <type2> value)
    {
        return <name1><name2>Maps.immutable.withAll(<name1><name2>HashMap.newWithKeysValues(this.key1, this.value1, key, value));
    }

    @Override
    public Immutable<name1><name2>Map newWithoutKey(<type1> key)
    {
        return <(equals.(type1))("this.key1", "key")> ? <name1><name2>Maps.immutable.with() : this;
    }

    @Override
    public Immutable<name1><name2>Map newWithoutAllKeys(<name1>Iterable keys)
    {
        return keys.contains(this.key1) ? <name1><name2>Maps.immutable.with() : this;
    }

    @Override
    public int size()
    {
        return 1;
    }

    @Override
    public boolean isEmpty()
    {
        return false;
    }

    @Override
    public boolean notEmpty()
    {
        return true;
    }

    @Override
    public Mutable<name1>Set keySet()
    {
        return Unmodifiable<name1>Set.of(<name1>HashSet.newSetWith(this.key1));
    }

    @Override
    public Mutable<name2>Collection values()
    {
        return Unmodifiable<name2>Collection.of(<name2>ArrayList.newListWith(this.value1));
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }
        if (!(obj instanceof <name1><name2>Map))
        {
            return false;
        }
        <name1><name2>Map map = (<name1><name2>Map) obj;
        if (map.size() != 1)
        {
            return false;
        }
        return map.containsKey(this.key1) && <(equals.(type2))("this.value1", "map.getOrThrow(this.key1)")>;
    }

    @Override
    public int hashCode()
    {
        return <(hashCode.(type1))("this.key1")> ^ <(hashCode.(type2))("this.value1")>;
    }

    @Override
    public String toString()
    {
        return "{" + this.key1 + "=" + this.value1 + "}";
    }

    @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.value1));
            appendable.append(end);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}

>>

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

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

@Override
public <type2> min()
{
    return this.value1;
}

@Override
public <type2> max()
{
    return this.value1;
}

@Override
public <type2> maxIfEmpty(<type2> defaultValue)
{
    return this.value1;
}

@Override
public <type2> minIfEmpty(<type2> defaultValue)
{
    return this.value1;
}

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

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

@Override
public <type2>[] toSortedArray()
{
    return new <type2>[]{this.value1};
}

@Override
public Mutable<name2>List toSortedList()
{
    return <name2>ArrayList.newListWith(this.value1);
}

>>

noMethods() ::= ""
