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

com.cookingfox.lapasse.impl.logging.RxLoggerHelper Maven / Gradle / Ivy

There is a newer version: 0.5.6
Show newest version
package com.cookingfox.lapasse.impl.logging;

import com.cookingfox.lapasse.api.command.Command;
import com.cookingfox.lapasse.api.command.logging.CommandHandlerError;
import com.cookingfox.lapasse.api.command.logging.CommandHandlerResult;
import com.cookingfox.lapasse.api.command.logging.CommandLogger;
import com.cookingfox.lapasse.api.command.logging.CommandLoggerAware;
import com.cookingfox.lapasse.api.event.Event;
import com.cookingfox.lapasse.api.event.logging.EventHandlerError;
import com.cookingfox.lapasse.api.event.logging.EventHandlerResult;
import com.cookingfox.lapasse.api.event.logging.EventLogger;
import com.cookingfox.lapasse.api.event.logging.EventLoggerAware;
import com.cookingfox.lapasse.api.state.State;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;

import java.util.Collection;

/**
 * Rx helper methods for logging features.
 */
public final class RxLoggerHelper {

    //----------------------------------------------------------------------------------------------
    // CONSTRUCTOR (disabled)
    //----------------------------------------------------------------------------------------------

    private RxLoggerHelper() {
        throw new UnsupportedOperationException();
    }

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

    /**
     * Creates an observable for command handler errors.
     *
     * @param commandLoggerAware The subject for which to observe command handler errors.
     * @return An observable for command handler errors.
     */
    public static Observable observeCommandHandlerErrors(
            final CommandLoggerAware commandLoggerAware) {
        return Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(final Subscriber subscriber) {
                // create logger
                final CommandLogger logger = new NoopCommandLogger() {
                    @Override
                    public void onCommandHandlerError(final Throwable e, final Command command) {
                        subscriber.onNext(new CommandHandlerError() {
                            @Override
                            public Command getCommand() {
                                return command;
                            }

                            @Override
                            public Throwable getError() {
                                return e;
                            }
                        });
                    }
                };

                // add logger
                commandLoggerAware.addCommandLogger(logger);

                // remove logger on unsubscribe
                subscriber.add(Subscriptions.create(new Action0() {
                    @Override
                    public void call() {
                        commandLoggerAware.removeCommandLogger(logger);
                    }
                }));
            }
        });
    }

    /**
     * Creates an observable for command handler results.
     *
     * @param commandLoggerAware The subject for which to observe command handler results.
     * @return An observable for command handler results.
     */
    public static Observable observeCommandHandlerResults(
            final CommandLoggerAware commandLoggerAware) {
        return Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(final Subscriber subscriber) {
                // create logger
                final CommandLogger logger = new NoopCommandLogger() {
                    @Override
                    public void onCommandHandlerResult(final Command command,
                                                       final Collection events) {
                        subscriber.onNext(new CommandHandlerResult() {
                            @Override
                            public Command getCommand() {
                                return command;
                            }

                            @Override
                            public Collection getEvents() {
                                return events;
                            }
                        });
                    }
                };

                // add logger
                commandLoggerAware.addCommandLogger(logger);

                // remove logger on unsubscribe
                subscriber.add(Subscriptions.create(new Action0() {
                    @Override
                    public void call() {
                        commandLoggerAware.removeCommandLogger(logger);
                    }
                }));
            }
        });
    }

    /**
     * Creates an observable for event handler errors.
     *
     * @param eventLoggerAware The subject for which to observe event handler errors.
     * @param               The concrete type of the state object.
     * @return An observable for event handler errors.
     */
    public static  Observable observeEventHandlerErrors(
            final EventLoggerAware eventLoggerAware) {
        return Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(final Subscriber subscriber) {
                // create logger
                final EventLogger logger = new NoopEventLogger() {
                    @Override
                    public void onEventHandlerError(final Throwable e, final Event event) {
                        subscriber.onNext(new EventHandlerError() {
                            @Override
                            public Event getEvent() {
                                return event;
                            }

                            @Override
                            public Throwable getError() {
                                return e;
                            }
                        });
                    }
                };

                // add logger
                eventLoggerAware.addEventLogger(logger);

                // remove logger on unsubscribe
                subscriber.add(Subscriptions.create(new Action0() {
                    @Override
                    public void call() {
                        eventLoggerAware.removeEventLogger(logger);
                    }
                }));
            }
        });
    }

    /**
     * Creates an observable for event handler results.
     *
     * @param eventLoggerAware The subject for which to observe event handler results.
     * @param               The concrete type of the state object.
     * @return An observable for event handler results.
     */
    public static  Observable observeEventHandlerResults(
            final EventLoggerAware eventLoggerAware) {
        return Observable.create(new Observable.OnSubscribe() {
            @Override
            public void call(final Subscriber subscriber) {
                // create logger
                final EventLogger logger = new NoopEventLogger() {
                    @Override
                    public void onEventHandlerResult(final Event event, final S newState) {
                        subscriber.onNext(new EventHandlerResult() {
                            @Override
                            public Event getEvent() {
                                return event;
                            }

                            @Override
                            public State getNewState() {
                                return newState;
                            }
                        });
                    }
                };

                // add logger
                eventLoggerAware.addEventLogger(logger);

                // remove logger on unsubscribe
                subscriber.add(Subscriptions.create(new Action0() {
                    @Override
                    public void call() {
                        eventLoggerAware.removeEventLogger(logger);
                    }
                }));
            }
        });
    }

    //----------------------------------------------------------------------------------------------
    // MEMBER CLASSES
    //----------------------------------------------------------------------------------------------

    /**
     * No-operation implementation of {@link CommandLogger}.
     */
    static class NoopCommandLogger implements CommandLogger {

        @Override
        public void onCommandHandlerError(Throwable e, Command command) {
            // override in subclass
        }

        @Override
        public void onCommandHandlerResult(Command command, Collection events) {
            // override in subclass
        }

    }

    /**
     * No-operation implementation of {@link EventLogger}.
     *
     * @param  The concrete type of the state object.
     */
    static class NoopEventLogger implements EventLogger {

        @Override
        public void onEventHandlerError(Throwable e, Event event) {
            // override in subclass
        }

        @Override
        public void onEventHandlerResult(Event event, S newState) {
            // override in subclass
        }

    }

}