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

com.epam.commons.map.MapArray Maven / Gradle / Ivy

There is a newer version: 1.1.34
Show newest version
package com.epam.commons.map;
/*
 * Copyright 2004-2016 EPAM Systems
 *
 * This file is part of JDI project.
 *
 * JDI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JDI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JDI. If not, see .
 */

import com.epam.commons.LinqUtils;
import com.epam.commons.linqinterfaces.JActionTTEx;
import com.epam.commons.linqinterfaces.JFuncTREx;
import com.epam.commons.linqinterfaces.JFuncTTREx;
import com.epam.commons.pairs.Pair;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.epam.commons.PrintUtils.print;
import static com.epam.commons.TryCatchUtil.throwRuntimeException;
import static java.util.stream.Collectors.toList;

/**
 * Created by Roman_Iovlev on 6/3/2015.
 */
public class MapArray implements Collection>, Cloneable {
    public List> pairs;

    public MapArray() {
        pairs = new CopyOnWriteArrayList<>();
    }

    public MapArray(K key, V value) {
        this();
        add(key, value);
    }

    public  MapArray(Collection collection, JFuncTREx key, JFuncTREx value) {
        this();
        try {
            for (T t : collection)
                add(key.invoke(t), value.invoke(t));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }

    public MapArray(Collection collection, JFuncTREx value) {
        this();
        try {
            for (K k : collection)
                add(k, value.invoke(k));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }

    public  MapArray(T[] array, JFuncTREx key, JFuncTREx value) {
        this();
        try {
            for (T t : array)
                add(key.invoke(t), value.invoke(t));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }

    public MapArray(K[] array, JFuncTREx value) {
        this();
        try {
        for (K k : array)
            add(k, value.invoke(k));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }

    public MapArray(int count, JFuncTREx key, JFuncTREx value) {
        this();
        try {
        for (int i = 0; i < count; i++)
            add(key.invoke(i), value.invoke(i));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }
    public MapArray(int count, JFuncTREx> pairFunc) {
        this();
        try {
        for (int i = 0; i < count; i++)
            add(pairFunc.invoke(i));
        } catch (Exception ex) {
            throw new RuntimeException("Can't create MapArray"); }
    }

    public MapArray(MapArray mapArray) {
        this();
        addAll(mapArray.stream().collect(toList()));
    }

    public MapArray(Object[][] objects) {
        this();
        add(objects);
    }
    public MapArray(List keys, List values) {
        this();
        if (keys == null || values == null ||
            keys.size() == 0 || keys.size() != values.size())
            throw new RuntimeException("keys and values count not equal");
        for (int i = 0; i < keys.size(); i++)
            add(keys.get(i), values.get(i));
    }

    public static  MapArray toMapArray(Collection collection) {
        MapArray mapArray = new MapArray<>();
        int i = 0;
        for (T t : collection)
            mapArray.add(i++, t);
        return mapArray;
    }

    public static  MapArray toMapArray(T[] array) {
        Set mySet = new HashSet<>();
        Collections.addAll(mySet, array);
        return toMapArray(mySet);
    }

    public static  MapArray toMapArray(Map map) {
        MapArray mapArray = new MapArray<>();
        for (Map.Entry e : map.entrySet())
            mapArray.add(e.getKey(), e.getValue());
        return mapArray;
    }

    public  MapArray toMapArray(
            JFuncTTREx key, JFuncTTREx value) {
        MapArray result = new MapArray<>();
        try {
            for (Pair pair : pairs)
                result.add(key.invoke(pair.key, pair.value), value.invoke(pair.key, pair.value));
        } catch (Exception ex) {
            throw new RuntimeException("Can't convert toMapArray"); }
        return result;
    }

    public  MapArray toMapArray(JFuncTREx value) {
        MapArray result = new MapArray<>();
        try {
        for (Pair pair : pairs)
            result.add(pair.key, value.invoke(pair.value));
        return result;
        } catch (Exception ex) {
            throw new RuntimeException("Can't convert toMapArray"); }
    }

    public Map toMap() {
        return toMap(v -> v);
    }
    public  Map toMap(JFuncTREx value) {
        return toMap((k, v) -> k, (k,v) -> value.invoke(v));
    }
    public  Map toMap(
            JFuncTTREx key, JFuncTTREx value) {
        Map result = new HashMap<>();
        try {
            for (Pair pair : pairs)
                result.put(key.invoke(pair.key, pair.value),
                        value.invoke(pair.key, pair.value));
        } catch (Exception ex) {
            throw new RuntimeException("Can't convert toMap"); }
        return result;
    }

    public boolean add(K key, V value) {
        if (hasKey(key))
            return false;
        pairs.add(new Pair<>(key, value));
        return true;
    }
    public MapArray update(K key, V value) {
        if (hasKey(key))
            removeByKey(key);
        pairs.add(new Pair<>(key, value));
        return this;
    }


    public MapArray update(K key, JFuncTREx func) {
        V value = null;
        if (hasKey(key)) {
            value = get(key);
            removeByKey(key);
        }
        try {
            pairs.add(new Pair<>(key, func.invoke(value)));
        } catch (Exception ex) {
            throw new RuntimeException("Can't do update"); }
        return this;
    }

    public void add(Object[][] pairs) {
        for (Object[] pair : pairs)
            if (pair.length == 2)
                add((K) pair[0], (V) pair[1]);
    }

    public void addOrReplace(K key, V value) {
        if (hasKey(key))
            removeByKey(key);
        add(key, value);
    }

    public void addOrReplace(Object[][] pairs) {
        for (Object[] pair : pairs)
            if (pair.length == 2)
                addOrReplace((K) pair[0], (V) pair[1]);
    }

    private boolean hasKey(K key) {
        return keys().contains(key);
    }

    public boolean addFirst(K key, V value) {
        if (hasKey(key))
            return false;
        List> result = new CopyOnWriteArrayList<>();
        result.add(new Pair<>(key, value));
        result.addAll(pairs);
        pairs = result;
        return true;
    }

    public V get(K key) {
        Pair first = null;
        try {
            first = LinqUtils.first(pairs, pair -> pair.key.equals(key));
        } catch (Exception ignore) {
        }
        return (first != null) ? first.value : null;
    }

    public Pair get(int i) {
        int index = i >= 0 ? i : pairs.size() + i;
        if (index < 0)
            return null;
        return (pairs.size() > index)
                ? pairs.get(index)
                : null;
    }

    public Pair getFromEnd(int index) {
        return get(size() - index - 1);
    }

    public K key(int index) {
        return get(index).key;
    }

    public V value(int index) {
        return get(index).value;
    }

    public Collection keys() {
        return LinqUtils.select(pairs, pair -> pair.key);
    }

    public Collection values() {
        return LinqUtils.select(pairs, pair -> pair.value);
    }

    public Collection values(JFuncTREx condition) {
        return LinqUtils.where(values(), condition);
    }

    public int size() {
        return pairs.size();
    }

    public int count() {
        return size();
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean any() {
        return size() > 0;
    }

    public Pair first() {
        return get(0);
    }

    public Pair last() {
        return getFromEnd(0);
    }

    public MapArray revert() {
        List> result = new CopyOnWriteArrayList<>();
        for (int i = size() - 1; i >= 0; i--)
            result.add(get(i));
        pairs = result;
        return this;
    }

    public boolean contains(Object o) {
        return values().contains(o);
    }

    public Iterator> iterator() {
        return pairs.iterator();
    }

    public Object[] toArray() {
        return pairs.toArray();
    }

    public  T[] toArray(T[] a) {
        return pairs.toArray(a);
    }

    public boolean add(Pair kv) {
        return pairs.add(kv);
    }
/*
    public V remove(Object key) {
        boolean isRemoved = false;
        for (Object kv : pairs)
            if (kv.equals(key)) {
                V value = ((Pair)kv).value;
                pairs.remove(kv);
                return value;
            }
        return null;
    }*/

    public boolean remove(Object o) {
        boolean isRemoved = false;
        for (Object kv : pairs)
            if (kv.equals(o)) {
                pairs.remove(kv);
                isRemoved = true;
            }
        return isRemoved;
    }

    public void removeByKey(K key) {
        pairs.remove(LinqUtils.firstIndex(pairs, pair -> pair.key.equals(key)));
    }

    public void removeAllValues(V value) {
        LinqUtils.where(pairs, p -> p.value.equals(value)).forEach(pairs::remove);
    }

    public boolean containsAll(Collection c) {
        for (Object o : c)
            if (!contains(o))
                return false;
        return true;
    }

    public boolean addAll(Collection> c) {
        for (Pair pair : c)
            add(pair);
        return true;
    }

    public boolean removeAll(Collection c) {
        for (Object o : c)
            if (!remove(o))
                return false;
        return true;
    }

    public boolean retainAll(Collection c) {
        for (Pair pair : pairs)
            if (!c.contains(pair))
                if (!remove(pair))
                    return false;
        return true;
    }

    public void clear() {
        pairs.clear();
    }

    @Override
    public String toString() {
        return print(LinqUtils.select(pairs, pair -> pair.key + ":" + pair.value));
    }

    @Override
    public MapArray clone() {
        return new MapArray<>(this);
    }

    public MapArray copy() {
        return clone();
    }

    public  List select(JFuncTTREx func) {
        try {
            List result = new ArrayList<>();
            for (Pair pair : pairs)
                result.add(func.invoke(pair.key, pair.value));
            return result;
        } catch (Exception ignore) {
            throwRuntimeException(ignore);
            return new ArrayList<>();
        }
    }

    public MapArray filter(JFuncTTREx func) {
        return where(func);
    }
    public MapArray where(JFuncTTREx func) {
        try {
            MapArray result = new MapArray<>();
            for (Pair pair : pairs)
                if (func.invoke(pair.key, pair.value))
                    result.add(pair);
            return result;
        } catch (Exception ignore) {
            throwRuntimeException(ignore);
            return null;
        }
    }

    public V first(JFuncTTREx func) {
        try {
            for (Pair pair : pairs)
                if (func.invoke(pair.key, pair.value))
                    return pair.value;
            return null;
        } catch (Exception ignore) {
            throwRuntimeException(ignore);
            return null;
        }
    }

    public void foreach(JActionTTEx action) {
        try {
            for (Pair pair : pairs)
                action.invoke(pair.key, pair.value);
        } catch (Exception ignore) {
            throwRuntimeException(ignore);
        }
    }

    public  List selectMany(JFuncTTREx> func) {
        try {
            List result = new ArrayList<>();
            for (Pair pair : pairs)
                result.addAll(func.invoke(pair.key, pair.value));
            return result;
        } catch (Exception ignore) {
            throwRuntimeException(ignore);
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy