cyclops.data.MultiMapHK Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-pure Show documentation
Show all versions of cyclops-pure Show documentation
Platform for Functional Reactive Programming with Java 8
The newest version!
package cyclops.data;
import com.oath.cyclops.hkt.DataWitness.multiMapHK;
import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.hkt.Higher2;
import cyclops.control.Option;
import cyclops.data.tuple.Tuple2;
import cyclops.typeclasses.Pure;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import java.util.function.Function;
/*
Higher kinded multimap
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class MultiMapHK implements Higher2 {
private final HashMap> multiMap;
private final Appender appender;
private final Pure pure;
public static MultiMapHK empty(Appender appender, Pure pure){
return new MultiMapHK<>(HashMap.empty(),appender,pure);
}
public MultiMapHK put(K key, V value) {
Higher hkt = multiMap.get(key).map(v->appender.append(v,value)).orElseGet(()->pure.unit(value));
return new MultiMapHK<>(multiMap.put(key,hkt),appender,pure);
}
public Option get(K key, Function,? extends R> decoder){
return multiMap.get(key).map(decoder);
}
public Option> get(K key){
return multiMap.get(key);
}
public boolean containsKey(K key){
return multiMap.containsKey(key);
}
public boolean contains(Tuple2> keyAndValue){
return multiMap.contains(keyAndValue);
}
@FunctionalInterface
public interface Appender{
public Higher append(Higher container, V value);
}
}