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

com.zackehh.jackson.stream.collectors.ArrayNodeCollector Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.zackehh.jackson.stream.collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

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;
import java.util.stream.Collector;

/**
 * Collector to collect a Stream of JsonNode instance into a new ArrayNode instance.
 *
 * Even though this class could operate on arbitrary types by coercing values into
 * JsonNode instance, it's better to enforce explicitness in pipelines as it raises
 * error handling into the userland layer.
 */
public class ArrayNodeCollector implements Collector {

    /**
     * Simply returns a Supplier which creates a new empty ArrayNode instance
     * in order to collect JsonNode values into.
     */
    @Override
    public Supplier supplier() {
        return JsonNodeFactory.instance::arrayNode;
    }

    /**
     * Returns a BiConsumer which adds a pre-created JsonNode into the ArrayNode
     * instance provided by the Supplier.
     */
    @Override
    public BiConsumer accumulator() {
        return ArrayNode::add;
    }

    /**
     * Returns an operator which combines two ArrayNode instances by adding all
     * JsonNode values from the right instance into the left instance.
     */
    @Override
    public BinaryOperator combiner() {
        return ArrayNode::addAll;
    }

    /**
     * Returns a Function to identify the input.
     *
     * This is moot and not called due to the provided Characteristics.
     */
    @Override
    public Function finisher() {
        return Function.identity();
    }

    /**
     * Returns a Set of Characteristics to apply to this Collector. The only one
     * provided is that there's an identity finisher, in order to remove the call.
     */
    @Override
    public Set characteristics() {
        return EnumSet.of(Characteristics.IDENTITY_FINISH);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy