List of usage examples for java.util Spliterator tryAdvance
boolean tryAdvance(Consumer<? super T> action);
From source file:de.tudarmstadt.lt.seg.app.Segmenter.java
public static void split_and_tokenize(Reader reader, String docid, ISentenceSplitter sentenceSplitter, ITokenizer tokenizer, int level_filter, int level_normalize, boolean merge_types, boolean merge_tokens, String separator_sentence, String separator_token, String separator_desc, PrintWriter writer) { try {/*from w ww . j a v a2 s. com*/ final StringBuffer buf = new StringBuffer(); // used for checking of stream is empty; take care when not running sequentially but in parallel! sentenceSplitter.init(reader).stream().sequential().forEach(sentence_segment -> { if (DEBUG) { writer.format("%s%s", docid, separator_desc); writer.println(sentence_segment.toString()); writer.print(separator_sentence); } if (sentence_segment.type != SegmentType.SENTENCE) return; tokenizer.init(sentence_segment.asString()); Stream<String> tokens = null; if (DEBUG) tokens = tokenizer.stream().map(x -> x.toString() + separator_token); else tokens = StreamSupport.stream(tokenizer .filteredAndNormalizedTokens(level_filter, level_normalize, merge_types, merge_tokens) .spliterator(), false).map(x -> x + separator_token); Spliterator<String> spliterator = tokens.spliterator(); tokens = StreamSupport.stream(spliterator, false); buf.setLength(0); boolean empty = !spliterator.tryAdvance(x -> { buf.append(x); }); if (empty) return; synchronized (writer) { // writer.write(Thread.currentThread().getId() + "\t"); writer.format("%s%s", docid, separator_desc); writer.print(buf); tokens.forEach(writer::print); writer.print(separator_sentence); writer.flush(); } }); } catch (Exception e) { Throwable t = e; while (t != null) { System.err.format("%s: %s%n", e.getClass(), e.getMessage()); t = e.getCause(); } } }
From source file:org.javersion.util.PersistentHashSetTest.java
@Test public void reduce() { PersistentHashSet<HashKey> set = new PersistentHashSet<>(); int sum = 0;/*from w ww.ja va2s. c o m*/ int count = 0; // ArrayNode for (int i = 0; i < 32; i++) { sum += i; count++; set = set.conj(new HashKey(i)); } // HashNode for (int i = 1; i < 5; i++) { int num = i << (4 + i); sum += num; count++; set = set.conj(new HashKey(num)); } // CollisionNodes set = set.conj(new HashKey(1)); sum += 1; count++; set = set.conj(new HashKey(1)); sum += 1; count++; assertThat(sumOf(set.stream())).isEqualTo(sum); assertThat(set.stream().count()).isEqualTo(count); assertThat(sumOf(set.parallelStream())).isEqualTo(sum); assertThat(set.parallelStream().count()).isEqualTo(count); // Reduce partially consumed in parallel for (int i = 1; i < set.size(); i++) { Spliterator<HashKey> spliterator = set.spliterator(); final MutableInt partialSum = new MutableInt(0); for (int j = 0; j < i; j++) { spliterator.tryAdvance(k -> partialSum.add(k.hash)); } Assertions.assertThat(sumOf(StreamSupport.stream(spliterator, true)) + partialSum.intValue()) .isEqualTo(sum); } }
From source file:org.javersion.util.PersistentHashSetTest.java
@Test public void split_till_the_end() { PersistentHashSet<Integer> ints = new PersistentHashSet<Integer>().conjAll(integers()); List<Spliterator<Integer>> spliterators = new ArrayList<>(); spliterators.add(ints.spliterator()); int size = 0; while (size != spliterators.size()) { size = spliterators.size();/*from w w w.ja va 2s.c om*/ for (int i = size - 1; i >= 0; i--) { Spliterator<Integer> spliterator = spliterators.get(i); Spliterator<Integer> split = spliterator.trySplit(); if (split != null) { spliterators.add(split); } } } final MutableLong sum = new MutableLong(0); for (Spliterator<Integer> spliterator : spliterators) { while (spliterator.tryAdvance(i -> sum.add(i))) ; } Assertions.assertThat(sum.longValue()).isEqualTo(ints.stream().map(Long::new).reduce(Long::sum).get()); }
From source file:org.javersion.util.PersistentHashSetTest.java
@Test public void try_split_single_entry() { PersistentHashSet<Integer> set = new PersistentHashSet<Integer>().conj(5); assertThat(set.spliterator().trySplit()).isNull(); Spliterator<Integer> spliterator = set.spliterator(); assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(5))).isTrue(); assertThat(spliterator.trySplit()).isNull(); }
From source file:org.javersion.util.PersistentHashSetTest.java
@Test public void try_split_sub_spliterator() { PersistentHashSet<Integer> set = new PersistentHashSet<Integer>().conj(1).conj(33); Spliterator<Integer> spliterator = set.spliterator(); assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(1))).isTrue(); assertThat(spliterator.trySplit()).isNull(); assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(33))).isTrue(); assertThat(spliterator.tryAdvance(i -> { })).isFalse();/*from w w w . j ava2s . c o m*/ }