All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.organicdesign.fp.collections.ImMap Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.0.13
Show newest version
package org.organicdesign.fp.collections;

import org.organicdesign.fp.Option;

import java.util.Map;

/** 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()); }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy