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

com.gs.collections.impl.set.fixed.EmptySet Maven / Gradle / Ivy

/*
 * Copyright 2011 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.set.fixed;

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

import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.EmptyIterator;
import com.gs.collections.impl.factory.Sets;
import net.jcip.annotations.Immutable;

/**
 * This class is a memory efficient list with no elements.  It is created by calling Lists.fixedSize.of() which
 * actually returns a singleton instance.
 */
@Immutable
final class EmptySet
        extends AbstractMemoryEfficientMutableSet
        implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Object readResolve()
    {
        return Sets.fixedSize.of();
    }

    // Weird implementation of clone() is ok on final classes

    @Override
    public EmptySet clone()
    {
        return this;
    }

    public int size()
    {
        return 0;
    }

    @Override
    public boolean contains(Object obj)
    {
        return false;
    }

    public T getFirst()
    {
        return null;
    }

    public T getLast()
    {
        return null;
    }

    public void forEach(Procedure procedure)
    {
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { } public Iterator iterator() { return EmptyIterator.getInstance(); } @Override public boolean equals(Object other) { if (other == this) { return true; } if (!(other instanceof Set)) { return false; } Set set = (Set) other; return set.isEmpty(); } @Override public int hashCode() { return 0; } @Override public T min(Comparator comparator) { throw new NoSuchElementException(); } @Override public T max(Comparator comparator) { throw new NoSuchElementException(); } @Override public T min() { throw new NoSuchElementException(); } @Override public T max() { throw new NoSuchElementException(); } @Override public > T minBy(Function function) { throw new NoSuchElementException(); } @Override public > T maxBy(Function function) { throw new NoSuchElementException(); } @Override public MutableSet> zip(Iterable that) { return Sets.fixedSize.of(); } @Override public >> R zip(Iterable that, R target) { return target; } @Override public MutableSet> zipWithIndex() { return Sets.fixedSize.of(); } @Override public >> R zipWithIndex(R target) { return target; } public MutableSet with(T element) { return new SingletonSet(element); } public MutableSet without(T element) { return this; } }