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

org.infinispan.functional.impl.Traversables Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.functional.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;

import org.infinispan.functional.Traversable;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.Closeables;

public final class Traversables {

   public static  Traversable of(Stream stream) {
      return new StreamTraversable<>(stream);
   }

   public static  CloseableIterator asIterator(Traversable traversable) {
      if (traversable instanceof StreamTraversable)
         return Closeables.iterator(((StreamTraversable) traversable).stream);
      else {
         List collected = traversable.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
         return Closeables.iterator(collected.iterator());
      }
   }

   private Traversables() {
      // Cannot be instantiated, it's just a holder class
   }

   // TODO: Attention! This is a very rudimentary/simplistic implementation!
   private static final class StreamTraversable implements Traversable {
      final Stream stream;

      private StreamTraversable(Stream stream) {
         this.stream = stream;
      }

      @Override
      public Traversable filter(Predicate p) {
         return new StreamTraversable<>(stream.filter(p));
      }

      @Override
      public  Traversable map(Function f) {
         return new StreamTraversable<>(stream.map(f));
      }

      @Override
      public  Traversable flatMap(Function> f) {
         Function> mapper = new Function>() {
            @Override
            public Stream apply(T t) {
               StreamTraversable applied = (StreamTraversable) f.apply(t);
               return applied.stream;
            }
         };

         return new StreamTraversable<>(stream.flatMap(mapper));
      }

      @Override
      public void forEach(Consumer c) {
         stream.forEach(c);
      }

      @Override
      public T reduce(T z, BinaryOperator folder) {
         return stream.reduce(z, folder);
      }

      @Override
      public Optional reduce(BinaryOperator folder) {
         return stream.reduce(folder);
      }

      @Override
      public  U reduce(U z, BiFunction mapper, BinaryOperator folder) {
         return stream.reduce(z, mapper, folder);
      }

      @Override
      public  R collect(Supplier s, BiConsumer accumulator, BiConsumer combiner) {
         return stream.collect(s, accumulator, combiner);
      }

      @Override
      public  R collect(Collector collector) {
         return stream.collect(collector);
      }

      @Override
      public long count() {
         return stream.count();
      }

      @Override
      public boolean anyMatch(Predicate p) {
         return stream.anyMatch(p);
      }

      @Override
      public boolean allMatch(Predicate p) {
         return stream.allMatch(p);
      }

      @Override
      public boolean noneMatch(Predicate predicate) {
         return stream.noneMatch(predicate);
      }

      @Override
      public Optional findAny() {
         return stream.findAny();
      }
   }

}