From 9176d80ae6dcad88da79714d934b226afe66c1ad Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 15 Apr 2010 21:07:01 +0200 Subject: Instantiate TodoDeque rather than using static variables. Unfortunately, processing is now rather significantly slower. --- org/madore/damlengine/DamlEngine.java | 5 ++-- org/madore/damlengine/ElementHandler.java | 5 ++-- org/madore/damlengine/TodoDeque.java | 42 ++++++++++++++++--------------- org/madore/damlengine/TodoItem.java | 2 ++ 4 files changed, 30 insertions(+), 24 deletions(-) (limited to 'org') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index a2ce87e..b87b069 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -18,12 +18,13 @@ public final class DamlEngine { private DamlEngine() { } // Forbid instantiation public static void processDocument() { + TodoDeque todoDeque = new TodoDeque(); HashMap options = new HashMap(); options.put("isRoot", true); - TodoDeque.registerAtEnd(new TodoElement(doc.getDocumentElement(), + todoDeque.registerAtEnd(new TodoElement(doc.getDocumentElement(), new HashMap(), options)); - TodoDeque.dispatchLoop(); + todoDeque.dispatchLoop(); } public static void main(String[] args) diff --git a/org/madore/damlengine/ElementHandler.java b/org/madore/damlengine/ElementHandler.java index 4e14756..59981ac 100644 --- a/org/madore/damlengine/ElementHandler.java +++ b/org/madore/damlengine/ElementHandler.java @@ -15,7 +15,7 @@ public abstract class ElementHandler { new HashMap()); toProcess.add(it); } - TodoDeque.registerAtStart(toProcess); + that.ownerDeque.registerAtStart(toProcess); } public void handleNodeOnly(TodoElement that) { @@ -30,10 +30,11 @@ public abstract class ElementHandler { toProcess.add(it); } } - TodoDeque.registerAtStart(toProcess); + that.ownerDeque.registerAtStart(toProcess); } public void handle(TodoElement that) { + assert(that.ownerDeque != null); handleAttributes(that); handleNodeOnly(that); } diff --git a/org/madore/damlengine/TodoDeque.java b/org/madore/damlengine/TodoDeque.java index 037723b..eb90006 100644 --- a/org/madore/damlengine/TodoDeque.java +++ b/org/madore/damlengine/TodoDeque.java @@ -5,47 +5,49 @@ import java.util.LinkedList; public final class TodoDeque { - // FIXME: having everything static is ugly as hell... maybe this - // should be instantiatable? + private LinkedList deque; - private TodoDeque() { } // Forbid instantiation - - private static LinkedList todoDeque = new LinkedList(); - - { - todoDeque = new LinkedList(); + public TodoDeque() { + deque = new LinkedList(); } - public static void registerAtEnd(TodoItem it) { - todoDeque.addLast(it); + public void registerAtEnd(TodoItem it) { + it.ownerDeque = this; + deque.addLast(it); } - public static void registerAtEnd(Collection them) { - todoDeque.addAll(them); + public void registerAtEnd(Collection them) { + for ( TodoItem it : them ) + it.ownerDeque = this; + deque.addAll(them); } - public static void registerAtStart(TodoItem it) { - todoDeque.addFirst(it); + public void registerAtStart(TodoItem it) { + it.ownerDeque = this; + deque.addFirst(it); } - public static void registerAtStart(Collection them) { - todoDeque.addAll(0, them); + public void registerAtStart(Collection them) { + for ( TodoItem it : them ) + it.ownerDeque = this; + deque.addAll(0, them); } - public static TodoItem removeNext() { - return todoDeque.poll(); + public TodoItem removeNext() { + return deque.poll(); } - public static boolean dispatchOne() { + public boolean dispatchOne() { TodoItem it = removeNext(); if ( it != null ) { + assert(it.ownerDeque == this); it.dispatch(); return true; } else return false; } - public static void dispatchLoop() { + public void dispatchLoop() { while ( dispatchOne() ) ; } diff --git a/org/madore/damlengine/TodoItem.java b/org/madore/damlengine/TodoItem.java index 677b3b1..84dfd05 100644 --- a/org/madore/damlengine/TodoItem.java +++ b/org/madore/damlengine/TodoItem.java @@ -2,6 +2,8 @@ package org.madore.damlengine; public abstract class TodoItem { + public TodoDeque ownerDeque; + public abstract void dispatch(); } -- cgit v1.2.3