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

org.organicdesign.fp.collections.ImInsertOrderMap 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 org.organicdesign.fp.tuple.Tuple2;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;

public class ImInsertOrderMap implements ImMap, Serializable {

    // For serializable.  Make sure to change whenever internal data format changes.
    private static final long serialVersionUID = 20160914085100L;

    private static final class Pair implements Serializable {
        // For serializable.  Make sure to change whenever internal data format changes.
        private static final long serialVersionUID = 20160914085100L;

        int idx;
        V val;
        Pair(V v, int i) { val = v; idx = i;}
    }

    private final ImMap> inner;
    private final int index;
    private ImInsertOrderMap(ImMap> kvs, int idx) {
        inner = kvs; index = idx;
    }

    private static final ImInsertOrderMap EMPTY =
            new ImInsertOrderMap<>(PersistentHashMap.empty(), 0);

    @SuppressWarnings("unchecked")
    public static  ImInsertOrderMap empty() { return EMPTY; }

    @Override public Option> entry(K key) {
        Option>> innerEntry = inner.entry(key);
        return innerEntry.then(entry -> Option.of(Tuple2.of(key, entry.getValue().val)));
    }

    @Override public ImMap assoc(K key, V val) {
        int nextIdx = index + 1;
        return new ImInsertOrderMap<>(inner.assoc(key, new Pair<>(val, nextIdx)),
                                      nextIdx);
    }

    @Override public ImMap without(K key) {
        return new ImInsertOrderMap<>(inner.without(key), index);
    }

    @Override public int size() { return inner.size(); }

    @Override public ImInsertOrderSet keySet() {
        return ImInsertOrderSet.ofMap((ImInsertOrderMap) inner);
    }

    @Override public boolean equals(Object o) {
        if (this == o) { return true; }
        if ( !(o instanceof Map) ) return false;
        Map that = (Map) o;
        if (this.size() != that.size()) {
            return false;
        }
        for (UnEntry> entry : inner) {
            K key = entry.getKey();
            if (!that.containsKey(key)) { return false; }
            if (!that.get(key).equals(entry.getValue().val)) { return false; }
        }
        return true;
    }

    @Override public int hashCode() {
        return inner.hashCode();
    }

    private static final class InsOrdComp implements Comparator>>,
                                                          Serializable {
        // For serializable.  Make sure to change whenever internal data format changes.
        private static final long serialVersionUID = 20160914085100L;

        private InsOrdComp() {}

        @Override
        public int compare(UnEntry> a,
                           UnEntry> b) {
            int aInt = a.getValue().idx;
            int bInt = b.getValue().idx;
            return aInt > bInt ? 1 :
                   aInt < bInt ? -1 : 0;
        }

        private static final InsOrdComp INSTANCE = new InsOrdComp();
    }

    @SuppressWarnings("unchecked")
    private static  Comparator>> defaultComparator() {
        return InsOrdComp.INSTANCE;
    }

    @SuppressWarnings("unchecked")
    static  Comparator> setComparator() {
        return InsOrdComp.INSTANCE;
    }

    @Override public UnmodSortedIterator> iterator() {
        return inner.toImSortedSet(defaultComparator())
                    .map(e -> (UnEntry) Tuple2.of(e.getKey(), e.getValue().val))
                    .toImList().iterator();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy