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

org.eclipse.collections.impl.bag.immutable.ImmutableSingletonBag Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2016 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

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

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Optional;

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.multimap.bag.ImmutableBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.MultimapEachPutProcedure;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;

/**
 * @since 1.0
 */
final class ImmutableSingletonBag
        extends AbstractImmutableBag implements Serializable
{
    private static final long serialVersionUID = 1L;

    private final T value;

    ImmutableSingletonBag(T object)
    {
        this.value = object;
    }

    @Override
    public boolean allSatisfy(Predicate predicate)
    {
        return predicate.accept(this.value);
    }

    @Override
    public boolean noneSatisfy(Predicate predicate)
    {
        return !predicate.accept(this.value);
    }

    @Override
    public  IV injectInto(IV injectedValue, Function2 function)
    {
        return function.value(injectedValue, this.value);
    }

    @Override
    public T min(Comparator comparator)
    {
        return this.value;
    }

    @Override
    public T max(Comparator comparator)
    {
        return this.value;
    }

    @Override
    public T min()
    {
        return this.value;
    }

    @Override
    public T max()
    {
        return this.value;
    }

    @Override
    public > T minBy(Function function)
    {
        return this.value;
    }

    @Override
    public > T maxBy(Function function)
    {
        return this.value;
    }

    @Override
    public ImmutableBag newWith(T element)
    {
        return Bags.immutable.with(this.value, element);
    }

    @Override
    public ImmutableBag newWithout(T element)
    {
        return this.emptyIfMatchesOrThis(Predicates.equal(element));
    }

    private ImmutableBag emptyIfMatchesOrThis(Predicate predicate)
    {
        return predicate.accept(this.value) ? Bags.immutable.empty() : this;
    }

    @Override
    public ImmutableBag newWithAll(Iterable elements)
    {
        return HashBag.newBag(elements).with(this.value).toImmutable();
    }

    @Override
    public ImmutableBag newWithoutAll(Iterable elements)
    {
        return this.emptyIfMatchesOrThis(Predicates.in(elements));
    }

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

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

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

    @Override
    public T getFirst()
    {
        return this.value;
    }

    @Override
    public T getLast()
    {
        return this.value;
    }

    @Override
    public T getOnly()
    {
        return this.value;
    }

    @Override
    public boolean contains(Object object)
    {
        return Comparators.nullSafeEquals(this.value, object);
    }

    @Override
    public boolean containsAllIterable(Iterable source)
    {
        return Iterate.allSatisfy(source, Predicates.equal(this.value));
    }

    @Override
    public boolean containsAllArguments(Object... elements)
    {
        return ArrayIterate.allSatisfy(elements, Predicates.equal(this.value));
    }

    @Override
    public ImmutableBag selectByOccurrences(IntPredicate predicate)
    {
        return predicate.accept(1)
                ? this
                : Bags.immutable.empty();
    }

    @Override
    public ImmutableBag select(Predicate predicate)
    {
        return predicate.accept(this.value)
                ? this
                : Bags.immutable.empty();
    }

    @Override
    public > R select(Predicate predicate, R target)
    {
        if (predicate.accept(this.value))
        {
            target.add(this.value);
        }
        return target;
    }

    @Override
    public > R selectWith(
            Predicate2 predicate,
            P parameter,
            R target)
    {
        if (predicate.accept(this.value, parameter))
        {
            target.add(this.value);
        }
        return target;
    }

    @Override
    public ImmutableBag reject(Predicate predicate)
    {
        return predicate.accept(this.value)
                ? Bags.immutable.empty()
                : this;
    }

    @Override
    public > R reject(Predicate predicate, R target)
    {
        if (!predicate.accept(this.value))
        {
            target.add(this.value);
        }
        return target;
    }

    @Override
    public > R rejectWith(
            Predicate2 predicate,
            P parameter,
            R target)
    {
        if (!predicate.accept(this.value, parameter))
        {
            target.add(this.value);
        }
        return target;
    }

    @Override
    public  ImmutableBag selectInstancesOf(Class clazz)
    {
        return clazz.isInstance(this.value)
                ? (ImmutableBag) this
                : Bags.immutable.empty();
    }

    @Override
    public  ImmutableBag collect(Function function)
    {
        return Bags.immutable.with(function.valueOf(this.value));
    }

    @Override
    public  ImmutableBag collectIf(
            Predicate predicate,
            Function function)
    {
        return predicate.accept(this.value)
                ? Bags.immutable.with(function.valueOf(this.value))
                : Bags.immutable.empty();
    }

    @Override
    public > R collectIf(
            Predicate predicate, Function function, R target)
    {
        if (predicate.accept(this.value))
        {
            target.add(function.valueOf(this.value));
        }
        return target;
    }

    @Override
    public  ImmutableBag flatCollect(Function> function)
    {
        return this.flatCollect(function, HashBag.newBag()).toImmutable();
    }

    @Override
    public > R flatCollect(
            Function> function, R target)
    {
        Iterate.addAllTo(function.valueOf(this.value), target);
        return target;
    }

    @Override
    public T detect(Predicate predicate)
    {
        return predicate.accept(this.value)
                ? this.value
                : null;
    }

    @Override
    public Optional detectOptional(Predicate predicate)
    {
        return predicate.accept(this.value)
                ? Optional.of(this.value)
                : Optional.empty();
    }

    @Override
    public T detectIfNone(Predicate predicate, Function0 function)
    {
        return predicate.accept(this.value)
                ? this.value
                : function.value();
    }

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

    @Override
    public boolean anySatisfy(Predicate predicate)
    {
        return predicate.accept(this.value);
    }

    @Override
    public  ImmutableBagMultimap groupBy(Function function)
    {
        return this.groupBy(function, HashBagMultimap.newMultimap()).toImmutable();
    }

    @Override
    public > R groupBy(Function function, R target)
    {
        target.putAll(function.valueOf(this.value), this);
        return target;
    }

    @Override
    public  ImmutableBagMultimap groupByEach(Function> function)
    {
        return this.groupByEach(function, HashBagMultimap.newMultimap()).toImmutable();
    }

    @Override
    public > R groupByEach(Function> function, R target)
    {
        this.forEach(MultimapEachPutProcedure.on(target, function));
        return target;
    }

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

    @Override
    public int occurrencesOf(Object item)
    {
        return Comparators.nullSafeEquals(this.value, item) ? 1 : 0;
    }

    @Override
    public void forEachWithOccurrences(ObjectIntProcedure objectIntProcedure)
    {
        objectIntProcedure.value(this.value, 1);
    }

    @Override
    public MutableMap toMapOfItemToCount()
    {
        return UnifiedMap.newWithKeysValues(this.value, 1);
    }

    @Override
    public ImmutableBag toImmutable()
    {
        return this;
    }

    @Override
    public void each(Procedure procedure)
    {
        procedure.value(this.value);
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        objectIntProcedure.value(this.value, 0);
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { procedure.value(this.value, parameter); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public ImmutableBag> zip(Iterable that) { Iterator iterator = that.iterator(); if (!iterator.hasNext()) { return Bags.immutable.empty(); } return Bags.immutable.with(Tuples.pair(this.value, iterator.next())); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public ImmutableSet> zipWithIndex() { return Sets.immutable.with(Tuples.pair(this.value, 0)); } @Override public Iterator iterator() { return new SingletonIterator(); } private class SingletonIterator implements Iterator { private boolean next = true; @Override public boolean hasNext() { return this.next; } @Override public T next() { if (this.next) { this.next = false; return ImmutableSingletonBag.this.value; } throw new NoSuchElementException("i=" + this.next); } @Override public void remove() { throw new UnsupportedOperationException("Cannot remove from an ImmutableBag"); } } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Bag)) { return false; } Bag bag = (Bag) obj; if (this.size() != bag.size()) { return false; } return this.occurrencesOf(this.value) == bag.occurrencesOf(this.value); } @Override public String toString() { return '[' + this.makeString() + ']'; } @Override public int hashCode() { return (this.value == null ? 0 : this.value.hashCode()) ^ 1; } private Object writeReplace() { return new ImmutableBagSerializationProxy<>(this); } }