You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2003/07/04 10:44:50 UTC

cvs commit: ant/src/main/org/apache/tools/ant RuntimeConfigurable.java Target.java UnknownElement.java

bodewig     2003/07/04 01:44:50

  Modified:    src/main/org/apache/tools/ant RuntimeConfigurable.java
                        Target.java UnknownElement.java
  Log:
  Reduce memory consumption by using lazy initialization.
  Speed things up a little by using unsynchronized collections.
  PR: 21296
  
  Revision  Changes    Path
  1.36      +69 -18    ant/src/main/org/apache/tools/ant/RuntimeConfigurable.java
  
  Index: RuntimeConfigurable.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/RuntimeConfigurable.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- RuntimeConfigurable.java	26 Jun 2003 08:54:28 -0000	1.35
  +++ RuntimeConfigurable.java	4 Jul 2003 08:44:50 -0000	1.36
  @@ -54,11 +54,16 @@
   
   package org.apache.tools.ant;
   
  +import java.io.Serializable;
  +import java.util.ArrayList;
  +import java.util.Collections;
   import java.util.Enumeration;
  -import java.util.Locale;
  -import java.util.Vector;
  +import java.util.HashMap;
   import java.util.Hashtable;
  -import java.io.Serializable;
  +import java.util.List;
  +import java.util.Locale;
  +import java.util.Map;
  +import java.util.NoSuchElementException;
   
   import org.xml.sax.AttributeList;
   import org.xml.sax.helpers.AttributeListImpl;
  @@ -76,7 +81,7 @@
       private String elementTag = null;
       
       /** List of child element wrappers. */
  -    private Vector children = new Vector();
  +    private List/*<RuntimeConfigurable>*/ children = null;
       
       /** The element to configure. It is only used during
        * maybeConfigure.
  @@ -94,14 +99,15 @@
        *  exact order. The following code is copied from AttributeImpl.
        *  We could also just use SAX2 Attributes and convert to SAX1 ( DOM
        *  attribute Nodes can also be stored in SAX2 Attributges )
  +     *  XXX under JDK 1.4 you can just use a LinkedHashMap for this purpose -jglick
        */
  -    private Vector attributeNames = new Vector();
  +    private List/*<String>*/ attributeNames = null;
       
       /** Map of attribute names to values */
  -    private Hashtable attributeMap = new Hashtable();
  +    private Map/*<String,String>*/ attributeMap = null;
   
       /** Text appearing within the element. */
  -    private StringBuffer characters = new StringBuffer();
  +    private StringBuffer characters = null;
       
       /** Indicates if the wrapped object has been configured */
       private boolean proxyConfigured = false;
  @@ -164,7 +170,11 @@
        * @param value the attribute's value.
        */
       public void setAttribute(String name, String value) {
  -        attributeNames.addElement(name);
  +        if (attributeNames == null) {
  +            attributeNames = new ArrayList();
  +            attributeMap = new HashMap();
  +        }
  +        attributeNames.add(name);
           attributeMap.put(name, value);
       }
   
  @@ -173,7 +183,12 @@
        * @return Attribute name to attribute value map
        */
       public Hashtable getAttributeMap() {
  -        return attributeMap;
  +        // Nobody calls this method, maybe it could just be deleted?
  +        if (attributeMap != null) {
  +            return new Hashtable(attributeMap);
  +        } else {
  +            return new Hashtable(1);
  +        }
       }
   
       /**
  @@ -194,7 +209,10 @@
        *              Must not be <code>null</code>.
        */
       public void addChild(RuntimeConfigurable child) {
  -        children.addElement(child);
  +        if (children == null) {
  +            children = new ArrayList();
  +        }
  +        children.add(child);
       }
   
       /**
  @@ -206,7 +224,7 @@
        *         list.
        */
       RuntimeConfigurable getChild(int index) {
  -        return (RuntimeConfigurable) children.elementAt(index);
  +        return (RuntimeConfigurable) children.get(index);
       }
   
       /**
  @@ -215,7 +233,21 @@
        * @since Ant 1.5.1
        */
       Enumeration getChildren() {
  -        return children.elements();
  +        if (children != null) {
  +            return Collections.enumeration(children);
  +        } else {
  +            return new EmptyEnumeration();
  +        }
  +    }
  +    
  +    static final class EmptyEnumeration implements Enumeration {
  +        public EmptyEnumeration() {}
  +        public boolean hasMoreElements() {
  +            return false;
  +        }
  +        public Object nextElement() throws NoSuchElementException {
  +            throw new NoSuchElementException();
  +        }
       }
   
       /**
  @@ -225,7 +257,14 @@
        *        Should not be <code>null</code>.
        */
       public void addText(String data) {
  -        characters.append(data);
  +        if (data.length() == 0) {
  +            return;
  +        }
  +        if (characters != null) {
  +            characters.append(data);
  +        } else {
  +            characters = new StringBuffer(data);
  +        }
       }
   
       /**
  @@ -238,7 +277,13 @@
        *
        */
       public void addText(char[] buf, int start, int count) {
  -        addText(new String(buf, start, count));
  +        if (count == 0) {
  +            return;
  +        }
  +        if (characters == null) {
  +            characters = new StringBuffer(count);
  +        }
  +        characters.append(buf, start, count);
       }
   
       /** Get the text content of this element. Various text chunks are
  @@ -248,7 +293,11 @@
        * @return the text content of this element.
        */
       public StringBuffer getText() {
  -        return characters;
  +        if (characters != null) {
  +            return characters;
  +        } else {
  +            return new StringBuffer(0);
  +        }
       }
   
       /**
  @@ -317,8 +366,9 @@
           IntrospectionHelper ih =
               IntrospectionHelper.getHelper(p, target.getClass());
   
  +        if (attributeNames != null) {
           for (int i = 0; i < attributeNames.size(); i++) {
  -            String name = (String) attributeNames.elementAt(i);
  +            String name = (String) attributeNames.get(i);
               String value = (String) attributeMap.get(name);
   
               // reflect these into the target
  @@ -334,12 +384,13 @@
               }
           }
           id = (String) attributeMap.get("id");
  +        }
   
  -        if (characters.length() != 0) {
  +        if (characters != null) {
               ProjectHelper.addText(p, wrappedObject, characters.substring(0));
           }
   
  -        Enumeration enum = children.elements();
  +        Enumeration enum = getChildren();
           while (enum.hasMoreElements()) {
               RuntimeConfigurable child
                       = (RuntimeConfigurable) enum.nextElement();
  
  
  
  1.37      +29 -21    ant/src/main/org/apache/tools/ant/Target.java
  
  Index: Target.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Target.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- Target.java	10 Feb 2003 14:13:30 -0000	1.36
  +++ Target.java	4 Jul 2003 08:44:50 -0000	1.37
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -54,9 +54,12 @@
   
   package org.apache.tools.ant;
   
  +import java.util.ArrayList;
  +import java.util.Collections;
   import java.util.Enumeration;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.StringTokenizer;
  -import java.util.Vector;
   
   /**
    * Class to implement a target object with required parameters.
  @@ -72,9 +75,9 @@
       /** The "unless" condition to test on execution. */
       private String unlessCondition = "";
       /** List of targets this target is dependent on. */
  -    private Vector dependencies = new Vector(2);
  +    private List/*<String>*/ dependencies = null;
       /** Children of this target (tasks and data types). */
  -    private Vector children = new Vector(5);
  +    private List/*<Task|RuntimeConfigurable>*/ children = new ArrayList(5);
       /** Project this target belongs to. */
       private Project project;
       /** Description of this target, if any. */
  @@ -166,7 +169,7 @@
        * @param task The task to be added. Must not be <code>null</code>.
        */
       public void addTask(Task task) {
  -        children.addElement(task);
  +        children.add(task);
       }
   
       /**
  @@ -176,7 +179,7 @@
        *          Must not be <code>null</code>.
        */
       public void addDataType(RuntimeConfigurable r) {
  -        children.addElement(r);
  +        children.add(r);
       }
   
       /** 
  @@ -185,18 +188,16 @@
        * @return an array of the tasks currently within this target
        */
       public Task[] getTasks() {
  -        Vector tasks = new Vector(children.size());
  -        Enumeration enum = children.elements();
  -        while (enum.hasMoreElements()) {
  -            Object o = enum.nextElement();
  +        List tasks = new ArrayList(children.size());
  +        Iterator it = children.iterator();
  +        while (it.hasNext()) {
  +            Object o = it.next();
               if (o instanceof Task) {
  -                tasks.addElement(o);
  +                tasks.add(o);
               }
           }
           
  -        Task[] retval = new Task[tasks.size()];
  -        tasks.copyInto(retval);
  -        return retval;
  +        return (Task[])tasks.toArray(new Task[tasks.size()]);
       }
   
       /**
  @@ -206,7 +207,10 @@
        *                   Must not be <code>null</code>.
        */
       public void addDependency(String dependency) {
  -        dependencies.addElement(dependency);
  +        if (dependencies == null) {
  +            dependencies = new ArrayList(2);
  +        }
  +        dependencies.add(dependency);
       }
   
       /**
  @@ -215,7 +219,11 @@
        * @return an enumeration of the dependencies of this target
        */
       public Enumeration getDependencies() {
  -        return dependencies.elements();
  +        if (dependencies != null) {
  +            return Collections.enumeration(dependencies);
  +        } else {
  +            return new RuntimeConfigurable.EmptyEnumeration();
  +        }
       }
   
       /**
  @@ -301,9 +309,9 @@
        */
       public void execute() throws BuildException {
           if (testIfCondition() && testUnlessCondition()) {
  -            Enumeration enum = children.elements();
  -            while (enum.hasMoreElements()) {
  -                Object o = enum.nextElement();
  +            Iterator it = children.iterator();
  +            while (it.hasNext()) {
  +                Object o = it.next();
                   if (o instanceof Task) {
                       Task task = (Task) o;
                       task.perform();
  @@ -352,7 +360,7 @@
       void replaceChild(Task el, RuntimeConfigurable o) {
           int index;
           while ((index = children.indexOf(el)) >= 0) {
  -            children.setElementAt(o, index);
  +            children.set(index, o);
           }
       }
   
  @@ -367,7 +375,7 @@
       void replaceChild(Task el, Task o) {
           int index;
           while ((index = children.indexOf(el)) >= 0) {
  -            children.setElementAt(o, index);
  +            children.set(index, o);
           }
       }
   
  
  
  
  1.52      +13 -5     ant/src/main/org/apache/tools/ant/UnknownElement.java
  
  Index: UnknownElement.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/UnknownElement.java,v
  retrieving revision 1.51
  retrieving revision 1.52
  diff -u -r1.51 -r1.52
  --- UnknownElement.java	26 Jun 2003 08:54:28 -0000	1.51
  +++ UnknownElement.java	4 Jul 2003 08:44:50 -0000	1.52
  @@ -54,8 +54,10 @@
   
   package org.apache.tools.ant;
   
  +import java.util.ArrayList;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.Locale;
  -import java.util.Vector;
   import java.io.IOException;
   
   /**
  @@ -87,7 +89,7 @@
       /**
        * List of child elements (UnknownElements).
        */
  -    private Vector children = new Vector();
  +    private List/*<UnknownElement>*/ children = null;
   
       /**
        * Creates an UnknownElement for the given element name.
  @@ -281,7 +283,10 @@
        * @param child The child element to add. Must not be <code>null</code>.
        */
       public void addChild(UnknownElement child) {
  -        children.addElement(child);
  +        if (children == null) {
  +            children = new ArrayList();
  +        }
  +        children.add(child);
       }
   
       /**
  @@ -307,9 +312,11 @@
           Class parentClass = parent.getClass();
           IntrospectionHelper ih = IntrospectionHelper.getHelper(parentClass);
   
  -        for (int i = 0;  i < children.size(); i++) {
  +        if (children != null) {
  +        Iterator it = children.iterator();
  +        for (int i = 0; it.hasNext(); i++) {
               RuntimeConfigurable childWrapper = parentWrapper.getChild(i);
  -            UnknownElement child = (UnknownElement) children.elementAt(i);
  +            UnknownElement child = (UnknownElement) it.next();
               
               // backwards compatibility - element names of nested
               // elements have been all lower-case in Ant, except for
  @@ -326,6 +333,7 @@
                       container.addTask(child);
                   }
               }
  +        }
           }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org