sebb        2004/02/21 14:03:09

  Modified:    src/core/org/apache/jmeter/testelement/property
                        AbstractProperty.java
  Log:
  Prevent creation of properties with a null name;

  re-implement equals to take account of name as well as value;

  add suitable hashCode() method
  
  Revision  Changes    Path
  1.16      +45 -11    
jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java
  
  Index: AbstractProperty.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- AbstractProperty.java     13 Feb 2004 02:21:38 -0000      1.15
  +++ AbstractProperty.java     21 Feb 2004 22:03:09 -0000      1.16
  @@ -55,6 +55,8 @@
   
       public AbstractProperty(String name)
       {
  +        if (name == null) 
  +            throw new IllegalArgumentException("Name cannot be null");
           this.name = name;
       }
   
  @@ -93,6 +95,8 @@
   
       public void setName(String name)
       {
  +        if (name == null) 
  +            throw new IllegalArgumentException("Name cannot be null");
           this.name = name;
       }
   
  @@ -232,12 +236,38 @@
           return Boolean.valueOf(val).booleanValue();
       }
   
  -    public boolean equals(Object o) //TODO probably ought to provide hashCode() as 
well
  -    {
  -        return compareTo(o) == 0;
  +    /**
  +     * Determines if the two objects are equal by comparing names and values
  +     *
  +     * @return true if names are equal and values are equal (or both null)
  +     */
  +    public boolean equals(Object o)
  +    {
  +     if (!(o instanceof JMeterProperty)) return false;
  +     if (this == o) return true;
  +     JMeterProperty jpo = (JMeterProperty) o;
  +     if (!name.equals(jpo.getName())) return false; 
  +     String s1 = getStringValue(); 
  +     String s2 = jpo.getStringValue();
  +             return s1 == null ? s2 == null : s1.equals(s2);
       }
  +    
  +     public int hashCode()
  +     {
  +             int result = 17;
  +             result = result * 37 + name.hashCode();// name cannot be null 
  +             String s = getStringValue();
  +             result = result * 37 + (s == null ? 0 : s.hashCode()); 
  +             return result;
  +     }
   
  -    /* (non-Javadoc)
  +    /**
  +     * Compares two JMeterProperty object values.
  +     * N.B. Does not compare names
  +     * 
  +     * @param arg0 JMeterProperty to compare against
  +     * @return 0 if equal values or both values null;
  +     * -1 otherwise
        * @seeComparable#compareTo(Object)
        */
       public int compareTo(Object arg0)
  @@ -249,12 +279,13 @@
               // any other value.  Log a warning so we can try to find the root
               // cause of the null value.
               String val = getStringValue();
  +            String val2 = ((JMeterProperty)arg0).getStringValue();
               if (val == null)
               {
                   log.warn(
                       "Warning: Unexpected null value for property: " + name);
                   
  -                if (((JMeterProperty)arg0).getStringValue() == null)
  +                if (val2 == null)
                   {
                       // Two null values -- return equal
                       return 0;
  @@ -264,9 +295,7 @@
                       return -1;
                   }
               }
  -            
  -            return getStringValue().compareTo(
  -                ((JMeterProperty) arg0).getStringValue());
  +            return val.compareTo(val2);
           }
           else
           {
  @@ -397,6 +426,11 @@
           }
       }
   
  +    /**
  +     * Provides a string representation of the property.
  +     * 
  +     * @return a string consisting of the name and its value
  +     */
       public String toString()
       {
           return getStringValue();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to