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

no.digipost.collection.EnforceAtMostOneElementCollector Maven / Gradle / Ivy

There is a newer version: 0.36
Show newest version
/*
 * Copyright (C) Posten Norge AS
 *
 * 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 no.digipost.collection;

import no.digipost.concurrent.OneTimeAssignment;
import no.digipost.concurrent.OneTimeAssignment.AlreadyAssigned;
import no.digipost.util.ViewableAsOptional;

import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import static java.util.Collections.unmodifiableSet;

public class EnforceAtMostOneElementCollector implements Collector, Optional> {

    private static final Set CHARACTERISTICS = unmodifiableSet(EnumSet.of(Characteristics.CONCURRENT));

    private final BiFunction exceptionOnExcessiveElements;

    public EnforceAtMostOneElementCollector(BiFunction exceptionOnExcessiveElements) {
        this.exceptionOnExcessiveElements = exceptionOnExcessiveElements;
    }

    @Override
    public Supplier> supplier() {
        return OneTimeAssignment::newInstance;
    }

    @Override
    public BiConsumer, T> accumulator() {
        return this::trySet;
    }

    @Override
    public BinaryOperator> combiner() {
        return (a1, a2) -> {
            if (a1.isSet() && a2.isSet()) {
                throw exceptionOnExcessiveElements.apply(a1.get(), a2.get());
            } else if (a1.isSet()) {
                return a1;
            } else {
                return a2;
            }
        };
    }

    @Override
    public Function, Optional> finisher() {
        return ViewableAsOptional::toOptional;
    }

    @Override
    public Set characteristics() {
        return CHARACTERISTICS;
    }



    private void trySet(OneTimeAssignment ref, T value) {
        try {
            ref.set(value);
        } catch (AlreadyAssigned e) {
            RuntimeException tooManyElements = exceptionOnExcessiveElements.apply(ref.get(), value);
            tooManyElements.addSuppressed(e);
            throw tooManyElements;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy