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

com.eventsourcing.StandardCommand Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.eventsourcing;

import com.eventsourcing.hlc.HybridTimestamp;

/**
 * Thin implementation of {@link Command}
 * @param  result type
 * @param  state type
 */
public abstract class StandardCommand extends StandardEntity> implements Command {

    public StandardCommand() { super(); }
    public StandardCommand(HybridTimestamp timestamp) {
        super(timestamp);
    }

    /**
     * Returns a stream of events that should be recorded. By default, an empty stream returned.
     *
     * @return stream of events
     * @throws Exception if the command is to be rejected, an exception has to be thrown. In this case, no events will
     *                   be recorded
     */

    public EventStream events() throws Exception {
        return EventStream.empty();
    }

    /**
     * Returns a stream of events that should be recorded. By default, calls {@link #events()}
     *
     * @param repository Configured repository
     * @return stream of events
     * @throws Exception if the command is to be rejected, an exception has to be thrown. In this case, no events will
     *                   be recorded
     */

    public EventStream events(Repository repository) throws Exception {
        return events();
    }

    @Override public EventStream events(Repository repository, LockProvider lockProvider) throws
                                                                                                       Exception {
        return events(repository);
    }

    /**
     * Once all events are recorded, this callback will be invoked.
     *
     * By default, it calls {@link #result(Object, Repository)}
     *
     * @param state
     * @param repository
     * @param lockProvider
     * @return result
     */
    @Override public R result(S state, Repository repository, LockProvider lockProvider) {
        return result(state, repository);
    }

    /**
     * By default, calls {@link #result(Object)}
     *
     * @param state
     * @param repository
     * @return result
     */
    public R result(S state, Repository repository) {
        return result(state);
    }

    /**
     * By default, it calls {@link #result()}
     *
     * @param state
     * @return result
     */
    public R result(S state) {
        return result();
    }

    /**
     * By default, does nothing (returns null)
     * @return result
     */
    public R result() {
        return null;
    }
}