cyclops.collections.vavr.VavrHashMapX 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.AbstractMap;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 io.vavr.collection.HashMap;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PMap;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Wither;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrHashMapX extends AbstractMap implements PMap, Unwrapable{
public static PersistentMapX copyFromMap(Map map){
return VavrHashMapX.empty()
.plusAll(map);
}
@Override
public R unwrap() {
return (R)map;
}
@Wither
HashMap map;
public static 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 VavrHashMapX fromMap(HashMap map){
return new VavrHashMapX<>(map);
}
public static VavrHashMapX ofAll(HashMap map){
return new VavrHashMapX<>(map);
}
public static VavrHashMapX fromJavaMap(Map map){
HashMap res = HashMap.ofAll(map);
return fromMap(res);
}
public static PersistentMapX empty(){
return new ExtensiblePMapX(fromMap(HashMap.empty()), Eval.later(()->toPersistentMapX()));
}
public static PMap singletonPMap(K key,V value){
HashMap map = HashMap.of(key, value);
return fromMap(map);
}
public static PersistentMapX singleton(K key,V value){
HashMap map = HashMap.of(key, value);
return new ExtensiblePMapX(fromMap(map),Eval.later(()-> VavrHashMapX.toPersistentMapX()));
}
public static PersistentMapX fromStream(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) {
HashMap 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);
}
}