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

net.sf.javagimmicks.collections8.MoreCollections Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections8;

import java.util.Collection;
import java.util.Collections;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import net.sf.javagimmicks.util8.Pair;

/**
 * Provides some {@link Collection} helper functions in addition to
 * {@link Collections}.
 */
public class MoreCollections
{
   private MoreCollections()
   {}

   /**
    * Performs a classic "cross-product" (non-parallel)
    * {@link Iterable#forEach(java.util.function.Consumer)} operation on two
    * given {@link Iterable}s invoking the given {@link BiConsumer} for each
    * combination of elements.
    * 
    * @param aElements
    *           the first level {@link Iterable} to iterate over
    * @param bElements
    *           the second level {@link Iterable} to iterate over
    * @param action
    *           the {@link BiConsumer} which will be called with each pair of
    *           elements of the two given {@link Iterable}s
    * @param 
    *           the type of elements of the first level {@link Iterable}
    * @param 
    *           the type of elements of the second level {@link Iterable}
    */
   public static  void crossProductForEach(final Iterable aElements,
         final Iterable bElements,
         final BiConsumer action)
   {
      aElements.forEach(aElement -> //
            bElements.forEach(bElement -> //
                  action.accept(aElement, bElement)));
   }

   /**
    * Creates a sequential {@link Stream} which contains the cross-product of
    * two given {@link Collection}s as {@link Pair} objects.
    * 
    * @param aCollection
    *           the "outer" {@link Collection} for the cross-product
    * @param bCollection
    *           the "inner" {@link Collection} for the cross-product
    * @param 
    *           the type of elements of the "outer" {@link Collection}
    * @param 
    *           the type of elements of the "inner" {@link Collection}
    * @return the resulting {@link Stream} of {@link Pair}s
    */
   public static  Stream> crossProductStream(final Collection aCollection,
         final Collection bCollection)
   {
      return StreamSupport.stream(new CrossProductSpliterator(aCollection, bCollection), false);
   }

   /**
    * Creates a parallel {@link Stream} which contains the cross-product of two
    * given {@link Collection}s as {@link Pair} objects.
    * 
    * @param aCollection
    *           the "outer" {@link Collection} for the cross-product
    * @param bCollection
    *           the "inner" {@link Collection} for the cross-product
    * @param 
    *           the type of elements of the "outer" {@link Collection}
    * @param 
    *           the type of elements of the "inner" {@link Collection}
    * @return the resulting {@link Stream} of {@link Pair}s
    */
   public static  Stream> crossProductParallelStream(final Collection aCollection,
         final Collection bCollection)
   {
      return StreamSupport.stream(new CrossProductSpliterator(aCollection, bCollection), true);
   }
}