Skip to content

Commit

Permalink
more tests & javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Sep 11, 2015
1 parent 2e64267 commit f23bbf1
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
Expand All @@ -36,6 +37,7 @@
import java.util.function.UnaryOperator;
import java.util.stream.BaseStream;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Expand Down Expand Up @@ -1666,26 +1668,114 @@ <U> U reduce(U identity,
* @return SequenceM that returns all elements until time period has elapsed
*/
SequenceM<T> limit(long time, final TimeUnit unit);
/**
* assertThat(SequenceM.of(1,2,3,4,5)
.skipLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList(1,2,3)));
*
* @param num
* @return
*/
SequenceM<T> skipLast(int num);
/**
* Limit results to the last x elements in a SequenceM
* <pre>
* {@code
* assertThat(SequenceM.of(1,2,3,4,5)
.limitLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList(4,5)));
*
* }
*
* @param num of elements to return (last elements)
* @return SequenceM limited to last num elements
*/
SequenceM<T> limitLast(int num);

/**
* Turns this SequenceM into a HotStream, a connectable Stream, being executed on a thread on the
* supplied executor, that is producing data
* <pre>
* {@code
* HotStream<Integer> ints = SequenceM.range(0,Integer.MAX_VALUE)
.hotStream(exec)
ints.connect().forEach(System.out::println);
* //print out all the ints
* //multiple consumers are possible, so other Streams can connect on different Threads
*
* }
* </pre>
* @param e Executor to execute this SequenceM on
* @return a Connectable HotStream
*/
HotStream<T> hotStream(Executor e);

/**
* <pre>
* {@code
* assertThat(SequenceM.of(1,2,3,4)
.map(u->{throw new RuntimeException();})
.recover(e->"hello")
.firstValue(),equalTo("hello"));
* }
* </pre>
* @return first value in this Stream
*/
T firstValue();

/**
* <pre>
* {@code
* assertThat(SequenceM.of(1).single(),equalTo(1));
* }
* </pre>
*
* @return a single value or an exception if 0/1 values in this Stream
*/
default T single(){
List<T> l= toList();
if(l.size()==1){
return l.get(l.size()-1);
}
Iterator<T> it = iterator();
if(it.hasNext()){
T result = it.next();
if(!it.hasNext())
return result;
}

throw new UnsupportedOperationException("single only works for Streams with a single value");

}

/**
* Return the elementAt index or Optional.empty
* <pre>
* {@code
* assertThat(SequenceM.of(1,2,3,4,5).elementAt(2).get(),equalTo(3));
* }
* </pre>
* @param index to extract element from
* @return elementAt index
*/
default Optional<T> elementAt(long index){
return this.zipWithIndex()
.filter(t->t.v2==index)
.findFirst()
.map(t->t.v1());
}
/**
* Gets the element at index, and returns a Tuple containing the element (it must be present)
* and a lazy copy of the Sequence for further processing.
*
* <pre>
* {@code
* SequenceM.of(1,2,3,4,5).get(2).v1
* //3
* }
* </pre>
*
* @param index to extract element from
* @return Element and Sequence
*/
default Tuple2<T,SequenceM<T>> get(long index){
Tuple2<SequenceM<T>, SequenceM<T>> tuple = this.duplicateSequence();
return tuple.map1(s->s.zipWithIndex()
Expand All @@ -1694,14 +1784,41 @@ default Tuple2<T,SequenceM<T>> get(long index){
.map(t->t.v1()).get());
}

/**
* <pre>
* {@code
* SequenceM.of(1,2,3,4,5)
.elapsed()
.forEach(System.out::println);
* }
* </pre>
*
* @return Sequence that adds the time between elements in millis to each element
*/
default SequenceM<Tuple2<T,Long>> elapsed(){
long[] last = {System.currentTimeMillis()};
return zip(SequenceM.generate( ()->{long now =System.currentTimeMillis();
long result = last[0]- now;
last[0]=now;
AtomicLong last = new AtomicLong(System.currentTimeMillis());

return zip(SequenceM.generate( ()->{
long now =System.currentTimeMillis();

long result = now-last.get();
last.set(now);
return result;
} ));
}
/**
* <pre>
* {@code
* SequenceM.of(1,2,3,4,5)
.timestamp()
.forEach(System.out::println)
*
* }
*
* </pre>
*
* @return Sequence that adds a timestamp to each element
*/
default SequenceM<Tuple2<T,Long>> timestamp(){
return zip(SequenceM.generate( ()->System.currentTimeMillis()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean hasNext() {

@Override
public T next() {
if(finished)
if(finished && buffer.size()==0)
throw new NoSuchElementException();
return buffer.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -25,6 +27,70 @@
import com.aol.cyclops.sequence.streamable.Streamable;

public class SequenceMTest {
@Test
public void elapsedIsPositive(){


assertTrue(SequenceM.of(1,2,3,4,5).elapsed().noneMatch(t->t.v2<0));
}
@Test
public void timeStamp(){


assertTrue(SequenceM.of(1,2,3,4,5)
.timestamp()
.allMatch(t-> t.v2 <= System.currentTimeMillis()));


}
@Test
public void get0(){
assertThat(SequenceM.of(1).get(0).v1,equalTo(1));
}
@Test
public void getMultple(){
assertThat(SequenceM.of(1,2,3,4,5).get(2).v1,equalTo(3));
}
@Test
public void getMultpleStream(){
assertThat(SequenceM.of(1,2,3,4,5).get(2).v2.toList(),equalTo(Arrays.asList(1,2,3,4,5)));
}
@Test(expected=NoSuchElementException.class)
public void getMultiple1(){
SequenceM.of(1).get(1);
}
@Test(expected=NoSuchElementException.class)
public void getEmpty(){
SequenceM.of().get(0);
}
@Test
public void elementAt0(){
assertTrue(SequenceM.of(1).elementAt(0).isPresent());
}
@Test
public void elementAtMultple(){
assertThat(SequenceM.of(1,2,3,4,5).elementAt(2).get(),equalTo(3));
}
@Test
public void elementAt1(){
assertFalse(SequenceM.of(1).elementAt(1).isPresent());
}
@Test
public void elementAtEmpty(){
assertFalse(SequenceM.of().elementAt(0).isPresent());
}
@Test
public void singleTest(){
assertThat(SequenceM.of(1).single(),equalTo(1));
}
@Test(expected=UnsupportedOperationException.class)
public void singleEmpty(){
SequenceM.of().single();
}
@Test(expected=UnsupportedOperationException.class)
public void single2(){
SequenceM.of(1,2).single();
}
@Test
public void limitTime(){
List<Integer> result = SequenceM.of(1,2,3,4,5,6)
Expand All @@ -36,6 +102,16 @@ public void limitTime(){
assertThat(result,equalTo(Arrays.asList(1,2,3,4)));
}
@Test
public void limitTimeEmpty(){
List<Integer> result = SequenceM.<Integer>of()
.peek(i->sleep(i*100))
.limit(1000,TimeUnit.MILLISECONDS)
.toList();


assertThat(result,equalTo(Arrays.asList()));
}
@Test
public void skipTime(){
List<Integer> result = SequenceM.of(1,2,3,4,5,6)
.peek(i->sleep(i*100))
Expand All @@ -45,6 +121,16 @@ public void skipTime(){

assertThat(result,equalTo(Arrays.asList(4,5,6)));
}
@Test
public void skipTimeEmpty(){
List<Integer> result = SequenceM.<Integer>of()
.peek(i->sleep(i*100))
.skip(1000,TimeUnit.MILLISECONDS)
.toList();


assertThat(result,equalTo(Arrays.asList()));
}
private int sleep(Integer i) {
try {
Thread.currentThread().sleep(i);
Expand All @@ -54,6 +140,30 @@ private int sleep(Integer i) {
return i;
}
@Test
public void testSkipLast(){
assertThat(SequenceM.of(1,2,3,4,5)
.skipLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList(1,2,3)));
}
@Test
public void testSkipLastEmpty(){
assertThat(SequenceM.of()
.skipLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList()));
}
@Test
public void testLimitLast(){
assertThat(SequenceM.of(1,2,3,4,5)
.limitLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList(4,5)));
}
@Test
public void testLimitLastEmpty(){
assertThat(SequenceM.of()
.limitLast(2)
.collect(Collectors.toList()),equalTo(Arrays.asList()));
}
@Test
public void endsWith(){
assertTrue(SequenceM.of(1,2,3,4,5,6)
.endsWith(Arrays.asList(5,6)));
Expand Down

0 comments on commit f23bbf1

Please sign in to comment.