cyclops.collections.vavr.VavrTreeMapX Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-vavr Show documentation
Show all versions of cyclops-vavr Show documentation
Converters and Comprehenders for Javaslang
The newest version!
package cyclops.collections.vavr;
import java.util.*;
import com.aol.cyclops2.data.collections.extensions.ExtensiblePMapX;
import com.aol.cyclops2.types.Unwrapable;
import com.aol.cyclops2.types.mixins.TupleWrapper;
import cyclops.collections.immutable.PersistentMapX;
import cyclops.control.Eval;
import cyclops.function.Reducer;
import cyclops.stream.ReactiveSeq;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PMap;
import io.vavr.collection.TreeMap;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.experimental.Wither;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrTreeMapX extends AbstractMap implements PMap, Unwrapable{
@Wither
TreeMap map;
@Override
public R unwrap() {
return (R)map;
}
public static PersistentMapX copyFromMap(Map map,Comparator super K> c){
return VavrTreeMapX.empty(c)
.plusAll(map);
}
public static , V> Reducer> toPersistentMapX() {
return Reducer.> of(empty(), (final PersistentMapX a) -> b -> a.plusAll(b), (in) -> {
final List w = ((TupleWrapper) () -> in).values();
return singleton((K) w.get(0), (V) w.get(1));
});
}
public static Reducer> toPersistentMapX(Comparator super K> c) {
return Reducer.> of(empty(c), (final PersistentMapX a) -> b -> a.plusAll(b), (in) -> {
final List w = ((TupleWrapper) () -> in).values();
return singleton((K) w.get(0), (V) w.get(1),c);
});
}
public static VavrTreeMapX fromMap(@NonNull TreeMap map){
return new VavrTreeMapX<>(map);
}
public static VavrTreeMapX ofAll(@NonNull TreeMap map){
return new VavrTreeMapX<>(map);
}
public static ,V> VavrTreeMapX fromJavaMap(Map extends K,? extends V> map){
TreeMap res = TreeMap.ofAll((Map)map);
return fromMap(res);
}
public static PersistentMapX empty(Comparator super K> c){
return new ExtensiblePMapX(fromMap(TreeMap.empty(c)), Eval.later(()->toPersistentMapX(c)));
}
public static ,V> PersistentMapX empty(){
return new ExtensiblePMapX(fromMap(TreeMap.empty()), Eval.later(()->toPersistentMapX()));
}
public static ,V> PMap singletonPMap(K key,V value){
TreeMap map = TreeMap.of(key, value);
return fromMap(map);
}
public static ,V> PersistentMapX singleton(K key,V value){
TreeMap map = TreeMap.of(key, value);
return new ExtensiblePMapX(fromMap(map),Eval.later(()-> VavrTreeMapX.toPersistentMapX()));
}
public static PersistentMapX singleton(K key,V value, Comparator super K> c){
TreeMap map = TreeMap.of(c,key, value);
return new ExtensiblePMapX(fromMap(map),Eval.later(()-> VavrTreeMapX.toPersistentMapX(c)));
}
public static ,V> PersistentMapX fromStream(@NonNull ReactiveSeq> stream){
return stream.mapReduce(toPersistentMapX());
}
@Override
public PMap plus(K key, V value) {
return withMap(map.put(key, value));
}
@Override
public PMap plusAll(java.util.Map extends K, ? extends V> m2) {
TreeMap m = map;
for(Map.Entry extends K, ? extends V> next : m2.entrySet()){
m = m.put(next.getKey(), next.getValue());
}
return withMap(m);
}
@Override
public PMap minus(Object key) {
return withMap(map.remove((K)key));
}
@Override
public PMap minusAll(Collection> keys) {
return withMap(map.removeAll((Iterable)keys));
}
@Override
public Set> entrySet() {
return map.toJavaMap().entrySet();
}
/* (non-Javadoc)
* @see java.util.AbstractMap#get(java.lang.Object)
*/
@Override
public V get(Object key) {
return (V)map.get((K)key);
}
}