• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

Revision9c9f9daf8e551ac6f5bc3e72d8b617ca70c1e1e2 (tree)
Zeit2015-07-02 07:24:38
AutorMirrgieRiana
CommiterMirrgieRiana

Log Message

add mirrg.event.nitrogen

Ändern Zusammenfassung

Diff

diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/.classpath
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/.classpath Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,7 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<classpath>
3+ <classpathentry kind="src" path="src"/>
4+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+ <classpathentry kind="output" path="bin"/>
7+</classpath>
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/.project
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/.project Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<projectDescription>
3+ <name>mirrg.event.nitrogen</name>
4+ <comment></comment>
5+ <projects>
6+ </projects>
7+ <buildSpec>
8+ <buildCommand>
9+ <name>org.eclipse.jdt.core.javabuilder</name>
10+ <arguments>
11+ </arguments>
12+ </buildCommand>
13+ </buildSpec>
14+ <natures>
15+ <nature>org.eclipse.jdt.core.javanature</nature>
16+ </natures>
17+</projectDescription>
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/HNitrogenEvent.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/HNitrogenEvent.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,11 @@
1+package mirrg.event.nitrogen;
2+
3+public class HNitrogenEvent
4+{
5+
6+ public static INitrogenEventManager createInstance()
7+ {
8+ return new NitrogenEventManagerImpl();
9+ }
10+
11+}
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventManager.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventManager.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,9 @@
1+package mirrg.event.nitrogen;
2+
3+/**
4+ * とてもシンプルなイベントマネージャです。
5+ */
6+public interface INitrogenEventManager extends INitrogenEventRegistry, INitrogenEventReceiver
7+{
8+
9+}
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventReceiver.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventReceiver.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,12 @@
1+package mirrg.event.nitrogen;
2+
3+public interface INitrogenEventReceiver
4+{
5+
6+ /**
7+ * 登録時のクラスがこのイベントと代入互換であるハンドラを、
8+ * 登録時の順番に起動します。
9+ */
10+ public <E> void post(E event);
11+
12+}
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventRegistry.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/INitrogenEventRegistry.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,13 @@
1+package mirrg.event.nitrogen;
2+
3+import java.util.function.Consumer;
4+
5+public interface INitrogenEventRegistry
6+{
7+
8+ /**
9+ * 登録した順番に呼び出されます。
10+ */
11+ public <E> void register(Class<E> clazz, Consumer<E> handler);
12+
13+}
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/NitrogenEventManagerImpl.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/NitrogenEventManagerImpl.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,30 @@
1+package mirrg.event.nitrogen;
2+
3+import java.util.ArrayList;
4+import java.util.function.Consumer;
5+
6+class NitrogenEventManagerImpl implements INitrogenEventManager
7+{
8+
9+ private ArrayList<Class<?>> classes = new ArrayList<>();
10+ private ArrayList<Consumer<?>> handlers = new ArrayList<>();
11+
12+ @Override
13+ public <E> void register(Class<E> clazz, Consumer<E> handler)
14+ {
15+ classes.add(clazz);
16+ handlers.add(handler);
17+ }
18+
19+ @SuppressWarnings("unchecked")
20+ @Override
21+ public <E> void post(E event)
22+ {
23+ for (int i = 0; i < classes.size(); i++) {
24+ if (classes.get(i).isInstance(event)) {
25+ ((Consumer<E>) handlers.get(i)).accept(event);
26+ }
27+ }
28+ }
29+
30+}
diff -r b682a49a9911 -r 9c9f9daf8e55 mirrg.event.nitrogen/src/mirrg/event/nitrogen/test/TestNitrogenEvent.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.event.nitrogen/src/mirrg/event/nitrogen/test/TestNitrogenEvent.java Thu Jul 02 07:24:38 2015 +0900
@@ -0,0 +1,86 @@
1+package mirrg.event.nitrogen.test;
2+
3+import static org.junit.Assert.*;
4+import mirrg.event.nitrogen.HNitrogenEvent;
5+import mirrg.event.nitrogen.INitrogenEventManager;
6+
7+import org.junit.Test;
8+
9+public class TestNitrogenEvent
10+{
11+
12+ @Test
13+ public void test()
14+ {
15+ String[] message = {
16+ "",
17+ };
18+
19+ INitrogenEventManager nitrogenEventManager = HNitrogenEvent.createInstance();
20+ nitrogenEventManager.register(Integer.class, event -> {
21+ message[0] += event;
22+ });
23+
24+ nitrogenEventManager.post(Integer.valueOf(4));
25+ assertEquals("4", message[0]);
26+ }
27+
28+ @Test
29+ public void test2()
30+ {
31+ String[] message = {
32+ "",
33+ };
34+
35+ INitrogenEventManager nitrogenEventManager = HNitrogenEvent.createInstance();
36+ nitrogenEventManager.register(Integer.class, event -> {
37+ message[0] += event;
38+ });
39+ nitrogenEventManager.register(String.class, event -> {
40+ message[0] += event;
41+ });
42+
43+ nitrogenEventManager.post(Integer.valueOf(4));
44+ assertEquals("4", message[0]);
45+
46+ nitrogenEventManager.post("G");
47+ assertEquals("4G", message[0]);
48+
49+ }
50+
51+ @Test
52+ public void test3()
53+ {
54+ String[] message = {
55+ "",
56+ };
57+
58+ INitrogenEventManager nitrogenEventManager = HNitrogenEvent.createInstance();
59+ nitrogenEventManager.register(Integer.class, event -> {
60+ message[0] += "I";
61+ });
62+ nitrogenEventManager.register(String.class, event -> {
63+ message[0] += "S";
64+ });
65+ nitrogenEventManager.register(Double.class, event -> {
66+ message[0] += "D";
67+ });
68+ nitrogenEventManager.register(Number.class, event -> {
69+ message[0] += "N";
70+ });
71+
72+ nitrogenEventManager.post(Integer.valueOf(4));
73+ assertEquals("IN", message[0]);
74+
75+ nitrogenEventManager.post("G");
76+ assertEquals("INS", message[0]);
77+
78+ nitrogenEventManager.post((Object) Integer.valueOf(4));
79+ assertEquals("INSIN", message[0]);
80+
81+ nitrogenEventManager.post(Double.valueOf(4));
82+ assertEquals("INSINDN", message[0]);
83+
84+ }
85+
86+}