org.organicdesign.fp.collections.ImMap Maven / Gradle / Ivy
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()); }
}