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

com.cookingfox.lapasse.impl.facade.LaPasseRxFacade Maven / Gradle / Ivy

The newest version!
package com.cookingfox.lapasse.impl.facade;

import com.cookingfox.lapasse.api.command.bus.CommandBus;
import com.cookingfox.lapasse.api.command.bus.RxCommandBus;
import com.cookingfox.lapasse.api.event.bus.EventBus;
import com.cookingfox.lapasse.api.facade.RxFacade;
import com.cookingfox.lapasse.api.logging.LoggersHelper;
import com.cookingfox.lapasse.api.message.store.MessageStore;
import com.cookingfox.lapasse.api.state.State;
import com.cookingfox.lapasse.api.state.manager.RxStateManager;
import com.cookingfox.lapasse.api.state.manager.StateManager;
import com.cookingfox.lapasse.api.state.observer.RxStateObserver;
import com.cookingfox.lapasse.api.state.observer.StateChanged;
import com.cookingfox.lapasse.impl.command.bus.DefaultRxCommandBus;
import com.cookingfox.lapasse.impl.state.manager.DefaultRxStateManager;
import rx.Observable;
import rx.Scheduler;

/**
 * Implementation of {@link RxFacade}, containing a Builder class.
 *
 * @param  The concrete type of the state object.
 */
public class LaPasseRxFacade extends LaPasseFacade implements RxFacade {

    //----------------------------------------------------------------------------------------------
    // CONSTRUCTOR
    //----------------------------------------------------------------------------------------------

    public LaPasseRxFacade(RxCommandBus commandBus,
                           EventBus eventBus,
                           LoggersHelper loggers,
                           MessageStore messageStore,
                           RxStateManager stateManager) {
        super(commandBus, eventBus, loggers, messageStore, stateManager);
    }

    //----------------------------------------------------------------------------------------------
    // RX COMMAND BUS
    //----------------------------------------------------------------------------------------------

    @Override
    public void setCommandObserveScheduler(Scheduler observeOnScheduler) {
        getRxCommandBus().setCommandObserveScheduler(observeOnScheduler);
    }

    @Override
    public void setCommandSubscribeScheduler(Scheduler subscribeOnScheduler) {
        getRxCommandBus().setCommandSubscribeScheduler(subscribeOnScheduler);
    }

    //----------------------------------------------------------------------------------------------
    // RX STATE OBSERVER
    //----------------------------------------------------------------------------------------------

    @Override
    public Observable> observeStateChanges() {
        return getRxStateObserver().observeStateChanges();
    }

    //----------------------------------------------------------------------------------------------
    // PROTECTED METHODS
    //----------------------------------------------------------------------------------------------

    /**
     * @return The command bus as Rx command bus.
     */
    protected RxCommandBus getRxCommandBus() {
        return (RxCommandBus) commandBus;
    }

    /**
     * @return The state observer as Rx state observer.
     */
    protected RxStateObserver getRxStateObserver() {
        // noinspection unchecked
        return (RxStateObserver) stateManager;
    }

    //----------------------------------------------------------------------------------------------
    // INNER CLASS: BUILDER
    //----------------------------------------------------------------------------------------------

    public static class Builder extends LaPasseFacade.Builder {

        //------------------------------------------------------------------------------------------
        // CONSTRUCTOR
        //------------------------------------------------------------------------------------------

        public Builder(S initialState) {
            super(initialState);
        }

        //------------------------------------------------------------------------------------------
        // PUBLIC METHODS
        //------------------------------------------------------------------------------------------

        @Override
        public LaPasseRxFacade build() {
            return new LaPasseRxFacade<>(getCommandBus(), getEventBus(), getLoggersHelper(),
                    getMessageStore(), getStateManager());
        }

        //------------------------------------------------------------------------------------------
        // GETTERS
        //------------------------------------------------------------------------------------------

        @Override
        public RxCommandBus getCommandBus() {
            if (commandBus == null) {
                commandBus = new DefaultRxCommandBus<>(getMessageStore(), getEventBus(),
                        getLoggersHelper(), getStateManager());
            }

            return (RxCommandBus) super.getCommandBus();
        }

        @Override
        public RxStateManager getStateManager() {
            if (stateManager == null) {
                stateManager = new DefaultRxStateManager<>(initialState);
            }

            return (RxStateManager) super.getStateManager();
        }

        //------------------------------------------------------------------------------------------
        // SETTERS
        //------------------------------------------------------------------------------------------

        @Override
        public LaPasseRxFacade.Builder setCommandBus(CommandBus commandBus) {
            if (!(commandBus instanceof RxCommandBus)) {
                throw new IllegalArgumentException("Command bus must be an implementation of " + RxCommandBus.class);
            }

            return (LaPasseRxFacade.Builder) super.setCommandBus(commandBus);
        }

        @Override
        public LaPasseRxFacade.Builder setEventBus(EventBus eventBus) {
            return (LaPasseRxFacade.Builder) super.setEventBus(eventBus);
        }

        @Override
        public LaPasseRxFacade.Builder setLoggersHelper(LoggersHelper loggersHelper) {
            return (LaPasseRxFacade.Builder) super.setLoggersHelper(loggersHelper);
        }

        @Override
        public LaPasseRxFacade.Builder setMessageStore(MessageStore messageStore) {
            return (LaPasseRxFacade.Builder) super.setMessageStore(messageStore);
        }

        @Override
        public LaPasseRxFacade.Builder setStateManager(StateManager stateManager) {
            if (!(stateManager instanceof RxStateManager)) {
                throw new IllegalArgumentException("State manager must be an implementation of " + RxStateManager.class);
            }

            return (LaPasseRxFacade.Builder) super.setStateManager(stateManager);
        }

    }

}