Permalink
Browse files

Other suggested refactoring completed.

  • Loading branch information...
1 parent 1794920 commit 6892bf1db379e1825d8ea31ec3dbae882c06b103 @skunkiferous skunkiferous committed Mar 16, 2013
@@ -24,6 +24,7 @@
package org.agilewiki.jactor;
import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.Semaphore;
import org.agilewiki.jactor.apc.JAMessage;
@@ -60,7 +61,7 @@
*/
private final BufferedEventsDestination<JAMessage> bufferedEventsDestination = new BufferedEventsDestination<JAMessage>() {
@Override
- public void putBufferedEvents(final ArrayList<JAMessage> bufferedEvents) {
+ public void putBufferedEvents(final List<JAMessage> bufferedEvents) {
final JAResponse japcResponse = (JAResponse) bufferedEvents.get(0);
result = japcResponse.getUnwrappedResponse();
done.release();
@@ -106,10 +106,13 @@ public boolean removeClosable(final Closable closable) {
*/
@Override
public void close() {
- final List<Closable> c = new ArrayList<Closable>(closables);
- final Iterator<Closable> it = c.iterator();
- while (it.hasNext())
- it.next().close();
+ for (final Closable c : closables.toArray(new Closable[closables.size()])) {
+ try {
+ c.close();
+ } catch (Throwable e) {
+ logException(false, "Failed to close "+c, e);
+ }
+ }
if (timer != null)
timer.cancel();
threadManager.close();
@@ -28,6 +28,7 @@
import org.agilewiki.jactor.events.EventQueue;
import java.util.ArrayList;
+import java.util.List;
/**
* Serves as the asynchronous transport for APCMessages.
@@ -101,5 +102,5 @@
*
* @return The event queue.
*/
- public EventQueue<ArrayList<JAMessage>> getEventQueue();
+ public EventQueue<List<JAMessage>> getEventQueue();
}
@@ -23,7 +23,7 @@
*/
package org.agilewiki.jactor.apc;
-import java.util.ArrayList;
+import java.util.List;
import org.agilewiki.jactor.ExceptionHandler;
import org.agilewiki.jactor.MailboxFactory;
@@ -101,7 +101,7 @@ public void processEvent(final JAMessage event) {
.getUnwrappedRequest(),
ex2);
} else
- response(currentRequest, ex2);
+ response(currentRequest, ex);
}
}
} else {
@@ -223,7 +223,7 @@ public boolean dispatchEvents() {
*/
@Override
final public void putBufferedEvents(
- final ArrayList<JAMessage> bufferedEvents) {
+ final List<JAMessage> bufferedEvents) {
bufferedEventQueue.putBufferedEvents(bufferedEvents);
}
@@ -277,7 +277,7 @@ final public void response(final JARequest jaRequest,
* @return The event queue.
*/
@Override
- public EventQueue<ArrayList<JAMessage>> getEventQueue() {
+ public EventQueue<List<JAMessage>> getEventQueue() {
return bufferedEventQueue.getEventQueue();
}
@@ -300,8 +300,8 @@ final public void processResponse(JARequest jaRequest, Object response) {
processSyncResponse(jaRequest, response);
return;
}
- EventQueue<ArrayList<JAMessage>> srcEventQueue = sourceMailbox.getEventQueue();
- EventQueue<ArrayList<JAMessage>> controller = getEventQueue().getController();
+ EventQueue<List<JAMessage>> srcEventQueue = sourceMailbox.getEventQueue();
+ EventQueue<List<JAMessage>> controller = getEventQueue().getController();
if (srcEventQueue.getController() == controller) {
processSyncResponse(jaRequest, response);
return;
@@ -23,7 +23,7 @@
*/
package org.agilewiki.jactor.bufferedEvents;
-import java.util.ArrayList;
+import java.util.List;
/**
* A BufferedEventsDestination receives lists of events from objects operating
@@ -37,5 +37,5 @@
*
* @param bufferedEvents The events to be processed.
*/
- public void putBufferedEvents(ArrayList<E> bufferedEvents);
+ public void putBufferedEvents(List<E> bufferedEvents);
}
@@ -26,7 +26,7 @@
import org.agilewiki.jactor.events.EventDispatcher;
import org.agilewiki.jactor.events.EventQueue;
-import java.util.ArrayList;
+import java.util.List;
/**
* A BufferedEventsQueue receives buffered events, queues them,
@@ -61,5 +61,5 @@
*
* @return The event queue.
*/
- public EventQueue<ArrayList<E>> getEventQueue();
+ public EventQueue<List<E>> getEventQueue();
}
@@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
/**
* A BufferedEventsQueue receives buffered events, queues them,
@@ -43,7 +44,7 @@
/**
* The eventQueue handles the actual dispatching of buffered events.
*/
- private EventQueue<ArrayList<E>> eventQueue;
+ private EventQueue<List<E>> eventQueue;
/**
* The eventProcessor is used to process unblocked events.
@@ -59,19 +60,19 @@
* The pending HashMap holds the buffered events which
* have not yet been sent.
*/
- private HashMap<BufferedEventsDestination<E>, ArrayList<E>> pending =
- new HashMap<BufferedEventsDestination<E>, ArrayList<E>>();
+ private HashMap<BufferedEventsDestination<E>, List<E>> pending =
+ new HashMap<BufferedEventsDestination<E>, List<E>>();
/**
* Create a BufferedEventsQueue.
*
* @param eventQueue Handles the actual dispatching of buffered events.
*/
- public JABufferedEventsQueue(EventQueue<ArrayList<E>> eventQueue) {
+ public JABufferedEventsQueue(EventQueue<List<E>> eventQueue) {
this.eventQueue = eventQueue;
- eventQueue.setActiveEventProcessor(new EventProcessor<ArrayList<E>>() {
+ eventQueue.setActiveEventProcessor(new EventProcessor<List<E>>() {
@Override
- public void processEvent(ArrayList<E> bufferedEvents) {
+ public void processEvent(List<E> bufferedEvents) {
int i = 0;
while (i < bufferedEvents.size()) {
eventProcessor.processEvent(bufferedEvents.get(i));
@@ -93,7 +94,7 @@ public void haveEvents() {
* @param autonomous Inhibits the acquireControl operation when true.
*/
public JABufferedEventsQueue(ThreadManager threadManager, boolean autonomous) {
- this(new JAEventQueue<ArrayList<E>>(threadManager, autonomous));
+ this(new JAEventQueue<List<E>>(threadManager, autonomous));
}
/**
@@ -114,7 +115,7 @@ public void setInitialBufferCapacity(int initialBufferCapacity) {
*/
@Override
public void send(BufferedEventsDestination<E> destination, E event) {
- ArrayList<E> bufferedEvents = pending.get(destination);
+ List<E> bufferedEvents = pending.get(destination);
if (bufferedEvents == null) {
bufferedEvents = new ArrayList<E>(initialBufferCapacity);
pending.put(destination, bufferedEvents);
@@ -130,7 +131,7 @@ public void sendPendingEvents() {
Iterator<BufferedEventsDestination<E>> it = pending.keySet().iterator();
while (it.hasNext()) {
BufferedEventsDestination<E> destination = it.next();
- ArrayList<E> bufferedEvents = pending.get(destination);
+ List<E> bufferedEvents = pending.get(destination);
destination.putBufferedEvents(bufferedEvents);
}
pending.clear();
@@ -143,7 +144,7 @@ public void sendPendingEvents() {
* @param bufferedEvents The events to be processed.
*/
@Override
- public void putBufferedEvents(ArrayList<E> bufferedEvents) {
+ public void putBufferedEvents(List<E> bufferedEvents) {
eventQueue.putEvent(bufferedEvents);
}
@@ -184,7 +185,7 @@ public boolean dispatchEvents() {
*
* @return The event queue.
*/
- public EventQueue<ArrayList<E>> getEventQueue() {
+ public EventQueue<List<E>> getEventQueue() {
return eventQueue;
}
}
@@ -24,6 +24,7 @@
package org.agilewiki.jactor.lpc;
import java.util.ArrayList;
+import java.util.List;
import org.agilewiki.jactor.Actor;
import org.agilewiki.jactor.ExceptionHandler;
@@ -143,11 +144,13 @@ public JLPCActor getMatch(final Class targetClass) {
*/
@Override
final public JLPCActor getAncestor(final Class targetClass) {
- if (parent == null)
- return null;
- if (targetClass.isInstance(parent))
- return parent;
- return parent.getAncestor(targetClass);
+ JLPCActor p = parent;
+ while (p != null) {
+ if (targetClass.isInstance(p))
+ return p;
+ p = p.parent;
+ }
+ return null;
}
/**
@@ -252,9 +255,9 @@ final public void acceptRequest(final APCRequestSource apcRequestSource,
private void acceptOtherRequest(final Mailbox sourceMailbox,
final RequestSource rs, final Request request, final RP rp)
throws Exception {
- final EventQueue<ArrayList<JAMessage>> eventQueue = mailbox
+ final EventQueue<List<JAMessage>> eventQueue = mailbox
.getEventQueue();
- final EventQueue<ArrayList<JAMessage>> srcController = sourceMailbox
+ final EventQueue<List<JAMessage>> srcController = sourceMailbox
.getEventQueue().getController();
if (eventQueue.getController() == srcController) {
syncSend(rs, request, rp);
@@ -330,9 +333,9 @@ final public void acceptEvent(final APCRequestSource apcRequestSource,
asyncSendEvent(rs, request);
return;
}
- final EventQueue<ArrayList<JAMessage>> eventQueue = mailbox
+ final EventQueue<List<JAMessage>> eventQueue = mailbox
.getEventQueue();
- final EventQueue<ArrayList<JAMessage>> srcController = sourceMailbox
+ final EventQueue<List<JAMessage>> srcController = sourceMailbox
.getEventQueue().getController();
if (eventQueue.getController() == srcController) {
syncSendEvent(rs, request, sourceExceptionHandler);
@@ -66,8 +66,11 @@ final public RESPONSE_TYPE send(JAFuture future, Actor targetActor)
if (isTargetType(targetActor))
return (RESPONSE_TYPE) future.send(targetActor, this);
Actor parent = targetActor.getParent();
- if (parent != null)
- return send(future, parent);
+ while (parent != null) {
+ if (isTargetType(parent))
+ return (RESPONSE_TYPE) future.send(parent, this);
+ parent = parent.getParent();
+ }
throw new UnsupportedOperationException(
"request: " + getClass().getName() +
" target actor: " + targetActor.getClass().getName());
@@ -100,9 +103,12 @@ final public void send(APCRequestSource requestSource, Actor targetActor, RP<RES
return;
}
Actor parent = targetActor.getParent();
- if (parent != null) {
- send(requestSource, parent, rp);
- return;
+ while (parent != null) {
+ if (isTargetType(parent)) {
+ parent.acceptRequest(requestSource, this, rp);
+ return;
+ }
+ parent = parent.getParent();
}
throw new UnsupportedOperationException(
"request: " + getClass().getName() +
@@ -137,8 +143,11 @@ final public void sendEvent(Actor targetActor)
}
Actor parent = targetActor.getParent();
if (parent != null) {
- sendEvent(parent);
- return;
+ if (isTargetType(parent)) {
+ parent.acceptEvent(JAEvent.requestSource, this);
+ return;
+ }
+ parent = parent.getParent();
}
throw new UnsupportedOperationException(
"request: " + getClass().getName() +
@@ -171,8 +180,11 @@ final public void sendEvent(APCRequestSource requestSource, Actor targetActor)
}
Actor parent = targetActor.getParent();
if (parent != null) {
- sendEvent(requestSource, parent);
- return;
+ if (isTargetType(parent)) {
+ parent.acceptEvent(requestSource, this);
+ return;
+ }
+ parent = parent.getParent();
}
throw new UnsupportedOperationException(
"request: " + getClass().getName() +
@@ -26,6 +26,7 @@
import org.agilewiki.jactor.RP;
import java.util.ArrayList;
+import java.util.List;
/**
* Counts the number of responses received
@@ -52,9 +53,9 @@
*/
private RP xrp;
- private ArrayList<JAResponseCounter3> pool;
+ private List<JAResponseCounter3> pool;
- public JAResponseCounter3(ArrayList<JAResponseCounter3> pool) {
+ public JAResponseCounter3(List<JAResponseCounter3> pool) {
this.pool = pool;
pool.add(this);
}
@@ -26,7 +26,7 @@
import org.agilewiki.jactor.concurrent.ThreadManager;
import org.agilewiki.jactor.events.JActor;
-import java.util.ArrayList;
+import java.util.List;
/**
* Test code: Implements BufferedEventsActor.
@@ -87,7 +87,7 @@ final protected void send(BufferedEventsDestination<E> destination, E event) {
* @param bufferedEvents The events to be processed.
*/
@Override
- final public void putBufferedEvents(ArrayList<E> bufferedEvents) {
+ final public void putBufferedEvents(List<E> bufferedEvents) {
mailbox.putBufferedEvents(bufferedEvents);
}
}
@@ -24,6 +24,7 @@
package org.agilewiki.jactor.bufferedEvents;
import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.Semaphore;
/**
@@ -69,7 +70,7 @@ public E send(BufferedEventsActor<E> actor, E event) {
* @param bufferedEvents The events to be processed.
*/
@Override
- public void putBufferedEvents(ArrayList<E> bufferedEvents) {
+ public void putBufferedEvents(List<E> bufferedEvents) {
result = bufferedEvents.get(0);
done.release();
}

0 comments on commit 6892bf1

Please sign in to comment.