You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/11/29 19:45:41 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/common PropertyName.java

luetzkendorf    2004/11/29 10:45:41

  Modified:    src/share/org/apache/slide/common PropertyName.java
  Log:
  new PropertyName instance pooling
  
  Revision  Changes    Path
  1.4       +60 -38    jakarta-slide/src/share/org/apache/slide/common/PropertyName.java
  
  Index: PropertyName.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/PropertyName.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PropertyName.java	28 Jul 2004 09:38:17 -0000	1.3
  +++ PropertyName.java	29 Nov 2004 18:45:41 -0000	1.4
  @@ -23,6 +23,9 @@
   package org.apache.slide.common;
   
   // import list
  +import java.util.HashMap;
  +import java.util.Map;
  +
   import org.apache.slide.content.NodeProperty;
   
   /**
  @@ -48,6 +51,7 @@
        * Creates a PropertyName within the {@link NodeProperty#DEFAULT_NAMESPACE
        * default namespace}.
        *
  +     * @deprecated use {@link #getPropertyName(String, String)}
        * @param      name       the name of the Property.
        */
       public PropertyName(String name) {
  @@ -57,12 +61,45 @@
       /**
        * Creates a PropertyName.
        *
  +     * @deprecated use {@link #getPropertyName(String, String)}
        * @param      name       the name of the Property.
        * @param      namespace  the namespace of the Property.
        */
       public PropertyName(String name, String namespace) {
  -        this.name = name;
  -        this.namespace = namespace;
  +        this.name = name.intern();
  +        this.namespace = namespace.intern();
  +    }
  +    
  +    
  +    private static Map instances = new HashMap();
  +    
  +    /**
  +     * Factory method.
  +     * @param name the properties name 
  +     * @param namespace the properties namespace
  +     * @return a property name object 
  +     */
  +    public static PropertyName getPropertyName(String name, String namespace) {
  +        if (name == null) throw new NullPointerException();
  +        if (namespace == null) throw new NullPointerException();
  +        
  +        Map namespaceMap = (Map)instances.get(namespace);
  +        if (namespaceMap == null) {
  +            namespaceMap = new HashMap();
  +            instances.put(namespace.intern(), namespaceMap);
  +        }
  +        
  +        PropertyName result = (PropertyName)namespaceMap.get(name);
  +        if (result == null) {
  +            result = new PropertyName(name.intern(), namespace.intern());
  +            namespaceMap.put(result.getName(), result);
  +        }
  +        
  +        return result;
  +    }
  +    
  +    public static PropertyName getPropertyName(String name) {
  +        return getPropertyName(name, NodeProperty.DEFAULT_NAMESPACE);
       }
       
       /**
  @@ -93,23 +130,17 @@
        */
       public boolean equals(Object other) {
           
  -        boolean equal = false;
  -        if (other instanceof PropertyName) {
  -            PropertyName otherPropertyName = (PropertyName)other;
  -            if (getName() == null) {
  -                equal = (otherPropertyName.getName() == null);
  -            }
  -            else {
  -                equal = getName().equals(otherPropertyName.getName());
  -            }
  -            if (getNamespace() == null) {
  -                equal &= (otherPropertyName.getNamespace() == null);
  -            }
  -            else {
  -                equal &= getNamespace().equals(otherPropertyName.getNamespace());
  -            }
  +        if (this == other) {
  +            return true;
           }
  -        return equal;
  +        
  +       if (other instanceof PropertyName) {
  +           PropertyName that = (PropertyName)other;
  +           
  +           return this.name == that.name && this.namespace == that.namespace;
  +       }
  +       
  +       return false;
       }
       
       /**
  @@ -121,12 +152,8 @@
       public int hashCode() {
           
           int hash = 0;
  -        if (getName() != null) {
  -            hash = getName().hashCode();
  -        }
  -        if (getNamespace() != null) {
  -            hash += 13 * getNamespace().hashCode();
  -        }
  +        hash = this.name.hashCode();
  +        hash += 13 * this.namespace.hashCode();
           return hash;
       }
       
  @@ -136,17 +163,12 @@
        * @return     a String representation of the PropertyName.
        */
       public String toString() {
  -        if (getNamespace() == null) {
  -            return getName();
  -        }
  -        else {
  -            StringBuffer buffer = new StringBuffer(getNamespace());
  -            buffer.append(":");
  -            if (getName() != null) {
  -                buffer.append(getName());
  -            }
  -            return buffer.toString();
  +        StringBuffer buffer = new StringBuffer(getNamespace());
  +        buffer.append(":");
  +        if (getName() != null) {
  +            buffer.append(getName());
           }
  +        return buffer.toString();
       }
   }
   
  
  
  

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