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-2016, 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 java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collector.Characteristics.CONCURRENT;
/**
*
* @author Emil Forslund
* @since 1.0.0
*/
public final class JsonCollectorImpl implements JsonCollector {
public static JsonCollector collect(Function converter, Function, String> merger) {
return new JsonCollectorImpl<>(converter, merger);
}
@Override
public Supplier> supplier() {
return () -> Collections.synchronizedList(new ArrayList<>());
}
@Override
public BiConsumer, ENTITY> accumulator() {
return (l, t) -> {
l.add(converter.apply(t));
};
}
@Override
public BinaryOperator> combiner() {
return (l1, l2) -> {
l1.addAll(l2);
return l1;
};
}
@Override
public Function, String> finisher() {
return merger::apply;
}
@Override
public Set characteristics() {
return EnumSet.of(CONCURRENT);
}
private JsonCollectorImpl(Function converter, Function, String> merger) {
this.converter = requireNonNull(converter);
this.merger = requireNonNull(merger);
}
private final Function converter;
private final Function, String> merger;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy