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

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

There is a newer version: 0.5.6
Show 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.LoggerCollection;
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;

/**
 * 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(CommandBus commandBus,
                           EventBus eventBus,
                           LoggerCollection loggers,
                           RxStateObserver stateObserver) {
        super(commandBus, eventBus, loggers, stateObserver);
    }

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

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

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

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

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

    public static class Builder extends LaPasseFacade.Builder {

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

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

        @Override
        public LaPasseRxFacade build() {
            return (LaPasseRxFacade) super.build();
        }

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

        @Override
        protected CommandBus createDefaultCommandBus(MessageStore messageStore,
                                                        EventBus eventBus,
                                                        LoggerCollection loggers,
                                                        StateManager stateManager) {
            return new DefaultRxCommandBus<>(messageStore, eventBus, loggers, (RxStateManager) stateManager);
        }

        @Override
        protected StateManager createDefaultStateManager(S initialState) {
            return new DefaultRxStateManager<>(initialState);
        }

        @Override
        protected LaPasseRxFacade createFacade(CommandBus commandBus,
                                                  EventBus eventBus,
                                                  LoggerCollection loggers,
                                                  StateManager stateManager) {
            return new LaPasseRxFacade<>(commandBus, eventBus, loggers, (RxStateManager) stateManager);
        }

        //------------------------------------------------------------------------------------------
        // 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 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);
        }

    }

}