com.aol.cyclops.vavr.hkt.QueueKind 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 java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import com.aol.cyclops2.hkt.Higher;
import cyclops.companion.vavr.Lists;
import cyclops.companion.vavr.Queues;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.queue;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.collection.Array;
import io.vavr.collection.LinearSeq;
import io.vavr.collection.Queue;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;
/**
* Simulates Higher Kinded Types for Queue's
*
* QueueKind is a Queue and a Higher Kinded Type (queue,T)
*
* @author johnmcclean
*
* @param Data type stored within the Queue
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class QueueKind implements Higher {
public static Higher widenK(final Queue completableList) {
return new QueueKind<>(
completableList);
}
public Active allTypeclasses(){
return Active.of(this, Queues.Instances.definitions());
}
public Nested mapM(Function super T,? extends Higher> fn, InstanceDefinitions defs){
return Queues.mapM(boxed,fn,defs);
}
public QueueKind fold(Function super Queue super T>,? extends Queue> op){
return widen(op.apply(boxed));
}
public static QueueKind of(T element) {
return widen(Queue.of(element));
}
@SafeVarargs
public static QueueKind of(T... elements) {
return widen(Queue.of(elements));
}
/**
* Convert a Queue to a simulated HigherKindedType that captures Queue nature
* and Queue element data type separately. Recover via @see QueueKind#narrow
*
* If the supplied Queue implements QueueKind it is returned already, otherwise it
* is wrapped into a Queue implementation that does implement QueueKind
*
* @param list Queue to widen to a QueueKind
* @return QueueKind encoding HKT info about Queues
*/
public static QueueKind widen(final Queue list) {
return new QueueKind<>(list);
}
/**
* Widen a QueueKind nested inside another HKT encoded type
*
* @param list HTK encoded type containing a Queue to widen
* @return HKT encoded type with a widened Queue
*/
public static Higher> widen2(Higher> list){
//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 QueueKind
return (Higher)list;
}
/**
* Convert the raw Higher Kinded Type for Queue types into the QueueKind type definition class
*
* @param list HKT encoded list into a QueueKind
* @return QueueKind
*/
public static QueueKind narrowK(final Higher list) {
return (QueueKind)list;
}
/**
* Convert the HigherKindedType definition for a Queue into
*
* @param list Type Constructor to convert back into narrowed type
* @return QueueX from Higher Kinded Type
*/
public static Queue narrow(final Higher list) {
return ((QueueKind)list).narrow();
}
@Delegate
private final Queue boxed;
/**
* @return This back as a QueueX
*/
public Queue narrow() {
return (Queue) (boxed);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return boxed.hashCode();
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
return boxed.equals(obj);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "QueueKind [" + boxed + "]";
}
}