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

com.automationrockstars.bmo.event.processor.SelectiveActionBuilder Maven / Gradle / Ivy

The newest version!
/*
 * 
 */

package com.automationrockstars.bmo.event.processor;

import com.automationrockstars.gunter.EventType;
import com.automationrockstars.gunter.events.Event;
import com.automationrockstars.gunter.events.impl.EventImplUtils;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import org.apache.commons.lang.ArrayUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class SelectiveActionBuilder {

    List, Action>> actions = Lists.newArrayList();
    private Predicate currentEventType;

    public static SelectiveActionBuilder newRule() {
        return new SelectiveActionBuilder();
    }

    private static Predicate createClassPredicate(final Class clazz) {
        return new Predicate() {

            @Override
            public boolean apply(Event input) {
                return ArrayUtils.contains(input.getClass().getInterfaces(), clazz);
            }
        };
    }

    public static Predicate predicate(final boolean apply) {
        return new Predicate() {

            @Override
            public boolean apply(Event input) {
                return apply;
            }
        };
    }

    public SelectiveActionBuilder on(Class eventType) {
        currentEventType = createClassPredicate(eventType);
        return this;
    }

    public SelectiveActionBuilder onOthers() {
        on(EventType.ALL);
        return this;
    }

    public SelectiveActionBuilder ignore() {
        run(Action.DO_NOTHING);
        return this;
    }

    public SelectiveActionBuilder store() {
        run(Action.STORE);
        return this;
    }

    public SelectiveActionBuilder passTrough() {
        run(Action.PASS_TROUGH);
        return this;
    }

    @SuppressWarnings("unchecked")
    public SelectiveActionBuilder on(EventType type) {
        if (type.equals(EventType.ALL)) {
            currentEventType = predicate(true);
        } else {
            currentEventType = createClassPredicate((Class) EventImplUtils.getClassForType(type).getInterfaces()[0]);
        }
        return this;
    }

    public SelectiveActionBuilder on(Predicate check) {
        currentEventType = check;
        return this;
    }

    @SuppressWarnings("unchecked")
    public SelectiveActionBuilder run(Action action) {
        actions.add(Collections.singletonMap(currentEventType, (Action) action).entrySet().iterator().next());
        return this;
    }

    public Message process(final Event event) {
        Optional result = FluentIterable.from(actions).filter(new Predicate, Action>>() {
            @Override
            public boolean apply(Entry, Action> input) {
                return input.getKey().apply(event);
            }
        }).transform(new Function, Action>, Message>() {
            @Override
            public Message apply(Entry, Action> input) {
                Message result = input.getValue().process(event);
                return (result == null) ? Message.NULL : result;
            }
        }).first();
        return result.or(Message.NULL);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy