Revision: 937
Author: allain.lalonde
Date: Wed Jan 20 10:01:15 2010
Log: Fully generified(?) core. Performance tests have not suffered measurably.

Also, I've made nodes without children use a common immutable empty collection for their children. Makes code flow more cleanly. No if (children != null && children.size() > 0) stuff. You can always assume that children is not null.
http://code.google.com/p/piccolo2d/source/detail?r=937

Modified:
/piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCamera.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCanvas.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PLayer.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PRoot.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PObjectOutputStream.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPaintContext.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPickPath.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PStack.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PUtil.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPCamera.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPInputEventListener.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPropertyChangeListener.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PCameraTest.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PLayerTest.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PNodeTest.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/SerializationTest.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/event/MockPBasicInputEventHandler.java

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCamera.java Tue Jan 19 12:49:37 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCamera.java Wed Jan 20 10:01:15 2010
@@ -28,6 +28,8 @@
  */
 package org.piccolo2d;

+import static org.piccolo2d.util.PUtil.reverse;
+
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Graphics2D;
@@ -39,7 +41,6 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;

 import org.piccolo2d.activities.PTransformActivity;
@@ -121,7 +122,7 @@
     private transient PComponent component;

     /** List of layers viewed by this camera. */
-    private transient List/*<PLayer>*/ layers;
+    private transient List<PLayer> layers;

     /**
      * Transform applied to layers before they are rendered. This transform
@@ -143,7 +144,7 @@
     public PCamera() {
         super();
         viewTransform = new PAffineTransform();
-        layers = new ArrayList/*<PLayer>*/();
+        layers = new ArrayList<PLayer>();
         viewConstraint = VIEW_CONSTRAINT_NONE;
     }

@@ -215,7 +216,7 @@
      *
      * @return the list of layers viewed by this camera
      */
-    public List/*<PLayer>*/ getLayersReference() {
+    public List<PLayer> getLayersReference() {
         return layers;
     }

@@ -377,7 +378,8 @@
             final Graphics2D g2 = paintContext.getGraphics();
paintContext.setRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
             g2.setStroke(new BasicStroke(0));
-            final ArrayList nodes = new ArrayList();
+            final ArrayList<PNode> nodes = new ArrayList<PNode>();
+
             final PBounds nodeBounds = new PBounds();

             final Color boundsColor = Color.red;
@@ -388,11 +390,7 @@
                 ((PLayer) layers.get(i)).getAllNodes(null, nodes);
             }

-            final Iterator i = getAllNodes(null, nodes).iterator();
-
-            while (i.hasNext()) {
-                final PNode each = (PNode) i.next();
-
+            for (PNode each :  getAllNodes(null, nodes)) {
                 if (PDebug.debugBounds) {
                     g2.setPaint(boundsColor);
                     nodeBounds.setRect(each.getBoundsReference());
@@ -503,9 +501,7 @@
      *    camera were picked
      */
     protected boolean pickCameraView(final PPickPath pickPath) {
-        final int size = layers.size();
-        for (int i = size - 1; i >= 0; i--) {
-            final PLayer each = (PLayer) layers.get(i);
+        for (PLayer each : reverse(layers)) {
             if (each.fullPick(pickPath)) {
                 return true;
             }
@@ -953,16 +949,16 @@
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
         in.defaultReadObject();

-        layers = new ArrayList();
+        layers = new ArrayList<PLayer>();

         while (true) {
-            final Object each = in.readObject();
+            final Object each =  in.readObject();
             if (each != null) {
                 if (each.equals(Boolean.FALSE)) {
                     break;
                 }
                 else {
-                    layers.add(each);
+                    layers.add((PLayer)each);
                 }
             }
         }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCanvas.java Tue Jan 19 12:49:37 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PCanvas.java Wed Jan 20 10:01:15 2010
@@ -92,7 +92,7 @@
* Stack of cursors used to keep track of cursors as they change through
      * interactions.
      */
-    private final PStack cursorStack;
+    private final PStack<Cursor> cursorStack;

     /**
* Whether the canvas is considered to be interacting, will probably mean
@@ -144,7 +144,7 @@
      * camera, and layer. Zooming and panning are automatically installed.
      */
     public PCanvas() {
-        cursorStack = new PStack();
+        cursorStack = new PStack<Cursor>();
         setCamera(createDefaultCamera());
         setDefaultRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
         setAnimatingRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
@@ -428,7 +428,7 @@
      */
     public void popCursor() {
         if (!cursorStack.isEmpty()) {
-            setCursor((Cursor) cursorStack.pop());
+            setCursor(cursorStack.pop());
         }
     }

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PLayer.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PLayer.java Wed Jan 20 10:01:15 2010
@@ -31,7 +31,8 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;

 import org.piccolo2d.util.PBounds;
@@ -82,14 +83,15 @@
     /**
      * Cameras which are registered as viewers of this PLayer.
      */
-    private transient List cameras;
+    private transient List<PCamera> cameras;
+
+ private static final List<PCamera> EMPTY_CAMERAS = Collections.<PCamera>emptyList();

     /**
      * Creates a PLayer without any cameras attached to it.
      */
-    public PLayer() {
-        super();
-        cameras = new ArrayList();
+    public PLayer() {
+        cameras = EMPTY_CAMERAS;
     }

     // ****************************************************************
@@ -102,7 +104,7 @@
      *
      * @return direct reference to registered cameras
      */
-    public List getCamerasReference() {
+    public List<PCamera> getCamerasReference() {
         return cameras;
     }

@@ -111,10 +113,7 @@
      *
      * @return the number of cameras attached to this layer
      */
-    public int getCameraCount() {
-        if (cameras == null) {
-            return 0;
-        }
+    public int getCameraCount() {
         return cameras.size();
     }

@@ -125,7 +124,7 @@
      * @return camera at the given index
      */
     public PCamera getCamera(final int index) {
-        return (PCamera) cameras.get(index);
+        return cameras.get(index);
     }

     /**
@@ -146,6 +145,9 @@
      * @param camera Camera to add to layer
      */
     public void addCamera(final int index, final PCamera camera) {
+        if (cameras == EMPTY_CAMERAS) {
+            cameras = new LinkedList<PCamera>();
+        }
         cameras.add(index, camera);
         invalidatePaint();
firePropertyChange(PROPERTY_CODE_CAMERAS, PROPERTY_CAMERAS, null, cameras);
@@ -211,10 +213,8 @@
* @param parentBounds bounds needing repainting in parent coordinate system
      */
     protected void notifyCameras(final PBounds parentBounds) {
-        final int count = getCameraCount();
-        for (int i = 0; i < count; i++) {
-            final PCamera each = (PCamera) cameras.get(i);
-            each.repaintFromLayer(parentBounds, this);
+        for (PCamera camera : cameras) {
+            camera.repaintFromLayer(parentBounds, this);
         }
     }

@@ -240,9 +240,8 @@
         }
         out.defaultWriteObject();

-        final int count = getCameraCount();
-        for (int i = 0; i < count; i++) {
- ((PObjectOutputStream) out).writeConditionalObject(cameras.get(i));
+        for (PCamera camera : cameras) {
+            ((PObjectOutputStream) out).writeConditionalObject(camera);
         }

         out.writeObject(Boolean.FALSE);
@@ -260,7 +259,7 @@
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
         in.defaultReadObject();

-        cameras = new ArrayList();
+        cameras = new LinkedList<PCamera>();

         while (true) {
             final Object each = in.readObject();
@@ -269,7 +268,7 @@
                     break;
                 }
                 else {
-                    cameras.add(each);
+                    cameras.add((PCamera)each);
                 }
             }
         }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Wed Jan 20 06:37:10 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Wed Jan 20 10:01:15 2010
@@ -28,6 +28,8 @@
  */
 package org.piccolo2d;

+import static org.piccolo2d.util.PUtil.reverse;
+
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
@@ -59,6 +61,7 @@
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.EventListener;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;

@@ -384,6 +387,19 @@
      */
     public static final int FILL_STRATEGY_EXACT_FIT = 4;

+ /** A utility enumeration with no elements for use with ClientProperties. */ + private static final Enumeration<?> NULL_ENUMERATION = new Enumeration<Object>() {
+        public boolean hasMoreElements() {
+            return false;
+        }
+
+        public Object nextElement() {
+            return null;
+        }
+    };
+
+ private static final List<PNode> EMPTY_CHILDREN = Collections.<PNode>emptyList();
+
     /**
      * Creates a new PNode with the given name.
      *
@@ -409,6 +425,8 @@
         pickable = true;
         childrenPickable = true;
         visible = true;
+
+        children = EMPTY_CHILDREN;
     }

     // ****************************************************************
@@ -749,7 +767,7 @@
      */
     public Enumeration<?> getClientPropertyKeysEnumeration() {
         if (clientProperties == null) {
-            return PUtil.NULL_ENUMERATION;
+            return NULL_ENUMERATION;
         }
         else {
             return clientProperties.getAttributeNames();
@@ -1654,10 +1672,8 @@
             resultBounds.resetToZero();
         }

-        if (children != null) {
-            for (PNode each : children) {
-                resultBounds.add(each.getFullBoundsReference());
-            }
+        for (PNode each : children) {
+            resultBounds.add(each.getFullBoundsReference());
         }

         return resultBounds;
@@ -1800,10 +1816,8 @@
         setBoundsChanged(true);
firePropertyChange(PROPERTY_CODE_BOUNDS, PROPERTY_BOUNDS, null, bounds);

-        if (children != null) {
-            for (PNode each : children) {
-                each.parentBoundsChanged();
-            }
+        for (PNode each : children) {
+            each.parentBoundsChanged();
         }
     }

@@ -1871,10 +1885,8 @@
             if (childBoundsInvalid || childBoundsVolatile) {
                 childBoundsVolatile = false;

-                if (children != null) {
-                    for (PNode each : children) {
-                        childBoundsVolatile |= each.validateFullBounds();
-                    }
+                for (PNode each : children) {
+                    childBoundsVolatile |= each.validateFullBounds();
                 }
             }

@@ -2540,10 +2552,8 @@
         }

         if (getChildPaintInvalid()) {
-            if (children != null) {
-                for (PNode each : children) {
-                    each.validateFullPaint();
-                }
+            for (PNode each : children) {
+                each.validateFullPaint();
             }

             setChildPaintInvalid(false);
@@ -2746,10 +2756,8 @@
                 paint(paintContext);
             }

-            if (children != null) {
-                for (PNode each : children) {
-                    each.fullPaint(paintContext);
-                }
+            for (PNode each : children) {
+                each.fullPaint(paintContext);
             }

             paintAfterChildren(paintContext);
@@ -3073,9 +3081,7 @@
             }

             if (getChildrenPickable()) {
-                final int count = getChildrenCount();
-                for (int i = count - 1; i >= 0; i--) {
-                    final PNode each = (PNode) children.get(i);
+                for (PNode each : reverse(children)) {
                     if (each.fullPick(pickPath)) {
                         return true;
                     }
@@ -3108,9 +3114,7 @@
                 results.add(this);
             }

-            final int count = getChildrenCount();
-            for (int i = count - 1; i >= 0; i--) {
-                final PNode each = (PNode) children.get(i);
+            for (PNode each : reverse(children)) {
                 each.findIntersectingNodes(localBounds, results);
             }
         }
@@ -3170,6 +3174,9 @@
         }

         child.setParent(this);
+        if (children == Collections.EMPTY_LIST) {
+            children = new LinkedList<PNode>();
+        }
         getChildrenReference().add(index, child);
         child.invalidatePaint();
         invalidateFullBounds();
@@ -3315,9 +3322,6 @@
      * @return index of child or -1 if not found
      */
     public int indexOfChild(final PNode child) {
-        if (children == null) {
-            return -1;
-        }
         return children.indexOf(child);
     }

@@ -3346,13 +3350,10 @@
      * @return the removed child
      */
     public PNode removeChild(final int index) {
-        if (children == null) {
-            return null;
-        }
         final PNode child = (PNode) children.remove(index);

         if (children.size() == 0) {
-            children = null;
+            children = EMPTY_CHILDREN;
         }

         child.repaint();
@@ -3381,17 +3382,15 @@
      * efficient then removing each child individually.
      */
     public void removeAllChildren() {
-        if (children != null) {
-            for (PNode each : children) {
-                each.setParent(null);
-            }
-            children = null;
-
-            invalidatePaint();
-            invalidateFullBounds();
-
- firePropertyChange(PROPERTY_CODE_CHILDREN, PROPERTY_CHILDREN, null, children);
-        }
+        for (PNode each : children) {
+            each.setParent(null);
+        }
+        children = EMPTY_CHILDREN;
+
+        invalidatePaint();
+        invalidateFullBounds();
+
+ firePropertyChange(PROPERTY_CODE_CHILDREN, PROPERTY_CHILDREN, null, children);
     }

     /**
@@ -3467,9 +3466,6 @@
      * @return the number of children
      */
     public int getChildrenCount() {
-        if (children == null) {
-            return 0;
-        }
         return children.size();
     }

@@ -3490,9 +3486,6 @@
      * @return reference to the children list
      */
     public List<PNode> getChildrenReference() {
-        if (children == null) {
-            children = new ArrayList<PNode>();
-        }
         return children;
     }

@@ -3502,10 +3495,7 @@
      * @return iterator over this nodes children
      */
     public ListIterator<PNode> getChildrenIterator() {
-        if (children == null) {
-            return Collections.<PNode>emptyList().listIterator();
-        }
- return Collections.<PNode>unmodifiableList(children).listIterator(); + return Collections.<PNode> unmodifiableList(children).listIterator();
     }

     /**
@@ -3556,10 +3546,8 @@
         }

         if (filter == null || filter.acceptChildrenOf(this)) {
-            if (children != null) {
-                for (PNode each : children) {
-                    each.getAllNodes(filter, results);
-                }
+            for (PNode each : children) {
+                each.getAllNodes(filter, results);
             }
         }

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PRoot.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PRoot.java Wed Jan 20 10:01:15 2010
@@ -30,7 +30,6 @@

 import java.awt.event.ActionListener;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;

 import javax.swing.SwingUtilities;
@@ -41,7 +40,6 @@
 import org.piccolo2d.util.PDebug;
 import org.piccolo2d.util.PNodeFilter;

-
 /**
* <b>PRoot</b> serves as the top node in Piccolo2D's runtime structure. The
  * PRoot responsible for running the main UI loop that processes input from
@@ -78,7 +76,7 @@
     /**
* The property name that identifies a change in this node's interacting
      * state.
-     *
+     *
      * @since 1.3
      */
public static final String PROPERTY_INTERACTING_CHANGED = "INTERACTING_CHANGED_NOTIFICATION";
@@ -86,7 +84,7 @@
     /**
* The property code that identifies a change in this node's interacting
      * state.
-     *
+     *
      * @since 1.3
      */
     public static final int PROPERTY_CODE_INTERACTING_CHANGED = 1 << 13;
@@ -106,7 +104,7 @@
     private transient PInputManager defaultInputManager;

     /** The Input Sources that are registered to this node. */
-    private final transient List inputSources;
+    private final transient List<InputSource> inputSources;

     /**
* Used to provide a consistent clock time to activities as they are being
@@ -130,7 +128,7 @@
      */
     public PRoot() {
         super();
-        inputSources = new ArrayList();
+        inputSources = new ArrayList<InputSource>();
         globalTime = System.currentTimeMillis();
         activityScheduler = new PActivityScheduler(this);
     }
@@ -173,10 +171,8 @@

         while (activityScheduler.getActivitiesReference().size() > 0) {
             processInputs();
- final Iterator i = getAllNodes(cameraWithCanvas, null).iterator();
-            while (i.hasNext()) {
-                final PCamera each = (PCamera) i.next();
-                each.getComponent().paintImmediately();
+            for (PNode each : getAllNodes(cameraWithCanvas, null)) {
+                ((PCamera) each).getComponent().paintImmediately();
             }
         }
     }
@@ -322,12 +318,8 @@
         processingInputs = true;

         globalTime = System.currentTimeMillis();
-        if (inputSources.size() > 0) {
-            final Iterator inputSourceIterator = inputSources.iterator();
-            while (inputSourceIterator.hasNext()) {
- final InputSource each = (InputSource) inputSourceIterator.next();
-                each.processInput();
-            }
+        for (InputSource inputSource : inputSources) {
+            inputSource.processInput();
         }

         activityScheduler.processActivities(globalTime);
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java Wed Jan 20 10:01:15 2010
@@ -38,7 +38,7 @@

 import org.piccolo2d.PRoot;
 import org.piccolo2d.util.PUtil;
-
+import static org.piccolo2d.util.PUtil.reverse;

 /**
  * <b>PActivityScheduler</b> is responsible for maintaining a list of
@@ -58,10 +58,10 @@
     private static final long serialVersionUID = 1L;
     private transient Timer activityTimer = null;
     private final PRoot root;
-    private final List activities;
+    private final List<PActivity> activities;
     private boolean activitiesChanged;
     private boolean animating;
-    private final ArrayList processingActivities;
+    private final List<PActivity> processingActivities;

     /**
      * Constructs an instance of PActivityScheduler. All activities it will
@@ -70,10 +70,10 @@
* @param rootNode root node of all activities to be performed. All nodes
      *            being animated should have this node as an ancestor.
      */
-    public PActivityScheduler(final PRoot rootNode) {
+    public PActivityScheduler(final PRoot rootNode) {
         root = rootNode;
-        activities = new ArrayList();
-        processingActivities = new ArrayList();
+        activities = new ArrayList<PActivity>();
+        processingActivities = new ArrayList<PActivity>();
     }

     /**
@@ -157,7 +157,7 @@
      *
      * @return reference to the current activities list.
      */
-    public List getActivitiesReference() {
+    public List<PActivity> getActivitiesReference() {
         return activities;
     }

@@ -172,8 +172,7 @@
         if (size > 0) {
             processingActivities.clear();
             processingActivities.addAll(activities);
-            for (int i = size - 1; i >= 0; i--) {
- final PActivity each = (PActivity) processingActivities.get(i);
+            for (PActivity each : reverse(processingActivities)) {
                 each.processStep(currentTime);
             }
         }
@@ -187,8 +186,7 @@
     public boolean getAnimating() {
         if (activitiesChanged) {
             animating = false;
-            for (int i = 0; i < activities.size(); i++) {
-                final PActivity each = (PActivity) activities.get(i);
+            for (PActivity each : activities) {
                 animating |= each.isAnimation();
             }
             activitiesChanged = false;
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java Tue Jan 19 12:49:37 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java Wed Jan 20 10:01:15 2010
@@ -39,6 +39,7 @@
 import java.text.AttributedCharacterIterator;
 import java.text.AttributedString;
 import java.util.ArrayList;
+import java.util.List;

 import org.piccolo2d.PNode;
 import org.piccolo2d.util.PPaintContext;
@@ -425,7 +426,8 @@
      * wrapped based on the bounds of this node.
      */
     public void recomputeLayout() {
-        final ArrayList linesList = new ArrayList();
+        final List<TextLayout> linesList = new ArrayList<TextLayout>();
+
         double textWidth = 0;
         double textHeight = 0;

@@ -470,7 +472,7 @@
             }
         }

-        lines = (TextLayout[]) linesList.toArray(EMPTY_TEXT_LAYOUT_ARRAY);
+        lines = linesList.toArray(EMPTY_TEXT_LAYOUT_ARRAY);

         if (constrainWidthToTextWidth || constrainHeightToTextHeight) {
             double newWidth = getWidth();
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PObjectOutputStream.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PObjectOutputStream.java Wed Jan 20 10:01:15 2010
@@ -32,7 +32,8 @@
 import java.io.IOException;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
-import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;

 /**
* <b>PObjectOutputStream</b> is an extension of ObjectOutputStream to handle
@@ -68,7 +69,7 @@
 public class PObjectOutputStream extends ObjectOutputStream {

     private boolean writingRoot;
-    private final HashMap unconditionallyWritten;
+    private final Set<Object> unconditionallyWritten;

     /**
      * Transform the given object into an array of bytes.
@@ -94,7 +95,7 @@
      */
     public PObjectOutputStream(final OutputStream out) throws IOException {
         super(out);
-        unconditionallyWritten = new HashMap();
+        unconditionallyWritten = new HashSet<Object>();
     }

     /**
@@ -125,7 +126,7 @@
"writeConditionalObject() may only be called when a root object has been written.");
         }

-        if (unconditionallyWritten.containsKey(object)) {
+        if (unconditionallyWritten.contains(object)) {
             writeObject(object);
         }
         else {
@@ -158,7 +159,7 @@
             }

             public Object replaceObject(final Object object) {
-                unconditionallyWritten.put(object, Boolean.TRUE);
+                unconditionallyWritten.add(object);
                 return object;
             }

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPaintContext.java Tue Jan 19 13:09:08 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPaintContext.java Wed Jan 20 10:01:15 2010
@@ -69,19 +69,19 @@
     private final Graphics2D graphics;

     /** Used while computing transparency. */
-    protected PStack compositeStack;
+    protected PStack<Composite> compositeStack;

     /** Used to optimize clipping region. */
-    protected PStack clipStack;
+    protected PStack<Shape> clipStack;

     /** Tracks clipping region in local coordinate system. */
-    protected PStack localClipStack;
+    protected PStack<Rectangle2D> localClipStack;

/** Stack of cameras through which the node being painted is being viewed. */
-    protected PStack cameraStack;
+    protected PStack<PCamera> cameraStack;

     /** Stack of transforms being applied to the drawing context. */
-    protected PStack transformStack;
+    protected PStack<AffineTransform> transformStack;

     /** The current render quality that all rendering should be done in. */
     protected int renderQuality;
@@ -93,11 +93,11 @@
      */
     public PPaintContext(final Graphics2D graphics) {
         this.graphics = graphics;
-        compositeStack = new PStack();
-        clipStack = new PStack();
-        localClipStack = new PStack();
-        cameraStack = new PStack();
-        transformStack = new PStack();
+        compositeStack = new PStack<Composite>();
+        clipStack = new PStack<Shape>();
+        localClipStack = new PStack<Rectangle2D>();
+        cameraStack = new PStack<PCamera>();
+        transformStack = new PStack<AffineTransform>();
         renderQuality = HIGH_QUALITY_RENDERING;

         Shape clip = graphics.getClip();
@@ -179,9 +179,8 @@
      *
      * @param clip clip to be pushed
      */
-    public void pushClip(final Shape clip) {
-        final Shape currentClip = graphics.getClip();
-        clipStack.push(currentClip);
+    public void pushClip(final Shape clip) {
+        clipStack.push(graphics.getClip());
         graphics.clip(clip);
         final Rectangle2D newLocalClip = clip.getBounds2D();
         Rectangle2D.intersect(getLocalClip(), newLocalClip, newLocalClip);
@@ -194,7 +193,7 @@
      * @param clip not used in this method
      */
     public void popClip(final Shape clip) {
-        final Shape newClip = (Shape) clipStack.pop();
+        final Shape newClip = clipStack.pop();
         graphics.setClip(newClip);
         localClipStack.pop();
     }
@@ -231,8 +230,8 @@
         if (transparency == 1.0f) {
             return;
         }
-        final Composite c = (Composite) compositeStack.pop();
-        graphics.setComposite(c);
+
+        graphics.setComposite(compositeStack.pop());
     }

     /**
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPickPath.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PPickPath.java Wed Jan 20 10:01:15 2010
@@ -31,7 +31,8 @@
 import java.awt.geom.Dimension2D;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
-import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;

 import javax.swing.event.EventListenerList;

@@ -40,6 +41,8 @@
 import org.piccolo2d.event.PInputEvent;
 import org.piccolo2d.event.PInputEventListener;

+import static org.piccolo2d.util.PUtil.reverse;
+

 /**
* <b>PPickPath</b> represents a ordered list of nodes that have been picked.
@@ -71,13 +74,13 @@
     private static final double[] PTS = new double[4];

     /** Stack of nodes representing all picked nodes. */
-    private PStack nodeStack;
+    private PStack<PNode> nodeStack;

     private final PCamera topCamera;
-    private PStack transformStack;
-    private PStack pickBoundsStack;
+    private PStack<PTuple> transformStack;
+    private PStack<Rectangle2D> pickBoundsStack;
     private PCamera bottomCamera;
-    private HashMap excludedNodes;
+    private Set<PNode> excludedNodes;

     /**
* Creates a pick pack originating from the provided camera and with the
@@ -88,10 +91,12 @@
      */
public PPickPath(final PCamera camera, final PBounds aScreenPickBounds) {
         super();
-        pickBoundsStack = new PStack();
+
         topCamera = camera;
-        nodeStack = new PStack();
-        transformStack = new PStack();
+
+        pickBoundsStack = new PStack<Rectangle2D>();
+        nodeStack = new PStack<PNode>();
+        transformStack = new PStack<PTuple>();
         pickBoundsStack.push(aScreenPickBounds);

         CURRENT_PICK_PATH = this;
@@ -114,7 +119,7 @@
      * @return true if node is acceptable to the path
      */
     public boolean acceptsNode(final PNode node) {
-        return excludedNodes == null || !excludedNodes.containsKey(node);
+        return excludedNodes == null || !excludedNodes.contains(node);
     }

     // ****************************************************************
@@ -168,20 +173,20 @@
         if (picked == topCamera) {
             return null;
         }
+
         if (excludedNodes == null) {
-            excludedNodes = new HashMap();
+            excludedNodes = new HashSet<PNode>();
         }

         // exclude current picked node
-        excludedNodes.put(picked, picked);
-
-        final Object screenPickBounds = pickBoundsStack.get(0);
+        excludedNodes.add(picked);
+
+        final Rectangle2D screenPickBounds = pickBoundsStack.get(0);

         // reset path state
-        pickBoundsStack = new PStack();
-        nodeStack = new PStack();
-        transformStack = new PStack();
-        pickBoundsStack = new PStack();
+        pickBoundsStack = new PStack<Rectangle2D>();
+        nodeStack = new PStack<PNode>();
+        transformStack = new PStack<PTuple>();

         pickBoundsStack.push(screenPickBounds);

@@ -221,12 +226,12 @@
     }

     private PCamera calculateBottomCamera() {
-        for (int i = nodeStack.size() - 1; i >= 0; i--) {
-            final PNode each = (PNode) nodeStack.get(i);
+        for (PNode each : reverse(nodeStack)) {
             if (each instanceof PCamera) {
                 return (PCamera) each;
             }
         }
+
         return null;
     }

@@ -235,7 +240,7 @@
      *
      * @return the node stack
      */
-    public PStack getNodeStackReference() {
+    public PStack<PNode> getNodeStackReference() {
         return nodeStack;
     }

@@ -257,11 +262,10 @@
         PTS[2] = 1;
         PTS[3] = 0;

-        final int count = transformStack.size();
-        for (int i = 0; i < count; i++) {
- final PAffineTransform each = ((PTuple) transformStack.get(i)).transform;
-            if (each != null) {
-                each.transform(PTS, 0, PTS, 0, 2);
+        for (PTuple tuple : transformStack) {
+            final PAffineTransform transform = tuple.transform;
+            if (transform != null) {
+                transform.transform(PTS, 0, PTS, 0, 2);
             }
         }

@@ -306,12 +310,11 @@
     public PAffineTransform getPathTransformTo(final PNode nodeOnPath) {
         final PAffineTransform aTransform = new PAffineTransform();

-        final int count = transformStack.size();
-        for (int i = 0; i < count; i++) {
-            final PTuple each = (PTuple) transformStack.get(i);
+        for (PTuple each : transformStack) {
             if (each.transform != null) {
                 aTransform.concatenate(each.transform);
             }
+
             if (nodeOnPath == each.node) {
                 return aTransform;
             }
@@ -329,17 +332,14 @@
      */
public void processEvent(final PInputEvent event, final int eventType) {
         event.setPath(this);
-
-        for (int i = nodeStack.size() - 1; i >= 0; i--) {
-            final PNode each = (PNode) nodeStack.get(i);
-
+
+        for (PNode each : reverse(nodeStack)) {
             final EventListenerList list = each.getListenerList();

             if (list != null) {
- final Object[] listeners = list.getListeners(PInputEventListener.class);
-
-                for (int j = 0; j < listeners.length; j++) {
- final PInputEventListener listener = (PInputEventListener) listeners[j]; + final PInputEventListener[] listeners = list.getListeners(PInputEventListener.class);
+
+                for (PInputEventListener listener : listeners) {
                     listener.processEvent(event, eventType);
                     if (event.isHandled()) {
                         return;
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PStack.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PStack.java Wed Jan 20 10:01:15 2010
@@ -38,7 +38,7 @@
  * @version 1.0
  * @author Jesse Grosjean
  */
-public class PStack extends ArrayList {
+public class PStack<T> extends ArrayList<T> {
     /**
      * Allows for future serialization code to understand versioned binary
      * formats.
@@ -56,7 +56,7 @@
      *
      * @param o object to add to the stack
      */
-    public void push(final Object o) {
+    public void push(final T o) {
         add(o);
     }

@@ -64,8 +64,8 @@
      * Returns  topmost element on the stack, or null if stack is empty.
      *
      * @return topmost element on the stack, or null if empty
-     */
-    public Object peek() {
+     */
+    public T peek() {
         final int s = size();
         if (s == 0) {
             return null;
@@ -80,7 +80,7 @@
      *
      * @return topmost element on stack.
      */
-    public Object pop() {
+    public T pop() {
         return remove(size() - 1);
     }
 }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PUtil.java Tue Jan 19 13:09:08 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PUtil.java Wed Jan 20 10:01:15 2010
@@ -36,9 +36,9 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
-import java.util.Collections;
-import java.util.Enumeration;
 import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;

 import org.piccolo2d.PCamera;
 import org.piccolo2d.PLayer;
@@ -61,26 +61,12 @@

/** Rate in milliseconds at which the activity timer will get invoked. */
     public static final int ACTIVITY_SCHEDULER_FRAME_DELAY = 10;
-
-    /** An iterator that iterates over an empty collection. */
- public static final Iterator NULL_ITERATOR = Collections.EMPTY_LIST.iterator();
-
+
     /**
* Used when persisting paths to an object stream. Used to mark the end of
      * the path.
      */
-    private static final int PATH_TERMINATOR = -1;
-
-    /** A utility enumeration with no elements. */
-    public static final Enumeration NULL_ENUMERATION = new Enumeration() {
-        public boolean hasMoreElements() {
-            return false;
-        }
-
-        public Object nextElement() {
-            return null;
-        }
-    };
+    private static final int PATH_TERMINATOR = -1;

     /**
      * Creates the simplest possible scene graph. 1 Camera, 1 Layer, 1 Root
@@ -294,4 +280,35 @@

         out.writeInt(PATH_TERMINATOR);
     }
-}
+
+    public static <T> Iterable<T> reverse(List<T> wrapped) {
+        return new ListReverser<T>(wrapped);
+    }
+
+    private static class ListReverser<T> implements Iterable<T> {
+        private ListIterator<T> listIterator;
+
+        public ListReverser(List<T> wrappedList) {
+ this.listIterator = wrappedList.listIterator(wrappedList.size());
+        }
+
+        public Iterator<T> iterator() {
+            return new Iterator<T>() {
+
+                public boolean hasNext() {
+                    return listIterator.hasPrevious();
+                }
+
+                public T next() {
+                    return listIterator.previous();
+                }
+
+                public void remove() {
+                    listIterator.remove();
+                }
+
+            };
+        }
+
+    }
+}
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPCamera.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPCamera.java Wed Jan 20 10:01:15 2010
@@ -42,7 +42,7 @@
  */
 class MockPCamera extends PCamera {
     private static final long serialVersionUID = 1L;
-    private final List notifications = new ArrayList();
+ private final List<Notification> notifications = new ArrayList<Notification>();

public void repaintFromLayer(final PBounds bounds, final PLayer layer) { notifications.add(new Notification("repaintFromLayer", bounds, layer));
@@ -75,7 +75,7 @@
     }

     public Notification getNotification(final int i) {
-        return (Notification) notifications.get(i);
+        return notifications.get(i);
     }

 }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPInputEventListener.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPInputEventListener.java Wed Jan 20 10:01:15 2010
@@ -49,7 +49,7 @@
         }
     }

-    private final List notifications = new ArrayList();
+ private final List<Notification> notifications = new ArrayList<Notification>();

     public void processEvent(final PInputEvent aEvent, final int type) {
         notifications.add(new Notification(aEvent, type));
@@ -60,7 +60,7 @@
     }

     public Notification getNotification(final int index) {
-        return (Notification) notifications.get(index);
+        return notifications.get(index);
     }

 }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPropertyChangeListener.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/MockPropertyChangeListener.java Wed Jan 20 10:01:15 2010
@@ -37,7 +37,7 @@
  * Mock PropertyChangeListener.
  */
 public class MockPropertyChangeListener implements PropertyChangeListener {
-    private final List changes = new ArrayList();
+ private final List<PropertyChangeEvent> changes = new ArrayList<PropertyChangeEvent>();

     public void propertyChange(final PropertyChangeEvent evt) {
         changes.add(evt);
@@ -48,6 +48,6 @@
     }

     public PropertyChangeEvent getPropertyChange(final int index) {
-        return (PropertyChangeEvent) changes.get(index);
+        return changes.get(index);
     }
 }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PCameraTest.java Tue Jan 19 12:49:37 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PCameraTest.java Wed Jan 20 10:01:15 2010
@@ -685,7 +685,7 @@
         }

         /** {...@inheritdoc} */
- public Collection getAllNodes(final PNodeFilter filter, final Collection nodes) { + public Collection<PNode> getAllNodes(final PNodeFilter filter, final Collection<PNode> nodes) {
             getAllNodesCalled = true;
             return super.getAllNodes(filter, nodes);
         }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PLayerTest.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PLayerTest.java Wed Jan 20 10:01:15 2010
@@ -47,7 +47,7 @@
     }

     public void testLayerHasEmptyCamerasCollectionByDefault() {
-        final Collection cameras = layer.getCamerasReference();
+        final Collection<PCamera> cameras = layer.getCamerasReference();
         assertNotNull(cameras);
         assertTrue(cameras.isEmpty());
         assertEquals(0, layer.getCameraCount());
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PNodeTest.java Wed Jan 20 06:37:10 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/PNodeTest.java Wed Jan 20 10:01:15 2010
@@ -541,7 +541,7 @@

public void testGetClientPropertyKeysEnumerationShouldReturnCorrectEnumWhenPropertiesExist() {
         node.addAttribute("Testing", "Hello");
- final Enumeration enumeration = node.getClientPropertyKeysEnumeration(); + final Enumeration<?> enumeration = node.getClientPropertyKeysEnumeration();
         assertNotNull(enumeration);
         assertTrue(enumeration.hasMoreElements());
         assertEquals("Testing", enumeration.nextElement());
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/SerializationTest.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/SerializationTest.java Wed Jan 20 10:01:15 2010
@@ -59,7 +59,7 @@
         l = (PNode) l.clone(); // copy uses serialization internally
         assertTrue(l.getChildrenCount() == 300);

-        final Iterator i = l.getChildrenIterator();
+        final Iterator<PNode> i = l.getChildrenIterator();
         while (i.hasNext()) {
             final PNode each = (PNode) i.next();
             assertEquals(l, each.getParent());
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/event/MockPBasicInputEventHandler.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/event/MockPBasicInputEventHandler.java Wed Jan 20 10:01:15 2010
@@ -37,7 +37,7 @@
  * Mock PBasicInputEventHandler.
  */
 public class MockPBasicInputEventHandler extends PBasicInputEventHandler {
-    private final ArrayList methodCalls = new ArrayList();
+    private final ArrayList<String> methodCalls = new ArrayList<String>();

     public String[] getMethodCalls() {
         final String[] result = new String[methodCalls.size()];
-- 
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to