
org.organicdesign.fp.collections.AbstractUnmodMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Paguro Show documentation
Show all versions of Paguro Show documentation
Immutable Clojure collections and a Transformation abstraction for Java 8+, immutably, type-safely, and with good performance.
package org.organicdesign.fp.collections;
import java.util.AbstractMap;
import java.util.Map;
/**
Implements equals() and hashCode() methods compatible with java.util.Map (which ignores order)
to make defining unmod Maps easier. Inherits hashCode() and toString() from
AbstractUnmodIterable.
*/
public abstract class AbstractUnmodMap
extends AbstractUnmodIterable>
implements UnmodMap {
/**
When comparing against a SortedMap, this is correct and O(n) fast, but BEWARE! It is also
Compatible with {@link AbstractMap#equals(Object)} which unfortunately means equality as defined by this method
(and java.util.AbstractMap) is not commutative when comparing ordered and unordered maps (it is
also O(n log n) slow). The Equator defined by this class prevents comparison with unordered
Maps.
*/
@Override public boolean equals(Object other) {
if (this == other) { return true; }
if (!(other instanceof Map)) { return false; }
Map, ?> that = (Map, ?>) other;
if (that.size() != size()) { return false; }
try {
for (Entry e : this) {
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(that.get(key) == null && that.containsKey(key))) {
return false;
}
} else {
if (!value.equals(that.get(key))) {
return false;
}
}
}
} catch (ClassCastException | NullPointerException unused) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy