package com.areoit.guava.eventbus; import org.junit.Test; import static org.junit.Assert.assertEquals; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class EventBusIntegerEventsTests { private static final String EVENT_BUS_ID = "numbers"; /** * Case: * + one listener * + listener subcribe for Integer event * + listener subcribe for Number event * + REMEMBER: Integer IS-A Number */ @Test public void oneIntegerEventTwoSubscribingMethod() { IntegerListener listener = new IntegerListener(); Integer integerEvent = Integer.MAX_VALUE; EventBus eventBus = new EventBus(EVENT_BUS_ID); eventBus.register(listener); eventBus.post(integerEvent); //check event counters int integerCount = listener.getIntegerEventCount(); int numberCount = listener.getNumberEventCount(); assertEquals(1, integerCount); assertEquals(1, numberCount); } class IntegerListener { private int integerEventCount = 0; private int numberEventCount = 0; @Subscribe public void onIntegerEvent(Integer event) { ++integerEventCount; } @Subscribe public void onNumberEvent(Number event) { ++numberEventCount; } public int getIntegerEventCount() { return integerEventCount; } public int getNumberEventCount() { return numberEventCount; } } }
20 lis 2012
Guava - EventBus example
Guava - Functions + composition
public class FunctionsTests { @Test public void Functions_converter_example() { FunctionstringToInteger = new Function () { @Override public Integer apply(String input) { return Integer.valueOf(input); } }; Function integerToLong = new Function\ () { @Override public Long apply(Integer input) { return Long.valueOf(input.longValue()); } }; Function stringToLong = Functions.compose(integerToLong, stringToInteger); Long actual = stringToLong.apply("5"); Long expected = 5L; assertEquals(expected, actual); } }
Subskrybuj:
Posty (Atom)