You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/02/17 17:39:34 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/core ValueContainer.java

arminw      2004/02/17 08:39:34

  Modified:    src/java/org/apache/ojb/broker Identity.java
               src/java/org/apache/ojb/broker/util JdbcTypesHelper.java
               src/java/org/apache/ojb/broker/metadata JdbcType.java
               src/java/org/apache/ojb/broker/core ValueContainer.java
  Log:
  improve equals and hashCode methods
  
  Revision  Changes    Path
  1.32      +25 -37    db-ojb/src/java/org/apache/ojb/broker/Identity.java
  
  Index: Identity.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/Identity.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- Identity.java	8 Feb 2004 13:19:47 -0000	1.31
  +++ Identity.java	17 Feb 2004 16:39:34 -0000	1.32
  @@ -60,6 +60,7 @@
   import org.apache.ojb.broker.core.ValueContainer;
   import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.ProxyHelper;
  +import org.apache.commons.lang.builder.HashCodeBuilder;
   
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
  @@ -303,34 +304,30 @@
        * Compare this Identity object to any other object. This comparison is delegated
        * to the super-class.
        */
  -    public boolean equals(Object o)
  +    public boolean equals(Object obj)
       {
  -        boolean result;
  +        if(this == obj) return true;
   
  -        if (o instanceof org.apache.ojb.broker.Identity)
  +        boolean result = false;
  +        if (obj instanceof org.apache.ojb.broker.Identity)
           {
  -            Identity id = (Identity) o;
  -            Object otherPkValues[] = id.getPrimaryKeyValues();
  +            Identity id = (Identity) obj;
  +            Object[] otherPkValues = id.getPrimaryKeyValues();
   
  -            result = getObjectsTopLevelClass().equals(id.getObjectsTopLevelClass());
  -            result = result && (m_pkValues.length == otherPkValues.length);
  +            result = getObjectsTopLevelClass().equals(id.getObjectsTopLevelClass())
  +                    && (m_pkValues.length == otherPkValues.length);
               for (int i = 0; result && i < m_pkValues.length; i++)
               {
                   result = (m_pkValues[i] == null) ? (otherPkValues[i] == null)
                           : m_pkValues[i].equals(otherPkValues[i]);
  -                        
  +
                   // special treatment for byte[]
  -				if (!result && m_pkValues[i] instanceof byte[] && otherPkValues[i] instanceof byte[])
  -				{
  -					result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
  -				}
  -			}
  -        }
  -        else
  -        {
  -            result = super.equals(o);
  +                if (!result && m_pkValues[i] instanceof byte[] && otherPkValues[i] instanceof byte[])
  +                {
  +                    result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
  +                }
  +            }
           }
  -
           return result;
       }
   
  @@ -344,30 +341,21 @@
        */
       public int hashCode()
       {
  -        // arminw:
  -        // identity is quasi immutable, thus
  -        // we can note hashCode
  +        /*
  +        arminw:
  +        identity is quasi immutable (toplevel class and PK fields
  +        never change), thus we can note hashCode
  +        */
           if(m_hashCode == null)
           {
  -            int result = getObjectsTopLevelClass().hashCode();
  +            HashCodeBuilder hb = new HashCodeBuilder();
  +            hb.append(getObjectsTopLevelClass().hashCode());
   
               for (int i = 0; i < m_pkValues.length; i++)
               {
  -            	// special treatment for computing byte[] hashcode
  -				if (m_pkValues[i] != null && m_pkValues[i] instanceof byte[])
  -				{
  -					byte[] bytes = (byte[]) m_pkValues[i];
  -					for (int j =0; j < bytes.length; j++)
  -					{
  -						result ^= bytes[j];
  -					}
  -				}
  -				else if (m_pkValues[i] != null)
  -				{
  -					result ^= m_pkValues[i].hashCode();
  -				}
  +            	hb.append(m_pkValues[i]);
               }
  -            m_hashCode = new Integer(result);
  +            m_hashCode = new Integer(hb.toHashCode());
           }
           return m_hashCode.intValue();
       }
  
  
  
  1.7       +13 -2     db-ojb/src/java/org/apache/ojb/broker/util/JdbcTypesHelper.java
  
  Index: JdbcTypesHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/JdbcTypesHelper.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JdbcTypesHelper.java	11 Feb 2004 19:43:00 -0000	1.6
  +++ JdbcTypesHelper.java	17 Feb 2004 16:39:34 -0000	1.7
  @@ -244,6 +244,17 @@
           */
           // abstract Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException;
   
  +        public boolean equals(Object obj)
  +        {
  +            if(this == obj) return true;
  +            boolean result = false;
  +            if(obj instanceof JdbcType)
  +            {
  +                result = this.getType() == ((JdbcType)obj).getType();
  +            }
  +            return result;
  +        }
  +
           public Object getObjectFromColumn(CallableStatement stmt, int columnId) throws SQLException
           {
               return getObjectFromColumn(null, stmt, null, columnId);
  @@ -279,7 +290,7 @@
   
           public String toString()
           {
  -            return super.toString() + "[" + getJdbcTypeAsString(getType()) + "JDBC TYPE]";
  +            return super.toString() + "[" + getJdbcTypeAsString(getType()) + " JDBC TYPE]";
           }
   
   //      // not used in code, but maybe useful in further versions
  
  
  
  1.4       +6 -1      db-ojb/src/java/org/apache/ojb/broker/metadata/JdbcType.java
  
  Index: JdbcType.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/JdbcType.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JdbcType.java	7 Jan 2004 11:41:18 -0000	1.3
  +++ JdbcType.java	17 Feb 2004 16:39:34 -0000	1.4
  @@ -58,6 +58,11 @@
        */
       public int getType();
   
  +    /**
  +     * Indicates whether some other object is "equal to" this one.
  +     */
  +    public boolean equals(Object obj);
  +
   
   //      // not used in code, but maybe useful in further versions
   //    /**
  
  
  
  1.5       +22 -13    db-ojb/src/java/org/apache/ojb/broker/core/ValueContainer.java
  
  Index: ValueContainer.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/ValueContainer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ValueContainer.java	16 Feb 2004 20:54:13 -0000	1.4
  +++ ValueContainer.java	17 Feb 2004 16:39:34 -0000	1.5
  @@ -1,10 +1,12 @@
   package org.apache.ojb.broker.core;
   
   import org.apache.ojb.broker.metadata.JdbcType;
  +import org.apache.commons.lang.builder.EqualsBuilder;
  +import org.apache.commons.lang.builder.HashCodeBuilder;
   
   /**
    * Encapsulates a persistent object field value and its associated
  - * {@link org.apache.ojb.broker.metadata.FieldDescriptor}.
  + * {@link JdbcType}.
    * <br/>
    * For internal use only!
    * <br/>
  @@ -49,28 +51,35 @@
   
       public boolean equals(Object obj)
       {
  -        if (obj == this)
  +        if(obj == this) return true;
  +        boolean result = false;
  +        if(obj instanceof ValueContainer)
           {
  -            return true;
  +            ValueContainer container = (ValueContainer) obj;
  +            // if jdbcType was null, we can't compare
  +            result = this.m_jdbcType != null ? this.m_jdbcType.equals(container.getJdbcType()) : false;
  +            if(result)
  +            {
  +                result = this.m_value == null ? null == container.getValue() : this.m_value.equals(container.getValue());
  +                result = new EqualsBuilder().append(this.m_value, container.getValue()).isEquals();
  +            }
           }
  -        if (!(obj instanceof ValueContainer))
  -        {
  -            return false;
  -        }
  -
  -        return m_value.equals(((ValueContainer)obj).m_value);
  +        return result;
       }
   
       public int hashCode()
       {
  -        return m_value.hashCode();
  +//        int hash =  m_value != null ? m_value.hashCode() : 0;
  +//        hash += m_jdbcType != null ? m_jdbcType.hashCode() : 0;
  +//        return hash;
  +        return new HashCodeBuilder().append(m_jdbcType).append(m_value).toHashCode();
       }
  -    
  +
       public String toString()
       {
           return this.getClass().getName() + "[jdbcType: "
           + m_jdbcType
           + ", value: " + m_value + "]";
       }
  -    
  +
   }
  
  
  

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