Java Code Examples for com.annimon.stream.Stream#close()

The following examples show how to use com.annimon.stream.Stream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2, 2, 3, 4, 4, 5)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer value) {
                    return value < 4;
                }
            })
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            })
            .distinct()
            .limit(2);
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
 
Example 2
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseFlatMap() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter[0]++;
        }
    };

    Stream<Integer> stream = Stream.rangeClosed(2, 4)
            .onClose(runnable)
            .onClose(runnable)
            .flatMap(new Function<Integer, Stream<Integer>>() {
                @Override
                public Stream<Integer> apply(final Integer i) {
                    return Stream.rangeClosed(2, 4)
                            .onClose(runnable);
                }
            });
    stream.count();
    assertThat(counter[0], is(3));
    stream.close();
    assertThat(counter[0], is(5));
}
 
Example 3
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseConcat() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter[0]++;
        }
    };

    Stream<Integer> stream1 = Stream.range(0, 2).onClose(runnable);
    Stream<Integer> stream2 = Stream.range(0, 2).onClose(runnable);
    Stream<Integer> stream = Stream.concat(stream1, stream2)
            .onClose(runnable);
    stream.count();
    assertThat(counter[0], is(0));
    stream.close();
    assertThat(counter[0], is(3));
}
 
Example 4
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2, 2, 3, 4, 4, 5)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer value) {
                    return value < 4;
                }
            })
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            })
            .distinct()
            .limit(2);
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
 
Example 5
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseFlatMap() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter[0]++;
        }
    };

    Stream<Integer> stream = Stream.rangeClosed(2, 4)
            .onClose(runnable)
            .onClose(runnable)
            .flatMap(new Function<Integer, Stream<Integer>>() {
                @Override
                public Stream<Integer> apply(final Integer i) {
                    return Stream.rangeClosed(2, 4)
                            .onClose(runnable);
                }
            });
    stream.count();
    assertThat(counter[0], is(3));
    stream.close();
    assertThat(counter[0], is(5));
}
 
Example 6
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseConcat() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter[0]++;
        }
    };

    Stream<Integer> stream1 = Stream.range(0, 2).onClose(runnable);
    Stream<Integer> stream2 = Stream.range(0, 2).onClose(runnable);
    Stream<Integer> stream = Stream.concat(stream1, stream2)
            .onClose(runnable);
    stream.count();
    assertThat(counter[0], is(0));
    stream.close();
    assertThat(counter[0], is(3));
}
 
Example 7
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnClose() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2)
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            });
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
 
Example 8
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnClose() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2)
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            });
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}