SelectiveActionBuilder { public Message process(final Event event) { Optional<Message> result = FluentIterable.from(actions).filter(new Predicate<Map.Entry<Predicate<Event>, Action<Event>>>() { @Override public boolean apply(Entry<Predicate<Event>, Action<Event>> input) { return input.getKey().apply(event); } }).transform(new Function<Entry<Predicate<Event>, Action<Event>>, Message>() { @Override public Message apply(Entry<Predicate<Event>, Action<Event>> input) { Message result = input.getValue().process(event); return (result == null) ? Message.NULL : result; } }).first(); return result.or(Message.NULL); }  static SelectiveActionBuilder newRule(); static Predicate<Event> predicate(final boolean apply); SelectiveActionBuilder on(Class<? extends Event> eventType); SelectiveActionBuilder onOthers(); SelectiveActionBuilder ignore(); SelectiveActionBuilder store(); SelectiveActionBuilder passTrough(); @SuppressWarnings("unchecked") SelectiveActionBuilder on(EventType type); SelectiveActionBuilder on(Predicate<Event> check); @SuppressWarnings("unchecked") SelectiveActionBuilder run(Action<? extends Event> action); Message process(final Event event);  }
@Test public void should_reactOnDifferentEvents() { Message result = b.process(execFinishEvent); assertThat(result, is(equalTo(Message.NULL))); assertThat("Incorrect sequence of events", executionFinished && !commit && !executionStart); result = b.process(commitEvent); assertThat(result, is(equalTo(Message.NULL))); assertThat("Incorrect sequence of events", executionFinished && commit && !executionStart); result = b.process(execStartEvent); assertThat(result, is(equalTo(Message.NULL))); assertThat("Incorrect sequence of events", executionFinished && commit && executionStart); }
@Test public void should_returnNullMessageOnDO_NOTHING() { assertThat(b.process(suiteFinish), is(equalTo(Message.NULL))); }
@Test public void should_returnOriginalMessageOnPASS_TROUGH() { assertThat(b.process(suiteStart).toEvent(), is(equalTo(suiteStart))); }
@Test public void should_useTypeToRecognizeEvent() { assertThat(b.process(log).toEvent(), is(equalTo(log))); }
@Test public void should_useTruePredicateOnUnspecifiedEvents() { assertThat(b.process(action).toEvent(), is(equalTo(action))); }