db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker Identity.java
Date Mon, 05 Sep 2005 23:38:32 GMT
arminw      2005/09/05 16:38:32

  Modified:    src/java/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        Identity.java
  Log:
  changes for best performance
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.36.2.10 +42 -30    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.36.2.9
  retrieving revision 1.36.2.10
  diff -u -r1.36.2.9 -r1.36.2.10
  --- Identity.java	2 Apr 2005 17:04:32 -0000	1.36.2.9
  +++ Identity.java	5 Sep 2005 23:38:32 -0000	1.36.2.10
  @@ -21,7 +21,6 @@
   import org.apache.ojb.broker.core.proxy.IndirectionHandler;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.util.BrokerHelper;
  -import org.apache.commons.lang.builder.HashCodeBuilder;
   import org.apache.commons.lang.SystemUtils;
   import org.apache.commons.lang.ArrayUtils;
   
  @@ -68,6 +67,10 @@
   public class Identity implements Serializable
   {
   	static final long serialVersionUID = 3182285550574178710L;
  +    /**
  +     * used for hashCode calculation.
  +     */
  +    private static final int iConstant = 37;
   
       /**
        * the top-level Class of the identified object<br>
  @@ -87,6 +90,9 @@
       private Object[] m_pkValues;
   
       /*
  +    the hashcode of different objects has to be unique across different
  +    JVM and have to be the same for the same object in different JVM.
  +
       In distributed enviroments the Identity object have to recalculate the
       hashCode and toString values, because the hash code of the Class object
       differs in different JVM
  @@ -247,8 +253,7 @@
               oos.close();
               gos.close();
               bao.close();
  -            final byte[] result = bao.toByteArray();
  -            return result;
  +            return bao.toByteArray();
           }
           catch (Exception ignored)
           {
  @@ -290,10 +295,10 @@
           if (m_pkValues == null || m_pkValues.length == 0)
           {
               throw createException("OJB needs at least one primary key attribute for class:
", realObject, null);
  -
           }
  -        if(m_pkValues[0] instanceof ValueContainer)
  -            throw new OJBRuntimeException("Can't handle pk values of type "+ValueContainer.class.getName());
  +// arminw: should never happen
  +//        if(m_pkValues[0] instanceof ValueContainer)
  +//            throw new OJBRuntimeException("Can't handle pk values of type "+ValueContainer.class.getName());
       }
   
       /**
  @@ -314,22 +319,27 @@
           if(this == obj) return true;
   
           boolean result = false;
  -        if (obj instanceof org.apache.ojb.broker.Identity)
  +        if (obj instanceof Identity)
           {
               final Identity id = (Identity) obj;
  -            final Object[] otherPkValues = id.getPrimaryKeyValues();
  -
  -            result = getObjectsTopLevelClass().equals(id.getObjectsTopLevelClass())
  -                    && (m_pkValues.length == otherPkValues.length);
  -            for (int i = 0; result && i < m_pkValues.length; i++)
  +            result = m_objectsTopLevelClass.equals(id.m_objectsTopLevelClass);
  +            if(result)
               {
  -                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[])
  +                final Object[] otherPkValues = id.m_pkValues;
  +                result = m_pkValues.length == otherPkValues.length;
  +                if(result)
                   {
  -                    result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
  +                    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]);
  +                        }
  +                    }
                   }
               }
           }
  @@ -353,20 +363,22 @@
           */
           if(m_hashCode == null)
           {
  -            final HashCodeBuilder hb = new HashCodeBuilder();
  +            int iTotal = 17;
  +            Object obj;
               for (int i = 0; i < m_pkValues.length; i++)
               {
  -            	hb.append(m_pkValues[i]);
  +            	obj = m_pkValues[i];
  +                if(obj instanceof byte[])
  +                {
  +                    iTotal = iTotal * iConstant + ((byte[]) obj).length;
  +            }
  +                else
  +                {
  +                    iTotal = iTotal * iConstant + (obj != null ? obj.hashCode() : 0);
  +        }
               }
  -            /*
  -            the hashcode of different objects to be unique across different
  -            JVM and have to be the same for the same object in different JVM,
  -            so we can't use
  -            hb.append(getObjectsTopLevelClass().hashCode());
  -            because Class identity is not same in different JVM
  -            */
  -            hb.append(getObjectsTopLevelClass().getName());
  -            m_hashCode = new Integer(hb.toHashCode());
  +            iTotal = iTotal * iConstant + getObjectsTopLevelClass().hashCode();
  +            m_hashCode = new Integer(iTotal);
           }
           return m_hashCode.intValue();
       }
  
  
  

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


Mime
View raw message