All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cyclops.collections.vavr.VavrQueueX Maven / Gradle / Ivy

The newest version!
package cyclops.collections.vavr;

import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import com.aol.cyclops2.data.collections.extensions.CollectionX;
import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyPQueueX;
import com.aol.cyclops2.types.Unwrapable;
import com.aol.cyclops2.types.foldable.Evaluation;
import cyclops.collections.immutable.PersistentQueueX;
import cyclops.collections.immutable.VectorX;
import cyclops.collections.mutable.QueueX;
import cyclops.function.Reducer;
import cyclops.stream.ReactiveSeq;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PQueue;


import io.vavr.collection.Queue;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Wither;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrQueueX extends AbstractQueue implements PQueue, Unwrapable {
    public static  PersistentQueueX queueX(ReactiveSeq stream){
        return fromStream(stream);
    }
    @Override
    public  R unwrap() {
        return (R)this.list;
    }

    public static  PersistentQueueX copyFromCollection(CollectionX vec) {

        return VavrQueueX.empty()
                .plusAll(vec);

    }
    public static  PersistentQueueX from(Queue q) {
        return fromPQueue(new VavrQueueX<>(q), toPQueue());
    }
    /**
     * Create a LazyPQueueX from a Stream
     * 
     * @param stream to construct a LazyQueueX from
     * @return LazyPQueueX
     */
    public static  LazyPQueueX fromStream(Stream stream) {
        return new LazyPQueueX(null, ReactiveSeq.fromStream(stream),toPQueue(), Evaluation.LAZY);
    }

    /**
     * Create a LazyPQueueX that contains the Integers between start and end
     * 
     * @param start
     *            Number of range to start from
     * @param end
     *            Number for range to end at
     * @return Range ListX
     */
    public static LazyPQueueX range(int start, int end) {
        return fromStream(ReactiveSeq.range(start, end));
    }

    /**
     * Create a LazyPQueueX that contains the Longs between start and end
     * 
     * @param start
     *            Number of range to start from
     * @param end
     *            Number for range to end at
     * @return Range ListX
     */
    public static LazyPQueueX rangeLong(long start, long end) {
        return fromStream(ReactiveSeq.rangeLong(start, end));
    }

    /**
     * Unfold a function into a ListX
     * 
     * 
     * {@code 
     *  LazyPQueueX.unfold(1,i->i<=6 ? Optional.of(Tuple.tuple(i,i+1)) : Optional.empty());
     * 
     * //(1,2,3,4,5)
     * 
     * }
* * @param seed Initial value * @param unfolder Iteratively applied function, terminated by an empty Optional * @return ListX generated by unfolder function */ public static LazyPQueueX unfold(U seed, Function>> unfolder) { return fromStream(ReactiveSeq.unfold(seed, unfolder)); } /** * Generate a LazyPQueueX from the provided Supplier up to the provided limit number of times * * @param limit Max number of elements to generate * @param s Supplier to generate ListX elements * @return ListX generated from the provided Supplier */ public static LazyPQueueX generate(long limit, Supplier s) { return fromStream(ReactiveSeq.generate(s) .limit(limit)); } /** * Create a LazyPQueueX by iterative application of a function to an initial element up to the supplied limit number of times * * @param limit Max number of elements to generate * @param seed Initial element * @param f Iteratively applied to each element to generate the next element * @return ListX generated by iterative application */ public static LazyPQueueX iterate(long limit, final T seed, final UnaryOperator f) { return fromStream(ReactiveSeq.iterate(seed, f) .limit(limit)); } /** *
     * {@code 
     * PQueue q = VavrQueueX.toPQueue()
                                          .mapReduce(Stream.of(1,2,3,4));
     * 
     * }
     * 
* @return Reducer for PQueue */ public static Reducer> toPQueue() { return Reducer.> of(VavrQueueX.emptyPQueue(), (final PQueue a) -> b -> a.plusAll(b), (final T x) -> VavrQueueX.singleton(x)); } public static VavrQueueX emptyPQueue(){ return new VavrQueueX<>(Queue.empty()); } public static LazyPQueueX empty(){ return fromPQueue(new VavrQueueX<>(Queue.empty()),toPQueue()); } private static LazyPQueueX fromPQueue(PQueue ts, Reducer> pQueueReducer) { return new LazyPQueueX(ts,null,pQueueReducer, Evaluation.LAZY); } public static LazyPQueueX singleton(T t){ return fromPQueue(new VavrQueueX<>(Queue.of(t)),toPQueue()); } public static LazyPQueueX of(T... t){ return fromPQueue(new VavrQueueX<>(Queue.of(t)),toPQueue()); } public static LazyPQueueX ofAll(T... t){ return fromPQueue(new VavrQueueX<>(Queue.of(t)),toPQueue()); } public static LazyPQueueX PQueue(Queue q) { return fromPQueue(new VavrQueueX<>(q), toPQueue()); } @SafeVarargs public static LazyPQueueX PQueue(T... elements){ return fromPQueue(of(elements),toPQueue()); } @Wither private final Queue list; @Override public PQueue plus(T e) { return withList(list.prepend(e)); } @Override public PQueue plusAll(Collection l) { return withList(list.prependAll(l)); } @Override public PQueue minus(Object e) { return withList(list.remove((T)e)); } @Override public PQueue minusAll(Collection l) { return withList(list.removeAll((Collection)l)); } @Override public int size() { return list.size(); } @Override public T peek() { return list.peek(); } @Override public org.pcollections.PQueue minus() { return withList(list.drop(1)); } @Override public boolean offer(T o) { throw new UnsupportedOperationException(); } @Override public T poll() { return list.get(); } @Override public Iterator iterator() { return list.iterator(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy