Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on Dec 30, 2011
@bbejeck Added EventPublisher, PurchaseEvent, PurchaseEventSubscriber, SimpleE…
…vent and EventBusTest
ee50d10
Commits on Jan 01, 2012
@bbejeck Moved events to separate package, created subscriber package and adde…
…d BaseEventSubscriber, CashPurchaseEventSubscriber, CreditPurchaseEventSubscriber updated test
d081f2e
View
30 src/main/java/bbejeck/guava/eventbus/EventPublisher.java
@@ -0,0 +1,30 @@
+package bbejeck.guava.eventbus;
+
+import bbejeck.guava.eventbus.events.CashPurchaseEvent;
+import bbejeck.guava.eventbus.events.CreditPurchaseEvent;
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/29/11
+ * Time: 11:15 PM
+ */
+
+public class EventPublisher {
+
+ EventBus eventBus;
+
+ public EventPublisher(EventBus eventBus) {
+ this.eventBus = eventBus;
+ }
+
+ public void createCashPurchaseEvent(String description, long amount) {
+ eventBus.post(new CashPurchaseEvent(amount, description));
+ }
+
+ public void createCreditPurchaseEvent(String item, String ccNumber, long amount) {
+ eventBus.post(new CreditPurchaseEvent(amount, ccNumber, item));
+ }
+
+}
View
22 src/main/java/bbejeck/guava/eventbus/events/CashPurchaseEvent.java
@@ -0,0 +1,22 @@
+package bbejeck.guava.eventbus.events;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/29/11
+ * Time: 11:12 PM
+ */
+
+public class CashPurchaseEvent extends PurchaseEvent {
+
+ private String item;
+
+ public CashPurchaseEvent(long amount, String item) {
+ super(amount);
+ this.item = item;
+ }
+
+ public String getItem() {
+ return item;
+ }
+}
View
28 src/main/java/bbejeck/guava/eventbus/events/CreditPurchaseEvent.java
@@ -0,0 +1,28 @@
+package bbejeck.guava.eventbus.events;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 12:50 PM
+ */
+
+public class CreditPurchaseEvent extends PurchaseEvent {
+
+ private String creditCardNumber;
+ private String item;
+
+ public CreditPurchaseEvent(long amount, String item, String creditCardNumber) {
+ super(amount);
+ this.item = item;
+ this.creditCardNumber = creditCardNumber;
+ }
+
+ public String getCreditCardNumber() {
+ return creditCardNumber;
+ }
+
+ public String getItem() {
+ return item;
+ }
+}
View
20 src/main/java/bbejeck/guava/eventbus/events/PurchaseEvent.java
@@ -0,0 +1,20 @@
+package bbejeck.guava.eventbus.events;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 12:42 PM
+ */
+
+public abstract class PurchaseEvent {
+ protected long amount;
+
+ public PurchaseEvent(long amount) {
+ this.amount = amount;
+ }
+
+ public long getAmount() {
+ return amount;
+ }
+}
View
30 src/main/java/bbejeck/guava/eventbus/subscriber/BaseEventSubscriber.java
@@ -0,0 +1,30 @@
+package bbejeck.guava.eventbus.subscriber;
+
+import com.google.common.eventbus.EventBus;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 10:04 PM
+ */
+
+public abstract class BaseEventSubscriber<T> {
+
+ EventBus eventBus;
+ List<T> events = new ArrayList<T>();
+
+ public BaseEventSubscriber(EventBus eventBus) {
+ this.eventBus = eventBus;
+ this.eventBus.register(this);
+ }
+
+ public abstract void handleEvent(T event);
+
+ public List<T> getHandledEvents() {
+ return events;
+ }
+}
View
25 src/main/java/bbejeck/guava/eventbus/subscriber/CashPurchaseEventSubscriber.java
@@ -0,0 +1,25 @@
+package bbejeck.guava.eventbus.subscriber;
+
+import bbejeck.guava.eventbus.events.CashPurchaseEvent;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 10:35 PM
+ */
+
+public class CashPurchaseEventSubscriber extends BaseEventSubscriber<CashPurchaseEvent> {
+
+ public CashPurchaseEventSubscriber(EventBus eventBus) {
+ super(eventBus);
+ }
+
+ @Override
+ @Subscribe
+ public void handleEvent(CashPurchaseEvent event) {
+ events.add(event);
+ }
+}
View
25 src/main/java/bbejeck/guava/eventbus/subscriber/CreditPurchaseEventSubscriber.java
@@ -0,0 +1,25 @@
+package bbejeck.guava.eventbus.subscriber;
+
+import bbejeck.guava.eventbus.events.CreditPurchaseEvent;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 10:37 PM
+ */
+
+public class CreditPurchaseEventSubscriber extends BaseEventSubscriber<CreditPurchaseEvent> {
+
+ public CreditPurchaseEventSubscriber(EventBus eventBus) {
+ super(eventBus);
+ }
+
+ @Override
+ @Subscribe
+ public void handleEvent(CreditPurchaseEvent event) {
+ events.add(event);
+ }
+}
View
25 src/main/java/bbejeck/guava/eventbus/subscriber/PurchaseEventSubscriber.java
@@ -0,0 +1,25 @@
+package bbejeck.guava.eventbus.subscriber;
+
+import bbejeck.guava.eventbus.events.PurchaseEvent;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/31/11
+ * Time: 10:39 PM
+ */
+
+public class PurchaseEventSubscriber extends BaseEventSubscriber<PurchaseEvent> {
+
+ public PurchaseEventSubscriber(EventBus eventBus) {
+ super(eventBus);
+ }
+
+ @Override
+ @Subscribe
+ public void handleEvent(PurchaseEvent event) {
+ events.add(event);
+ }
+}
View
65 src/test/java/bbejeck/guava/eventbus/EventBusTest.java
@@ -0,0 +1,65 @@
+package bbejeck.guava.eventbus;
+
+import bbejeck.guava.eventbus.events.CashPurchaseEvent;
+import bbejeck.guava.eventbus.events.CreditPurchaseEvent;
+import bbejeck.guava.eventbus.subscriber.CashPurchaseEventSubscriber;
+import bbejeck.guava.eventbus.subscriber.CreditPurchaseEventSubscriber;
+import bbejeck.guava.eventbus.subscriber.PurchaseEventSubscriber;
+import com.google.common.eventbus.EventBus;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: bbejeck
+ * Date: 12/29/11
+ * Time: 11:34 PM
+ */
+public class EventBusTest {
+
+ EventPublisher eventPublisher;
+ CashPurchaseEventSubscriber cashPurchaseEventSubscriber;
+ CreditPurchaseEventSubscriber creditPurchaseEventSubscriber;
+ PurchaseEventSubscriber purchaseEventSubscriber;
+ EventBus eventBus;
+
+ @Before
+ public void setUp() {
+ eventBus = new EventBus();
+ eventPublisher = new EventPublisher(eventBus);
+ cashPurchaseEventSubscriber = new CashPurchaseEventSubscriber(eventBus);
+ creditPurchaseEventSubscriber = new CreditPurchaseEventSubscriber(eventBus);
+ purchaseEventSubscriber = new PurchaseEventSubscriber(eventBus);
+ }
+
+ @Test
+ public void testCashPurchaseEventReceived() {
+ eventPublisher.createCashPurchaseEvent("Jeep Wrangler", 25000l);
+ assertThat(cashPurchaseEventSubscriber.getHandledEvents().size(), is(1));
+ assertThat(creditPurchaseEventSubscriber.getHandledEvents().size(), is(0));
+ assertSame(cashPurchaseEventSubscriber.getHandledEvents().get(0).getClass(), CashPurchaseEvent.class);
+ }
+
+ @Test
+ public void testCreditCardPurchaseEventReceived() {
+ eventPublisher.createCreditPurchaseEvent("Plane Tickets", "123456789", 25900l);
+ assertThat(cashPurchaseEventSubscriber.getHandledEvents().size(), is(0));
+ assertThat(creditPurchaseEventSubscriber.getHandledEvents().size(), is(1));
+ assertSame(creditPurchaseEventSubscriber.getHandledEvents().get(0).getClass(), CreditPurchaseEvent.class);
+ }
+
+ @Test
+ public void testGetAllPurchaseEvents() {
+ eventPublisher.createCashPurchaseEvent("Jeep Wrangler", 25700l);
+ eventPublisher.createCreditPurchaseEvent("Jeep Wrangler", "123456789", 25300l);
+ assertThat(purchaseEventSubscriber.getHandledEvents().size(), is(2));
+
+ assertSame(purchaseEventSubscriber.getHandledEvents().get(0).getClass(), CashPurchaseEvent.class);
+ assertSame(purchaseEventSubscriber.getHandledEvents().get(1).getClass(), CreditPurchaseEvent.class);
+ }
+
+}

No commit comments for this range

Something went wrong with that request. Please try again.