Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
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.pairs.Pair;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
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, Function key, Function value) {
this();
for (T t : collection)
add(key.apply(t), value.apply(t));
}
public MapArray(Collection collection, Function value) {
this();
for (K k : collection)
add(k, value.apply(k));
}
public MapArray(T[] array, Function key, Function value) {
this();
for (T t : array)
add(key.apply(t), value.apply(t));
}
public MapArray(K[] array, Function value) {
this();
for (K k : array)
add(k, value.apply(k));
}
public MapArray(int count, Function key, Function value) {
this();
for (int i = 0; i < count; i++)
add(key.apply(i), value.apply(i));
}
public MapArray(int count, Function> pairFunc) {
this();
for (int i = 0; i < count; i++)
add(pairFunc.apply(i));
}
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(
BiFunction key, BiFunction value) {
MapArray result = new MapArray<>();
for (Pair pair : pairs)
result.add(key.apply(pair.key, pair.value), value.apply(pair.key, pair.value));
return result;
}
public MapArray toMapArray(Function value) {
MapArray result = new MapArray<>();
for (Pair pair : pairs)
result.add(pair.key, value.apply(pair.value));
return result;
}
public Map toMap() {
return toMap(v -> v);
}
public Map toMap(Function value) {
return toMap((k, v) -> k, (k,v) -> value.apply(v));
}
public Map toMap(
BiFunction key, BiFunction value) {
Map result = new HashMap<>();
for (Pair pair : pairs)
result.put(key.apply(pair.key, pair.value), value.apply(pair.key, pair.value));
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, Function func) {
V value = null;
if (hasKey(key)) {
value = get(key);
removeByKey(key);
}
pairs.add(new Pair<>(key, func.apply(value)));
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(Function 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 extends Pair> 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(BiFunction func) {
try {
return pairs.stream()
.map(pair -> func.apply(pair.key, pair.value))
.collect(toList());
} catch (Exception ignore) {
throwRuntimeException(ignore);
return new ArrayList<>();
}
}
public MapArray filter(BiFunction func) {
return where(func);
}
public MapArray where(BiFunction func) {
try {
return pairs.stream().filter(pair -> func.apply(pair.key, pair.value))
.collect(Collectors.toCollection(MapArray::new));
} catch (Exception ignore) {
throwRuntimeException(ignore);
return null;
}
}
public V first(BiFunction func) {
try {
for (Pair pair : pairs)
if (func.apply(pair.key, pair.value))
return pair.value;
return null;
} catch (Exception ignore) {
throwRuntimeException(ignore);
return null;
}
}
public void foreach(BiConsumer action) {
try {
for (Pair pair : pairs)
action.accept(pair.key, pair.value);
} catch (Exception ignore) {
throwRuntimeException(ignore);
}
}
public List selectMany(BiFunction> func) {
try {
List result = new ArrayList<>();
for (Pair pair : pairs)
result.addAll(func.apply(pair.key, pair.value));
return result;
} catch (Exception ignore) {
throwRuntimeException(ignore);
return null;
}
}
}