org.javimmutable.collections.MapEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javimmutable-collections Show documentation
Show all versions of javimmutable-collections Show documentation
Library providing immutable/persistent collection classes for
Java. While collections are immutable they provide methods for
adding and removing values by creating new modified copies of
themselves. Each copy shares almost all of its structure with
other copies to minimize memory consumption.
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2019, Burton Computer Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Burton Computer Corporation nor the names
// of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package org.javimmutable.collections;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Immutable implementation of both Map.Entry and JImmutableMap.Entry that uses the same equals() and hashCode() implementations as
* documented in javadoc for Map.Entry.
*/
@Immutable
public class MapEntry
implements JImmutableMap.Entry,
Map.Entry
{
@Nonnull
protected final K key;
protected final V value;
public MapEntry(@Nonnull Map.Entry entry)
{
this(entry.getKey(), entry.getValue());
}
public MapEntry(@Nonnull JImmutableMap.Entry entry)
{
this(entry.getKey(), entry.getValue());
}
public MapEntry(@Nonnull K key,
V value)
{
this.key = key;
this.value = value;
}
@Nonnull
public static MapEntry of(@Nonnull Map.Entry entry)
{
return new MapEntry(entry);
}
@Nonnull
public static MapEntry of(@Nonnull JImmutableMap.Entry entry)
{
return new MapEntry(entry);
}
@Nonnull
public static MapEntry of(@Nonnull K key,
V value)
{
return new MapEntry(key, value);
}
@Nonnull
public static JImmutableMap.Entry entry(@Nonnull K key,
V value)
{
return new MapEntry(key, value);
}
@Nonnull
public static Map.Entry javaEntry(@Nonnull K key,
V value)
{
return new MapEntry(key, value);
}
public static , V> int compareKeys(@Nonnull JImmutableMap.Entry a,
@Nonnull JImmutableMap.Entry b)
{
return a.getKey().compareTo(b.getKey());
}
@Nonnull
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V v)
{
throw new UnsupportedOperationException();
}
public JImmutableMap.Entry asEntry()
{
return this;
}
public Map.Entry asJavaEntry()
{
return this;
}
@Override
public int hashCode()
{
return ((key == null) ? 0 : key.hashCode()) ^
((value == null) ? 0 : value.hashCode());
}
@Override
public boolean equals(Object o)
{
if (o instanceof JImmutableMap.Entry) {
JImmutableMap.Entry jentry = (JImmutableMap.Entry)o;
//noinspection ConstantConditions
return ((key == null) ? (jentry.getKey() == null) : key.equals(jentry.getKey())) &&
((value == null) ? (jentry.getValue() == null) : value.equals(jentry.getValue()));
}
if (!(o instanceof Map.Entry)) {
return false;
}
Map.Entry entry2 = (Map.Entry)o;
return ((key == null) ? (entry2.getKey() == null) : key.equals(entry2.getKey())) &&
((value == null) ? (entry2.getValue() == null) : value.equals(entry2.getValue()));
}
@Override
public String toString()
{
return makeToString(this);
}
private static void addToString(StringBuilder sb,
Object obj)
{
if (obj == null) {
sb.append("null");
} else {
sb.append(obj);
}
}
public static String makeToString(JImmutableMap.Entry entry)
{
StringBuilder sb = new StringBuilder();
addToString(sb, entry);
return sb.toString();
}
public static void addToString(StringBuilder sb,
JImmutableMap.Entry entry)
{
addToString(sb, entry.getKey(), entry.getValue());
}
public static void addToString(StringBuilder sb,
K key,
V value)
{
addToString(sb, key);
sb.append("=");
addToString(sb, value);
}
public static List> toMutableEntries(@Nonnull Collection> source)
{
return source.stream().map(e -> MapEntry.of(e)).collect(Collectors.toList());
}
public static List> toImmutableEntries(@Nonnull Collection> source)
{
return source.stream().map(e -> MapEntry.of(e)).collect(Collectors.toList());
}
}