org.eclipse.collections.impl.map.fixed.EmptyMap Maven / Gradle / Ivy
/*
* 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.map.fixed;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate2;
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.FixedSizeMap;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.set.MutableSetMultimap;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Multimaps;
import org.eclipse.collections.impl.factory.Sets;
@Immutable
final class EmptyMap
extends AbstractMemoryEfficientMutableMap
implements Serializable
{
private static final long serialVersionUID = 1L;
private Object readResolve()
{
return Maps.fixedSize.of();
}
@Override
public int size()
{
return 0;
}
@Override
public MutableMap withKeyValue(K addKey, V addValue)
{
return new SingletonMap<>(addKey, addValue);
}
@Override
public MutableMap withoutKey(K key)
{
return this;
}
// Weird implementation of clone() is ok on final classes
@Override
public EmptyMap clone()
{
return this;
}
@Override
public ImmutableMap toImmutable()
{
return Maps.immutable.empty();
}
@Override
public MutableSetMultimap flip()
{
return Multimaps.mutable.set.with();
}
@Override
public boolean containsKey(Object key)
{
return false;
}
@Override
public boolean containsValue(Object value)
{
return false;
}
@Override
public V get(Object key)
{
return null;
}
@Override
public Set keySet()
{
return Sets.fixedSize.of();
}
@Override
public Collection values()
{
return Lists.fixedSize.of();
}
@Override
public MutableSet> entrySet()
{
return Sets.fixedSize.of();
}
@Override
public String toString()
{
return "{}";
}
@Override
public int hashCode()
{
return 0;
}
@Override
public boolean equals(Object other)
{
if (!(other instanceof Map))
{
return false;
}
Map that = (Map) other;
return that.size() == this.size();
}
@Override
public MutableMap flipUniqueValues()
{
return Maps.fixedSize.with();
}
@Override
public FixedSizeMap tap(Procedure super V> procedure)
{
return this;
}
@Override
public void forEachKeyValue(Procedure2 super K, ? super V> procedure)
{
}
@Override
public void forEachKey(Procedure super K> procedure)
{
}
@Override
public void forEachValue(Procedure super V> procedure)
{
}
@Override
public void forEachWithIndex(ObjectIntProcedure super V> objectIntProcedure)
{
}
@Override
public void forEachWith(Procedure2 super V, ? super P> procedure, P parameter)
{
}
@Override
public FixedSizeMap select(Predicate2 super K, ? super V> predicate)
{
return Maps.fixedSize.of();
}
@Override
public FixedSizeMap collectValues(Function2 super K, ? super V, ? extends R> function)
{
return Maps.fixedSize.of();
}
@Override
public FixedSizeMap collect(Function2 super K, ? super V, Pair> function)
{
return Maps.fixedSize.of();
}
@Override
public FixedSizeMap reject(Predicate2 super K, ? super V> predicate)
{
return Maps.fixedSize.of();
}
@Override
public Pair detect(Predicate2 super K, ? super V> predicate)
{
return null;
}
@Override
public V getOnly()
{
throw new IllegalStateException("Size must be 1 but was " + this.size());
}
}