From f17907c88ed455f3e4bacaa737ef0406a82ef64c Mon Sep 17 00:00:00 2001 From: Alex Clayton Date: Wed, 25 Jun 2014 11:12:13 +0100 Subject: 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 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 events_ = new ArrayList(); + 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 events = new ArrayList(); + 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 events = new ArrayList(); + 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 void swapCollectionContents(Collection coll1, Collection coll2) { + Collection temp = new ArrayList(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; -- cgit v0.10.2-6-g49f6