You are viewing a plain text version of this content. The canonical link for it is here.
Posted to s4-commits@incubator.apache.org by mm...@apache.org on 2012/01/03 14:03:28 UTC
[16/50] [abbrv] git commit: Added arbitrary attributes to Event.
Event no longer abstract.
Added arbitrary attributes to Event. Event no longer abstract.
Project: http://git-wip-us.apache.org/repos/asf/incubator-s4/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s4/commit/12d1ccb4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s4/tree/12d1ccb4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s4/diff/12d1ccb4
Branch: refs/heads/piper
Commit: 12d1ccb429a8522ce86365d00e5672a72498de22
Parents: 5b6fae2
Author: Leo Neumeyer <le...@s4.io>
Authored: Thu Dec 8 22:47:01 2011 -0800
Committer: Leo Neumeyer <le...@s4.io>
Committed: Thu Dec 8 22:47:01 2011 -0800
----------------------------------------------------------------------
.../src/main/java/org/apache/s4/base/Event.java | 93 ++++++++++++---
.../src/main/java/org/apache/s4/base/MapEvent.java | 15 ---
.../org/apache/s4/base/EventAttributeTest.java | 27 ++++
.../test/java/org/apache/s4/base/SimpleEvent.java | 18 +++
4 files changed, 119 insertions(+), 34 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/12d1ccb4/subprojects/s4-base/src/main/java/org/apache/s4/base/Event.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-base/src/main/java/org/apache/s4/base/Event.java b/subprojects/s4-base/src/main/java/org/apache/s4/base/Event.java
index c1f86bd..b8bc0fe 100644
--- a/subprojects/s4-base/src/main/java/org/apache/s4/base/Event.java
+++ b/subprojects/s4-base/src/main/java/org/apache/s4/base/Event.java
@@ -15,11 +15,21 @@
*/
package org.apache.s4.base;
-public abstract class Event {
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class Event {
+
+ private static final Logger logger = LoggerFactory.getLogger(Event.class);
final private long time;
private int streamId;
private int appId;
+ private Map<String, Data<?>> map;
/** Default constructor sets time using system time. */
protected Event() {
@@ -27,8 +37,7 @@ public abstract class Event {
}
/**
- * This constructor explicitly sets the time. Event that need to explicitly
- * set the time must call {super(time)}
+ * This constructor explicitly sets the time. Event that need to explicitly set the time must call {super(time)}
*/
protected Event(long time) {
this.time = time;
@@ -42,8 +51,8 @@ public abstract class Event {
}
/**
- * The stream id is used to identify streams uniquely in a cluster
- * configuration. It is not required to operate in local mode.
+ * The stream id is used to identify streams uniquely in a cluster configuration. It is not required to operate in
+ * local mode.
*
* @return the target stream id
*/
@@ -52,8 +61,8 @@ public abstract class Event {
}
/**
- * The stream id is used to identify streams uniquely in a cluster
- * configuration. It is not required to operate in local mode.
+ * The stream id is used to identify streams uniquely in a cluster configuration. It is not required to operate in
+ * local mode.
*
* @param targetStreamId
*/
@@ -62,11 +71,9 @@ public abstract class Event {
}
/**
- * All events must be assigned the unique App ID of the App that owns the
- * stream to which this event is injected. The assignment must be done
- * automatically by the stream that receives the event. Each application
- * has a unique ID. We use the app ID in combination with the stream
- * ID to identify stream instances in a cluster.
+ * All events must be assigned the unique App ID of the App that owns the stream to which this event is injected.
+ * The assignment must be done automatically by the stream that receives the event. Each application has a unique
+ * ID. We use the app ID in combination with the stream ID to identify stream instances in a cluster.
*
*
* @return the unique application ID.
@@ -76,17 +83,65 @@ public abstract class Event {
}
/**
- * All events must be assigned the unique App ID of the App that owns the
- * stream to which this event is injected. The assignment must be done
- * automatically by the stream that receives the event. Each application
- * has a unique ID. We use the app ID in combination with the stream
- * ID to identify stream instances in a cluster.
+ * All events must be assigned the unique App ID of the App that owns the stream to which this event is injected.
+ * The assignment must be done automatically by the stream that receives the event. Each application has a unique
+ * ID. We use the app ID in combination with the stream ID to identify stream instances in a cluster.
*
* @param appId
- * a unique application identifier, typically assigned by the
- * deployment system.
+ * a unique application identifier, typically assigned by the deployment system.
*/
public void setAppId(int appId) {
this.appId = appId;
}
+
+ /**
+ * Put an arbitrary key-value pair in the event. The type of the value is T.
+ *
+ * @param type
+ * the type of the value
+ * @param key
+ * the key
+ * @param value
+ * the value
+ */
+ public <T> void put(String key, Class<T> type, T value) {
+
+ if (map == null) {
+ map = Maps.newHashMap();
+ }
+
+ map.put(key, new Data<T>(type, value));
+ }
+
+ /**
+ * Get value for key. The caller must know the type of the value.
+ *
+ * @param key
+ * @return the value
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T get(String key, Class<T> type) {
+
+ Data<?> data = map.get(key);
+
+ if (type != data.type) {
+ logger.error("Trying to get a value of type {} for an attribute of type {}.", type, data.type);
+ return null;
+ }
+
+ return (T) data.value;
+ }
+
+ /* Helper data object. */
+ private class Data<T> {
+
+ private T value;
+ private Class<T> type;
+
+ private Data(Class<T> type, T value) {
+ this.value = value;
+ this.type = type;
+ }
+
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/12d1ccb4/subprojects/s4-base/src/main/java/org/apache/s4/base/MapEvent.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-base/src/main/java/org/apache/s4/base/MapEvent.java b/subprojects/s4-base/src/main/java/org/apache/s4/base/MapEvent.java
deleted file mode 100644
index 2baefcd..0000000
--- a/subprojects/s4-base/src/main/java/org/apache/s4/base/MapEvent.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package org.apache.s4.base;
-
-public class MapEvent extends Event {
-
- public <T> void put(Class<T> type, String name, T value) {
-
- }
-
- public <T> T get(String name) {
-
- T x = null;
-
- return x;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/12d1ccb4/subprojects/s4-core/src/test/java/org/apache/s4/base/EventAttributeTest.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/test/java/org/apache/s4/base/EventAttributeTest.java b/subprojects/s4-core/src/test/java/org/apache/s4/base/EventAttributeTest.java
new file mode 100644
index 0000000..b4ed181
--- /dev/null
+++ b/subprojects/s4-core/src/test/java/org/apache/s4/base/EventAttributeTest.java
@@ -0,0 +1,27 @@
+package org.apache.s4.base;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class EventAttributeTest {
+
+ @Test
+ public void test() {
+
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("snoopy", "dog");
+
+ SimpleEvent ev = new SimpleEvent("Hello");
+ ev.put("An Int", Integer.class, 33);
+ ev.put("A String", String.class, "XXX");
+ ev.put("A Map", Map.class, map);
+
+ Assert.assertEquals("XXX", ev.get("A String", String.class));
+ Assert.assertEquals(33, ev.get("An Int", Integer.class).intValue());
+ Assert.assertEquals("dog", ev.get("A Map", Map.class).get("snoopy"));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/12d1ccb4/subprojects/s4-core/src/test/java/org/apache/s4/base/SimpleEvent.java
----------------------------------------------------------------------
diff --git a/subprojects/s4-core/src/test/java/org/apache/s4/base/SimpleEvent.java b/subprojects/s4-core/src/test/java/org/apache/s4/base/SimpleEvent.java
new file mode 100644
index 0000000..b1b2915
--- /dev/null
+++ b/subprojects/s4-core/src/test/java/org/apache/s4/base/SimpleEvent.java
@@ -0,0 +1,18 @@
+package org.apache.s4.base;
+
+public class SimpleEvent extends Event {
+
+ private String name;
+
+ public SimpleEvent(String name) {
+ super();
+ this.name = name;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+}