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

org.axonframework.eventsourcing.conflictresolution.Conflicts Maven / Gradle / Ivy

Go to download

Module containing all necessary infrastructure components to support Event Sourcing Command and Query models.

There is a newer version: 4.10.3
Show newest version
/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * 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 org.axonframework.eventsourcing.conflictresolution;

import org.axonframework.eventhandling.DomainEventMessage;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.messaging.Message;

import java.util.List;
import java.util.function.Predicate;

/**
 * Utility class providing common Predicates used to detect conflicts between the actual state of an event sourced
 * aggregate and the expected state of the aggregate.
 *
 * @author Rene de Waele
 */
public class Conflicts {

    /**
     * Returns a {@link Predicate} for a {@link ConflictResolver} that responds affirmative if any event in a list of
     * unseen events matches the given {@code messageFilter}. If the returned predicate matches an unseen event the
     * ConflictResolver will throw an exception.
     *
     * @param messageFilter predicate for a single event
     * @return a Predicate to detect conflicts in unseen aggregate events
     */
    public static > Predicate> eventMatching(
            Predicate messageFilter) {
        return events -> events.stream().anyMatch(messageFilter::test);
    }

    /**
     * Returns a {@link Predicate} for a {@link ConflictResolver} that responds affirmative if the payload of any event
     * in a list of unseen events matches the given {@code messageFilter}. If the returned predicate matches an unseen
     * event the ConflictResolver will throw an exception.
     *
     * @param payloadFilter predicate for the payload of a single event
     * @return a Predicate to detect conflicts in unseen aggregate events
     */
    public static Predicate>> payloadMatching(Predicate payloadFilter) {
        return events -> events.stream().map(Message::getPayload).anyMatch(payloadFilter::test);
    }

    /**
     * Returns a {@link Predicate} for a {@link ConflictResolver} that responds affirmative if the payload of any event
     * in a list of unseen events is of given {@code payloadType} and matches the given {@code messageFilter}. If the
     * returned predicate matches an unseen event the ConflictResolver will throw an exception.
     *
     * @param payloadType   the type of event payload to filter for
     * @param payloadFilter predicate for the payload of a single event
     * @return a Predicate to detect conflicts in unseen aggregate events
     */
    @SuppressWarnings("unchecked")
    public static  Predicate>> payloadMatching(Class payloadType,
                                                                       Predicate
                                                                                               payloadFilter) {
        return events -> events.stream().filter(event -> payloadType.isAssignableFrom(event.getPayloadType()))
                .map(event -> (T) event.getPayload()).anyMatch(payloadFilter::test);
    }

    /**
     * Returns a {@link Predicate} for a {@link ConflictResolver} that responds affirmative if the payload type of any
     * event in a list of unseen events is assignable to given {@code payloadType}. If the returned predicate matches an
     * unseen event the ConflictResolver will throw an exception.
     *
     * @param payloadType the type of event payload to filter for
     * @return a Predicate to detect conflicts in unseen aggregate events
     */
    public static  Predicate>> payloadTypeOf(Class payloadType) {
        return eventMatching(event -> payloadType.isAssignableFrom(event.getPayloadType()));
    }

    private Conflicts() {
    }
}