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

no.digipost.collection.EnforceDistinctFirstTupleElementCollector 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.tuple.Tuple;
import no.digipost.tuple.ViewableAsTuple;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.*;
import java.util.stream.Collector;

import static java.util.Collections.unmodifiableSet;


public class EnforceDistinctFirstTupleElementCollector implements Collector>, MultitupleBuilder, Optional>>> {

    private static final Set CHARACTERISTICS = unmodifiableSet(EnumSet.noneOf(Characteristics.class));

    private final BiFunction>, ? super Tuple>, ? extends RuntimeException> exceptionOnNonDistinctFirstElement;

    public EnforceDistinctFirstTupleElementCollector(BiFunction>, ? super Tuple>, ? extends RuntimeException> exceptionOnNonDistinctFirstElement) {
        this.exceptionOnNonDistinctFirstElement = exceptionOnNonDistinctFirstElement;
    }

    @Override
    public Supplier> supplier() {
        return () -> new MultitupleBuilder<>(exceptionOnNonDistinctFirstElement);
    }

    @Override
    public BiConsumer, ViewableAsTuple>> accumulator() {
        return MultitupleBuilder::tryAccumulate;
    }

    @Override
    public BinaryOperator> combiner() {
        return (m1, m2) -> { throw new UnsupportedOperationException("combiner() is not supported"); } ;
    }

    @Override
    public Function, Optional>>> finisher() {
        return MultitupleBuilder::build;
    }

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



class MultitupleBuilder {

    private final OneTimeAssignment firstElement = OneTimeAssignment.newInstance();
    private final ConcurrentLinkedDeque secondElements = new ConcurrentLinkedDeque<>();

    private final BiFunction>, ? super Tuple>, ? extends RuntimeException> exceptionOnNonDistinctFirstElement;

    MultitupleBuilder(BiFunction>, ? super Tuple>, ? extends RuntimeException> exceptionOnNonDistinctFirstElement) {
        this.exceptionOnNonDistinctFirstElement = exceptionOnNonDistinctFirstElement;
    }

    void tryAccumulate(ViewableAsTuple> hasTuple) {
        Tuple> tuple = hasTuple.asTuple();
        try {
            firstElement.set(tuple.first());
        } catch (AlreadyAssigned e) {
            T1 assignedFirst = firstElement.get();
            if (!Objects.equals(assignedFirst, tuple.first())) {
                RuntimeException tooManyElements = exceptionOnNonDistinctFirstElement.apply(Tuple.of(assignedFirst, new ArrayList<>(secondElements)), tuple);
                tooManyElements.addSuppressed(e);
                throw tooManyElements;
            }
        }
        tuple.second().ifPresent(secondElements::add);
    }

    Optional>> build() {
        T1 first = firstElement.get();
        return first != null ? Optional.of(Tuple.of(first, new ArrayList<>(secondElements))) : Optional.empty();
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy