org.organicdesign.fp.collections.ImMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of UncleJim Show documentation
Show all versions of UncleJim Show documentation
Immutable Clojure collections and a Transformation abstraction for Java 8+, immutably, type-safely, and with good performance. Name will change to "Paguro" in November 2016.
package org.organicdesign.fp.collections;
import java.util.Map;
import org.organicdesign.fp.Option;
/** An immutable map with no guarantees about its ordering. */
public interface ImMap extends UnmodMap {
Option> entry(K key);
// Sequence> seq();
/** Returns a new map with the given key/value added */
ImMap assoc(K key, V val);
/** Returns a new map with the given key/value removed */
ImMap without(K key);
/**
Returns a view of the mappings contained in this map. The set should actually contain
UnmodMap.Entry items, but that return signature is illegal in Java, so you'll just have to
remember.
*/
@Override default ImSet> entrySet() {
return map(e -> (Map.Entry) e)
.toImSet();
}
/** Returns a view of the keys contained in this map. */
@Override ImSet keySet();
@SuppressWarnings("unchecked")
@Override default boolean containsKey(Object key) { return entry((K) key).isSome(); }
@SuppressWarnings("unchecked")
@Override default V get(Object key) {
Option> entry = entry((K) key);
return entry.isSome() ? entry.get().getValue() : null;
}
default V getOrElse(K key, V notFound) {
Option> entry = entry(key);
return entry.isSome() ? entry.get().getValue() : notFound;
}
// @Override default UnmodCollection values() { return map(e -> e.getValue()).toImSet(); }
// @Override default UnmodIterator> iterator() { return seq().iterator(); }
/** Returns a new map with an immutable copy of the given entry added */
default ImMap assoc(Map.Entry entry) { return assoc(entry.getKey(), entry.getValue()); }
}