/** * Create a stream from a map * * @param it Iterator to convert to a Stream * @return Stream from a map */ public static <K,V> Stream<Map.Entry<K, V>> stream(Map<K,V> it){ return StreamUtils.stream(it); } /**
/** * Create a stream from an iterable * * @param it Iterable to convert to a Stream * @return Stream from iterable */ public static <U> Stream<U> stream(Iterable<U> it){ return StreamUtils.stream(it); } /**
/** * Create a stream from an iterator * * @param it Iterator to convert to a Stream * @return Stream from iterator */ public static <U> Stream<U> stream(Iterator<U> it){ return StreamUtils.stream(it); }
public Stream<T> limitLast(){ Iterator<T> it = stream.iterator(); return StreamUtils.stream(new Iterator<T>(){ @Override public boolean hasNext() { while(it.hasNext()){ buffer.add(it.next()); if(buffer.size()>limit) buffer.pop(); } return buffer.size()>0; } @Override public T next() { return buffer.pop(); } }); } }
static <T> T unwrapOtherMonadTypes(Comprehender<T> comp,Object apply){ if(apply instanceof LazySeq){ return (T)StreamUtils.stream(((LazySeq)apply).iterator()); } if(apply instanceof Iterable){ return (T)StreamUtils.stream(((Iterable)apply)); } if(apply instanceof Collection){ return (T)((Collection)apply).stream(); } return Comprehender.unwrapOtherMonadTypes(comp,apply); }
/** * Construct a Sequence from an Iterable * @param iterable to construct Sequence from * @return SequenceM */ public static <T> SequenceM<T> fromIterable(Iterable<T> iterable){ return new SequenceM(StreamUtils.stream(iterable)); } @Override
public List zero(){ return StreamUtils.stream(reducers).map(r->r.zero()).collect(Collectors.toList()); } public BiFunction<List,List,List> combiner(){
public List zero(){ return stream(reducers).map(r->r.zero()).collect(Collectors.toList()); } public BiFunction<List,List,List> combiner(){
public static Stream<Object> stream(Object t){ if(t instanceof Iterable){ return Stream.concat(StreamUtils.stream((Iterable)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Stream){ return Stream.concat( ((Stream)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Iterator){ return Stream.concat( StreamUtils.stream((Iterator)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Map){ return Stream.concat(StreamUtils.stream((Map)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } return Stream.concat(Stream.of(t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); }
public static <T> SortedSetX<T> fromIterable(Collector<T,?,SortedSet<T>> collector,Iterable<T> it){ if(it instanceof SortedSetX) return (SortedSetX)it; if(it instanceof SortedSet) return new SortedSetXImpl<T>( (SortedSet)it, collector); return new SortedSetXImpl<T>(StreamUtils.stream(it).collect(collector),collector); }
public static <T> DequeX<T> fromIterable(Collector<T,?,Deque<T>> collector,Iterable<T> it){ if(it instanceof DequeX) return (DequeX)it; if(it instanceof Deque) return new DequeXImpl<T>( (Deque)it, collector); return new DequeXImpl<T>(StreamUtils.stream(it).collect(collector),collector); }
/** * <pre> * {@code * Stream<String> helloWorld = Stream.of(); Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld); assertTrue(!headAndTail.isPresent()); * } * </pre> * @param stream to extract head and tail from * @return */ public final static <T> Optional<HeadAndTail<T>> headAndTailOptional(Stream<T> stream){ Iterator<T> it = stream.iterator(); if(!it.hasNext()) return Optional.empty(); return Optional.of(new HeadAndTail(it.next(),sequenceM(stream(it),Optional.empty()))); }
public static <T> QueueX<T> fromIterable(Collector<T,?,Queue<T>> collector,Iterable<T> it){ if(it instanceof QueueX) return (QueueX)it; if(it instanceof Deque) return new QueueXImpl<T>( (Queue)it, collector); return new QueueXImpl<T>(StreamUtils.stream(it).collect(collector),collector); }
public static <T> SetX<T> fromIterable(Collector<T,?,Set<T>> collector,Iterable<T> it){ if(it instanceof SetX) return (SetX)it; if(it instanceof Set) return new SetXImpl<T>( (Set)it, collector); return new SetXImpl<T>(StreamUtils.stream(it).collect(collector),collector); } @Override
public static <T> ListX<T> fromIterable(Collector<T,?,List<T>> collector,Iterable<T> it){ if(it instanceof ListX) return (ListX)it; if(it instanceof List) return new ListXImpl<T>( (List)it, collector); return new ListXImpl<T>(StreamUtils.stream(it).collect(collector),collector); } @Override
/** * flatMap operation * * <pre> * {@code * assertThat(anyM(Stream.of(1,2,3)).asSequence().flatMapStream(i->Stream.of(i)).toList(),equalTo(Arrays.asList(1,2,3))); * } * </pre> * * @param fn to be applied * @return new stage in Sequence with flatMap operation to be lazily applied */ public final <R> SequenceM<R> flatMapStream(Function<? super T,BaseStream<? extends R,?>> fn) { return new SequenceM<>(monad.flatMap( in->StreamUtils.stream(fn.apply(in).iterator()))); } /**
/** * <pre> * {@code * Stream<String> helloWorld = Stream.of(); Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld); assertTrue(!headAndTail.isPresent()); * } * </pre> * @param stream to extract head and tail from * @return */ public final static <T> Optional<HeadAndTail<T>> headAndTailOptional(Stream<T> stream){ Iterator<T> it = stream.iterator(); if(!it.hasNext()) return Optional.empty(); return Optional.of(new HeadAndTail(it.next(),AsAnyM.anyM(stream(it)).asSequence())); }
/** * Duplicate a Stream, buffers intermediate values, leaders may change positions so a limit * can be safely applied to the leading stream. Not thread-safe. * <pre> * {@code * Tuple2<SequenceM<Integer>, SequenceM<Integer>> copies =of(1,2,3,4,5,6).duplicate(); assertTrue(copies.v1.anyMatch(i->i==2)); assertTrue(copies.v2.anyMatch(i->i==2)); * * } * </pre> * * @return duplicated stream */ public final static <T> Tuple2<Stream<T>,Stream<T>> duplicate(Stream<T> stream){ Tuple2<Iterator<T>,Iterator<T>> Tuple2 = StreamUtils.toBufferingDuplicator(stream.iterator()); return new Tuple2(StreamUtils.stream(Tuple2.v1()),StreamUtils.stream(Tuple2.v2())); } private final static <T> Tuple2<Stream<T>,Stream<T>> duplicatePos(Stream<T> stream,int pos){