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

org.jgrasstools.gears.utils.StreamUtils Maven / Gradle / Ivy

/*
 * This file is part of JGrasstools (http://www.jgrasstools.org)
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * JGrasstools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.jgrasstools.gears.utils;

import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * Utilities for Streams. This more a streams j8 documentation then something to really use.
 *
 * 

Use .peek(e->sysout) to debug without interference on the stream's items

*

Use primitive streams were possible: {@link IntStream}, {@link LongStream}, {@link DoubleStream}.

* *

Intermediate Methods, which produce again streams: *

    *
  • map
  • *
  • filter
  • *
  • distinct
  • *
  • sorted
  • *
  • peek
  • *
  • limit
  • *
  • substream
  • *
  • parallel
  • *
  • sequential
  • *
  • unordered
  • *
      *

      *

      Terminate Methods, after which the stream is consumed and no more operations can be performed on it: *

    • forEach
    • *
    • forEachOrdered
    • *
    • toArray
    • *
    • reduce
    • *
    • collect
    • *
    • min
    • *
    • max
    • *
    • count
    • *
    • anyMatch
    • *
    • allMatch
    • *
    • noneMatch
    • *
    • findFirst
    • *
    • findAny
    • *
    • iterator
    • *

      *

      Short-circuit Methods, that stop stream processing once conditions are satisfied. *

    • anyMatch
    • *
    • allMatch
    • *
    • noneMatch
    • *
    • findFirst
    • *
    • findAny
    • *
    • limit
    • *
    • substream
    • *

      * * @author Antonello Andrea (www.hydrologis.com) */ public class StreamUtils { public static Stream fromArray( T[] array ) { return Stream.of(array); } public static Stream fromList( List list ) { return list.stream(); } public static Stream fromListParallel( List list ) { return list.parallelStream(); } public static Stream fromSupplier( Supplier supplier ) { return Stream.generate(supplier); } public static Stream distinct( Stream stream ) { return stream.distinct(); } public static long countStrings( Stream stream, boolean distinct, boolean ignoreCase ) { if (ignoreCase && distinct) { return stream.map(String::toLowerCase).distinct().count(); } else if (ignoreCase) { return stream.map(String::toLowerCase).count(); } else if (distinct) { return stream.distinct().count(); } else { return stream.count(); } } public static String getString( Stream stream, String separator, String prefix, String suffix ) { if (prefix != null || suffix != null) { if (prefix == null) prefix = ""; if (suffix == null) suffix = ""; return stream.map(Object::toString).collect(Collectors.joining(separator, prefix, suffix)); } return stream.map(Object::toString).collect(Collectors.joining(separator)); } public static List toList( Stream stream ) { return stream.collect(Collectors.toList()); } public static Set toSet( Stream stream ) { return stream.collect(Collectors.toSet()); } public static TreeSet toTreeSet( Stream stream ) { return stream.collect(Collectors.toCollection(TreeSet::new)); } /** * Collect stream to map. * * @param stream the stream to convert. * @param keySupplier the supplier for the key, ex. Object::getId() * @param valueSupplier the value supplier, ex. Object::getValue() * @return the map. */ public static Map toMap( Stream stream, Function keySupplier, Function valueSupplier ) { return stream.collect(Collectors.toMap(keySupplier, valueSupplier)); } public static Map toMapWithToStringValue( Stream stream, Function keySupplier ) { return stream.collect(Collectors.toMap(keySupplier, Function.identity())); } /** * Collect a map by grouping based on the object's field. * * @param stream the stream to collect. * @param groupingFunction the function supplying the grouping field, ex. Object::getField() * @return the map of lists. */ public static Map> toMapGroupBy( Stream stream, Function groupingFunction ) { return stream.collect(Collectors.groupingBy(groupingFunction)); // to get a set: Collectors.groupingBy(groupingFunction, Collectors.toSet()) } /** * Split a stream into a map with true and false. * * @param stream the stream to collect. * @param predicate the predicate to use to define true or false, ex. e->e.getValue().equals("test") * @return the map of lists. */ public static Map> toMapPartition( Stream stream, Predicate predicate ) { return stream.collect(Collectors.partitioningBy(predicate)); } public static void printLongStats( Stream stream, ToLongFunction summarizingFunction ) { LongSummaryStatistics summary = stream.collect(Collectors.summarizingLong(summarizingFunction)); System.out.println(summary.getCount()); System.out.println(summary.getSum()); System.out.println(summary.getMin()); System.out.println(summary.getMax()); System.out.println(summary.getAverage()); } /** * Find an element in the stream. * * @param stream the stream to check. This should be parallel. * @param predicate the predicate to use. * @return the element or null. */ public static T findAny( Stream stream, Predicate predicate ) { Optional element = stream.filter(predicate).findAny(); return element.orElse(null); } public static Stream findAll( Stream stream, Predicate predicate ) { Stream filteredStream = stream.filter(predicate); return filteredStream; } }