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

org.infinispan.commons.util.ArrayCollector Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util;

import java.util.EnumSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;

public class ArrayCollector implements java.util.stream.Collector, Supplier {
   private final Object[] array;
   private int pos = 0;

   public ArrayCollector(Object[] array) {
      this.array = array;
   }

   public void add(Object item) {
      array[pos] = item;
      ++pos;
   }

   @Override
   public Supplier supplier() {
      return this;
   }

   @Override
   public ArrayCollector get() {
      return this;
   }

   @Override
   public BiConsumer accumulator() {
      return ArrayCollector::add;
   }

   @Override
   public BinaryOperator combiner() {
      return (a1, a2) -> {
         throw new UnsupportedOperationException("The stream is not supposed to be parallel");
      };
   }

   @Override
   public Function finisher() {
      return Function.identity();
   }

   @Override
   public Set characteristics() {
      return EnumSet.of(Characteristics.IDENTITY_FINISH);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy