summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2014-06-25 10:12:13 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2014-06-26 11:03:31 (GMT)
commitf17907c88ed455f3e4bacaa737ef0406a82ef64c (patch)
treecaed300b826d33f9359f55a44521ab1bc5d4c8ff
parentdb8568aa6a9bbfa8dca9cdae696e8428a0068d89 (diff)
downloadstroke-f17907c88ed455f3e4bacaa737ef0406a82ef64c.zip
stroke-f17907c88ed455f3e4bacaa737ef0406a82ef64c.tar.bz2
Import SimpleEventLoop into stroke
Import the class SimpleEventLoop from Swiften into Stroke. This also involves renaming the current SimpleEventLoop class to ImmediateEventLoop Test Information: By code inspection. Change-Id: Ie108a7b3ff98bb078cdd0017f4536e8bd9b76956 Signed-off-by: Alex Clayton <alex.clayton@isode.com>
-rw-r--r--src/com/isode/stroke/eventloop/ImmediateEventLoop.java17
-rw-r--r--src/com/isode/stroke/eventloop/SimpleEventLoop.java104
-rw-r--r--test/com/isode/stroke/parser/XMLParserTest.java3
3 files changed, 115 insertions, 9 deletions
diff --git a/src/com/isode/stroke/eventloop/ImmediateEventLoop.java b/src/com/isode/stroke/eventloop/ImmediateEventLoop.java
new file mode 100644
index 0000000..19bb42d
--- /dev/null
+++ b/src/com/isode/stroke/eventloop/ImmediateEventLoop.java
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2010-14, Isode Limited, London, England.
+ * All rights reserved.
+ */
+
+package com.isode.stroke.eventloop;
+
+/**
+ * Don't use this, it simply runs the callback in the same thread.
+ * It is useful for unit testing, but will break GUIs.
+ */
+public class ImmediateEventLoop extends EventLoop {
+ @Override
+ protected void post(Event event) {
+ handleEvent(event);
+ }
+}
diff --git a/src/com/isode/stroke/eventloop/SimpleEventLoop.java b/src/com/isode/stroke/eventloop/SimpleEventLoop.java
index 423321e..a471457 100644
--- a/src/com/isode/stroke/eventloop/SimpleEventLoop.java
+++ b/src/com/isode/stroke/eventloop/SimpleEventLoop.java
@@ -1,17 +1,107 @@
/*
- * Copyright (c) 2010, Isode Limited, London, England.
- * All rights reserved.
+* Copyright (c) 2014, Isode Limited, London, England.
+* All rights reserved.
+*/
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
*/
package com.isode.stroke.eventloop;
-/**
- * Don't use this, it simply runs the callback in the same thread.
- * It is useful for unit testing, but will break GUIs.
- */
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
public class SimpleEventLoop extends EventLoop {
+
+ private boolean isRunning_;
+ private final List<Event> events_ = new ArrayList<Event>();
+ private final Object eventsMutex_ = new Object();
+
+ public SimpleEventLoop() {
+ isRunning_ = true;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ synchronized (eventsMutex_) {
+ if (!events_.isEmpty()) {
+ System.err.println("Warning: Pending events in SimpleEventLoop at finalize time");
+ }
+ }
+ }
+
+ public void run() {
+ doRun(false);
+ }
+
+ public void runUntilEvents() {
+ doRun(true);
+ }
+
+ private void doRun(boolean breakAfterEvents) {
+ while (isRunning_) {
+ List<Event> events = new ArrayList<Event>();
+ synchronized (eventsMutex_) {
+ while (events_.isEmpty()) {
+ try {
+ eventsMutex_.wait();
+ } catch (InterruptedException e) {
+ // Ignore
+ }
+ }
+ swapCollectionContents(events, events_);
+ }
+ for (Event event : events) {
+ handleEvent(event);
+ }
+ if (breakAfterEvents) {
+ return;
+ }
+ }
+ }
+
+ public void runOnce() {
+ List<Event> events = new ArrayList<Event>();
+ synchronized (eventsMutex_) {
+ swapCollectionContents(events, events_);
+ }
+ for (Event event : events) {
+ handleEvent(event);
+ }
+ }
+
+ public void stop() {
+ postEvent(new Event.Callback() {
+
+ @Override
+ public void run() {
+ doStop();
+ }
+
+ });
+ }
+
+ private void doStop() {
+ isRunning_ = false;
+ }
+
+ static <T> void swapCollectionContents(Collection<T> coll1, Collection<T> coll2) {
+ Collection<T> temp = new ArrayList<T>(coll1);
+ coll1.clear();
+ coll1.addAll(coll2);
+ coll2.clear();
+ coll2.addAll(temp);
+ }
+
@Override
protected void post(Event event) {
- handleEvent(event);
+ synchronized (eventsMutex_) {
+ events_.add(event);
+ eventsMutex_.notifyAll();
+ }
}
+
}
diff --git a/test/com/isode/stroke/parser/XMLParserTest.java b/test/com/isode/stroke/parser/XMLParserTest.java
index d9406bd..41c10cb 100644
--- a/test/com/isode/stroke/parser/XMLParserTest.java
+++ b/test/com/isode/stroke/parser/XMLParserTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2011, Isode Limited, London, England.
+ * Copyright (c) 2010-2014, Isode Limited, London, England.
* All rights reserved.
*/
/*
@@ -8,7 +8,6 @@
*/
package com.isode.stroke.parser;
-import com.isode.stroke.eventloop.SimpleEventLoop;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;