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

org.infinispan.commons.dataconversion.internal.JsonArrayCollector Maven / Gradle / Ivy

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

import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * A {@link Collector} implementation that create a {@link Json} array.
 *
 * @author Pedro Ruivo
 * @since 13.0
 */
public class JsonArrayCollector implements Collector {

   private final Json array;

   public JsonArrayCollector() {
      this.array = Json.array();
   }

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

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

   @Override
   public BinaryOperator combiner() {
      return JsonArrayCollector::combine;
   }

   @Override
   public Function finisher() {
      return JsonArrayCollector::getArray;
   }

   @Override
   public Set characteristics() {
      return Collections.emptySet();
   }

   public void add(Json json) {
      this.array.add(json);
   }

   public JsonArrayCollector combine(JsonArrayCollector collector) {
      collector.array.asJsonList().forEach(this::add);
      return this;
   }

   public Json getArray() {
      return array;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy