com.aol.cyclops.vavr.hkt.HashSetKind 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 com.aol.cyclops.vavr.hkt;
import com.aol.cyclops2.hkt.Higher;
import cyclops.companion.vavr.HashSets;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.hashSet;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import java.util.function.Function;
/**
* Simulates Higher Kinded Types for Vavr Set's
*
* HashSetKind is a Set and a Higher Kinded Type (HashSetKind.µ,T)
*
* @author johnmcclean
*
* @param Data type stored within the Set
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class HashSetKind implements Higher, Publisher, Set {
public static Higher widenK(final HashSet completableList) {
return new HashSetKind<>(
completableList);
}
public Active allTypeclasses(){
return Active.of(this, HashSets.Instances.definitions());
}
public Nested mapM(Function super T, ? extends Higher> fn, InstanceDefinitions defs){
return HashSets.mapM(boxed,fn,defs);
}
public HashSetKind fold(Function super Set super T>, ? extends HashSet> op){
return widen(op.apply(this));
}
/**
* Construct a HKT encoded completed Set
*
* @param value To encode inside a HKT encoded Set
* @return Completed HKT encoded FSet
*/
public static HashSetKind just(T value) {
return widen(HashSet.of(value));
}
public static HashSetKind just(T... values) {
return widen(HashSet.of(values));
}
public static HashSetKind empty() {
return widen(HashSet.empty());
}
/**
* Convert a Set to a simulated HigherKindedType that captures Set nature
* and Set element data type separately. Recover via @see HashSetKind#narrow
*
* If the supplied Set implements HashSetKind it is returned already, otherwise it
* is wrapped into a Set implementation that does implement HashSetKind
*
* @param completableSet Set to widen to a HashSetKind
* @return HashSetKind encoding HKT info about HashSets
*/
public static HashSetKind widen(final HashSet completableSet) {
return new HashSetKind<>(
completableSet);
}
/**
* Widen a HashSetKind nested inside another HKT encoded type
*
* @param flux HTK encoded type containing a Set to widen
* @return HKT encoded type with a widened Set
*/
public static Higher> widen2(Higher> flux) {
// a functor could be used (if C2 is a functor / one exists for C2 type)
// instead of casting
// cast seems safer as Higher must be a HashSetKind
return (Higher) flux;
}
public static HashSetKind widen(final Publisher completableSet) {
return new HashSetKind<>(
HashSet.ofAll((Iterable)ReactiveSeq.fromPublisher(completableSet)));
}
/**
* Convert the raw Higher Kinded Type for HashSetKind types into the HashSetKind type definition class
*
* @param future HKT encoded hashSet into a HashSetKind
* @return HashSetKind
*/
public static HashSetKind narrowK(final Higher future) {
return (HashSetKind) future;
}
/**
* Convert the HigherKindedType definition for a Set into
*
* @param set Type Constructor to convert back into narrowed type
* @return Set from Higher Kinded Type
*/
public static HashSet narrow(final Higher set) {
return ((HashSetKind) set).narrow();
}
@Delegate
private final HashSet boxed;
public ReactiveSeq toReactiveSeq(){
return ReactiveSeq.fromIterable(boxed);
}
/**
* @return wrapped Set
*/
public HashSet narrow() {
return boxed;
}
@Override
public void subscribe(Subscriber super T> s) {
ReactiveSeq.fromIterable(boxed)
.subscribe(s);
}
/**
* @param o
* @return
* @see io.vavr.Value#equals(Object)
*/
public boolean equals(Object o) {
return boxed.equals(o);
}
/**
* @return
* @see io.vavr.Value#hashCode()
*/
public int hashCode() {
return boxed.hashCode();
}
/**
* @return
* @see io.vavr.Value#toString()
*/
public String toString() {
return boxed.toString();
}
}