All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.collections.impl.map.immutable.primitive.ImmutableObjectBooleanSingletonMap Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2014 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.map.immutable.primitive;

import java.io.IOException;
import java.io.Serializable;
import java.util.Set;

import com.gs.collections.api.BooleanIterable;
import com.gs.collections.api.LazyBooleanIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.primitive.MutableBooleanBag;
import com.gs.collections.api.block.function.primitive.BooleanToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.BooleanPredicate;
import com.gs.collections.api.block.predicate.primitive.ObjectBooleanPredicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.primitive.BooleanProcedure;
import com.gs.collections.api.block.procedure.primitive.ObjectBooleanProcedure;
import com.gs.collections.api.collection.ImmutableCollection;
import com.gs.collections.api.collection.primitive.ImmutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.iterator.BooleanIterator;
import com.gs.collections.api.list.primitive.MutableBooleanList;
import com.gs.collections.api.map.primitive.ImmutableObjectBooleanMap;
import com.gs.collections.api.map.primitive.ObjectBooleanMap;
import com.gs.collections.api.set.primitive.MutableBooleanSet;
import com.gs.collections.api.tuple.primitive.ObjectBooleanPair;
import com.gs.collections.impl.bag.mutable.primitive.BooleanHashBag;
import com.gs.collections.impl.collection.mutable.primitive.UnmodifiableBooleanCollection;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.factory.primitive.BooleanLists;
import com.gs.collections.impl.factory.primitive.ObjectBooleanMaps;
import com.gs.collections.impl.lazy.primitive.LazyBooleanIterableAdapter;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.map.mutable.primitive.ObjectBooleanHashMap;
import com.gs.collections.impl.set.mutable.primitive.BooleanHashSet;
import com.gs.collections.impl.tuple.primitive.PrimitiveTuples;

/**
 * ImmutableObjectBooleanSingletonMap is an optimization for {@link ImmutableObjectBooleanMap} of size 1.
 * This file was automatically generated from template file immutableObjectPrimitiveSingletonMap.stg.
 *
 * @since 4.0.
 */
final class ImmutableObjectBooleanSingletonMap extends AbstractImmutableObjectBooleanMap implements Serializable
{
    private static final long serialVersionUID = 1L;
    private static final boolean EMPTY_VALUE = false;
    private final K key1;
    private final boolean value1;

    ImmutableObjectBooleanSingletonMap(K key1, boolean value1)
    {
        this.key1 = key1;
        this.value1 = value1;
    }

    public BooleanIterator booleanIterator()
    {
        return ObjectBooleanHashMap.newWithKeysValues(this.key1, this.value1).booleanIterator();
    }

    public void forEach(BooleanProcedure procedure)
    {
        procedure.value(this.value1);
    }

    public int count(BooleanPredicate predicate)
    {
        return predicate.accept(this.value1) ? 1 : 0;
    }

    public boolean anySatisfy(BooleanPredicate predicate)
    {
        return predicate.accept(this.value1);
    }

    public boolean allSatisfy(BooleanPredicate predicate)
    {
        return predicate.accept(this.value1);
    }

    public boolean noneSatisfy(BooleanPredicate predicate)
    {
        return !predicate.accept(this.value1);
    }

    public ImmutableBooleanCollection select(BooleanPredicate predicate)
    {
        return predicate.accept(this.value1) ? BooleanLists.immutable.with(this.value1) : BooleanLists.immutable.with();
    }

    public ImmutableBooleanCollection reject(BooleanPredicate predicate)
    {
        return predicate.accept(this.value1) ? BooleanLists.immutable.with() : BooleanLists.immutable.with(this.value1);
    }

    public boolean detectIfNone(BooleanPredicate predicate, boolean ifNone)
    {
        return predicate.accept(this.value1) ? this.value1 : ifNone;
    }

    public  T injectInto(T injectedValue, ObjectBooleanToObjectFunction function)
    {
        return function.valueOf(injectedValue, this.value1);
    }

    public  ImmutableCollection collect(BooleanToObjectFunction function)
    {
        return Lists.immutable.of(function.valueOf(this.value1));
    }

    public boolean[] toArray()
    {
        return new boolean[]{this.value1};
    }

    public boolean contains(boolean value)
    {
        return this.value1 == value;
    }

    public boolean containsAll(boolean... source)
    {
        for (boolean value : source)
        {
            if (this.value1 != value)
            {
                return false;
            }
        }
        return true;
    }

    public boolean containsAll(BooleanIterable source)
    {
        for (BooleanIterator iterator = source.booleanIterator(); iterator.hasNext(); )
        {
            if (this.value1 != iterator.next())
            {
                return false;
            }
        }
        return true;
    }

    public MutableBooleanList toList()
    {
        return BooleanArrayList.newListWith(this.value1);
    }

    public MutableBooleanSet toSet()
    {
        return BooleanHashSet.newSetWith(this.value1);
    }

    public MutableBooleanBag toBag()
    {
        return BooleanHashBag.newBagWith(this.value1);
    }

    public LazyBooleanIterable asLazy()
    {
        return new LazyBooleanIterableAdapter(this);
    }

    public ImmutableObjectBooleanMap newWithKeyValue(K key, boolean value)
    {
        return ObjectBooleanMaps.immutable.withAll(ObjectBooleanHashMap.newWithKeysValues(this.key1, this.value1, key, value));
    }

    public ImmutableObjectBooleanMap newWithoutKey(K key)
    {
        return nullSafeEquals(this.key1, key) ? (ImmutableObjectBooleanMap) ImmutableObjectBooleanEmptyMap.INSTANCE : this;
    }

    private static boolean nullSafeEquals(Object value, Object other)
    {
        if (value == null)
        {
            if (other == null)
            {
                return true;
            }
        }
        else if (other == value || value.equals(other))
        {
            return true;
        }
        return false;
    }

    public ImmutableObjectBooleanMap newWithoutAllKeys(Iterable keys)
    {
        ObjectBooleanHashMap map = new ObjectBooleanHashMap(this);
        for (K key : keys)
        {
            map.removeKey(key);
        }
        return map.toImmutable();
    }

    public boolean get(Object key)
    {
        return this.getIfAbsent(key, EMPTY_VALUE);
    }

    public boolean getOrThrow(Object key)
    {
        if (nullSafeEquals(this.key1, key))
        {
            return this.value1;
        }
        throw new IllegalStateException("Key " + key + " not present.");
    }

    public boolean getIfAbsent(Object key, boolean ifAbsent)
    {
        return nullSafeEquals(this.key1, key) ? this.value1 : ifAbsent;
    }

    public boolean containsKey(Object key)
    {
        return nullSafeEquals(this.key1, key);
    }

    public boolean containsValue(boolean value)
    {
        return this.value1 == value;
    }

    public void forEachValue(BooleanProcedure procedure)
    {
        procedure.value(this.value1);
    }

    public void forEachKey(Procedure procedure)
    {
        procedure.value(this.key1);
    }

    public void forEachKeyValue(ObjectBooleanProcedure objectBooleanProcedure)
    {
        objectBooleanProcedure.value(this.key1, this.value1);
    }

    public ImmutableObjectBooleanMap select(ObjectBooleanPredicate objectBooleanPredicate)
    {
        return objectBooleanPredicate.accept(this.key1, this.value1) ? ObjectBooleanHashMap.newWithKeysValues(this.key1, this.value1).toImmutable()
                : (ImmutableObjectBooleanMap) ImmutableObjectBooleanEmptyMap.INSTANCE;
    }

    public ImmutableObjectBooleanMap reject(ObjectBooleanPredicate objectBooleanPredicate)
    {
        return objectBooleanPredicate.accept(this.key1, this.value1) ? (ImmutableObjectBooleanMap) ImmutableObjectBooleanEmptyMap.INSTANCE
                : ObjectBooleanHashMap.newWithKeysValues(this.key1, this.value1).toImmutable();
    }

    public ImmutableObjectBooleanMap toImmutable()
    {
        return this;
    }

    public int size()
    {
        return 1;
    }

    public boolean isEmpty()
    {
        return false;
    }

    public boolean notEmpty()
    {
        return true;
    }

    public Set keySet()
    {
        return Sets.immutable.of(this.key1).castToSet();
    }

    public MutableBooleanCollection values()
    {
        return UnmodifiableBooleanCollection.of(BooleanArrayList.newListWith(this.value1));
    }

    public LazyIterable keysView()
    {
        return Lists.immutable.of(this.key1).asLazy();
    }

    public RichIterable> keyValuesView()
    {
        return Lists.immutable.of(PrimitiveTuples.pair(this.key1, this.value1)).asLazy();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }
        if (!(obj instanceof ObjectBooleanMap))
        {
            return false;
        }
        ObjectBooleanMap map = (ObjectBooleanMap) obj;
        if (map.size() != 1)
        {
            return false;
        }
        return map.containsKey(this.key1) && this.value1 == map.getOrThrow(this.key1);
    }

    @Override
    public int hashCode()
    {
        return (this.key1 == null ? 0 : this.key1.hashCode()) ^ (this.value1 ? 1231 : 1237)  ;
    }

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

    public String makeString()
    {
        return this.makeString(", ");
    }

    public String makeString(String separator)
    {
        return this.makeString("", separator, "");
    }

    public String makeString(String start, String separator, String end)
    {
        Appendable stringBuilder = new StringBuilder();
        this.appendString(stringBuilder, start, separator, end);
        return stringBuilder.toString();
    }

    public void appendString(Appendable appendable)
    {
        this.appendString(appendable, ", ");
    }

    public void appendString(Appendable appendable, String separator)
    {
        this.appendString(appendable, "", separator, "");
    }

    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);
        }
    }

    private Object writeReplace()
    {
        return new ImmutableObjectBooleanMapSerializationProxy(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy