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

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

The newest version!
package cyclops.collections.vavr;


import java.util.*;
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.LazyPSetX;
import com.aol.cyclops2.types.Unwrapable;
import com.aol.cyclops2.types.foldable.Evaluation;
import cyclops.collections.immutable.LinkedListX;
import cyclops.collections.immutable.OrderedSetX;
import cyclops.collections.immutable.PersistentSetX;
import cyclops.function.Reducer;
import cyclops.stream.ReactiveSeq;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import org.jooq.lambda.tuple.Tuple2;
import org.pcollections.PSet;



import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.Wither;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class VavrHashSetX extends AbstractSet implements PSet, Unwrapable {

    public static  PersistentSetX listX(ReactiveSeq stream){
        return fromStream(stream);
    }
    public static  PersistentSetX copyFromCollection(CollectionX vec) {
        PersistentSetX res = VavrHashSetX.empty()
                .plusAll(vec);
        return res;
    }
    @Override
    public  R unwrap() {
        return (R)set;
    }
    /**
     * Create a LazyPSetX from a Stream
     * 
     * @param stream to construct a LazyQueueX from
     * @return LazyPSetX
     */
    public static  LazyPSetX fromStream(Stream stream) {
        return new LazyPSetX(null, ReactiveSeq.fromStream(stream),toPSet(), Evaluation.LAZY);
    }

    /**
     * Create a LazyPSetX 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 LazyPSetX range(int start, int end) {
        return fromStream(ReactiveSeq.range(start, end));
    }

    /**
     * Create a LazyPSetX 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 LazyPSetX rangeLong(long start, long end) {
        return fromStream(ReactiveSeq.rangeLong(start, end));
    }

    /**
     * Unfold a function into a ListX
     * 
     * 
     * {@code 
     *  LazyPSetX.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 LazyPSetX unfold(U seed, Function>> unfolder) { return fromStream(ReactiveSeq.unfold(seed, unfolder)); } /** * Generate a LazyPSetX 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 LazyPSetX generate(long limit, Supplier s) { return fromStream(ReactiveSeq.generate(s) .limit(limit)); } /** * Create a LazyPSetX 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 LazyPSetX iterate(long limit, final T seed, final UnaryOperator f) { return fromStream(ReactiveSeq.iterate(seed, f) .limit(limit)); } /** *
     * {@code 
     * PSet q = VavrHashSetX.toPSet()
                                      .mapReduce(Stream.of(1,2,3,4));
     * 
     * }
     * 
* @return Reducer for PSet */ public static Reducer> toPSet() { return Reducer.> of(VavrHashSetX.emptyPSet(), (final PSet a) -> b -> a.plusAll(b), (final T x) -> VavrHashSetX.singleton(x)); } public static LazyPSetX PSet(Set q) { return fromPSet(new VavrHashSetX<>(q), toPSet()); } public static VavrHashSetX emptyPSet(){ return new VavrHashSetX<>(HashSet.empty()); } public static LazyPSetX empty(){ return fromPSet( new VavrHashSetX<>(HashSet.empty()), toPSet()); } private static LazyPSetX fromPSet(PSet ts, Reducer> pSetReducer) { return new LazyPSetX(ts,null,pSetReducer, Evaluation.LAZY); } public static LazyPSetX singleton(T t){ return fromPSet(new VavrHashSetX<>(HashSet.of(t)), toPSet()); } public static LazyPSetX of(T... t){ return fromPSet( new VavrHashSetX<>(HashSet.of(t)), toPSet()); } public static LazyPSetX ofAll(Set q) { return fromPSet(new VavrHashSetX<>(q), toPSet()); } @SafeVarargs public static LazyPSetX PSet(T... elements){ return of(elements); } @Wither private final Set set; @Override public PSet plus(T e) { return withSet(set.add(e)); } @Override public PSet plusAll(Collection l) { return withSet(set.addAll(l)); } @Override public PSet minus(Object e) { return withSet(set.remove((T)e)); } @Override public PSet minusAll(Collection l) { return withSet(set.removeAll((Collection)l)); } @Override public int size() { return set.size(); } @Override public Iterator iterator() { return set.iterator(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy