com.speedment.plugins.json.internal.JsonCollectorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-stream Show documentation
Show all versions of json-stream Show documentation
This plugin allows you to easily convert Speedment entities into JSON.
/**
*
* Copyright (c) 2006-2018, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.plugins.json.internal;
import com.speedment.plugins.json.JsonCollector;
import java.util.*;
import static java.util.Objects.requireNonNull;
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;
/**
*
* @author Emil Forslund
* @param type of the entity
* @since 1.0.0
*/
public final class JsonCollectorImpl implements JsonCollector {
public static JsonCollector collect(Function converter) {
return new JsonCollectorImpl<>(converter);
}
@Override
public Supplier supplier() {
return () -> new StringJoiner(",", "[", "]");
}
@Override
public BiConsumer accumulator() {
return (sj, t) -> sj.add(converter.apply(t));
}
@Override
public BinaryOperator combiner() {
return StringJoiner::merge;
}
@Override
public Function finisher() {
return StringJoiner::toString;
}
private static final Set CHARACTERISTICS
= Collections.unmodifiableSet(EnumSet.noneOf(Characteristics.class));
@Override
public Set characteristics() {
return CHARACTERISTICS;
}
private JsonCollectorImpl(Function converter) {
this.converter = requireNonNull(converter);
}
private final Function converter;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy