[jboss-cvs] JBossAS SVN: r92230 - in projects/annotations/trunk/core/src: test/java/org/jboss/annotations/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Aug 11 10:09:55 EDT 2009


Author: jesper.pedersen
Date: 2009-08-11 10:09:54 -0400 (Tue, 11 Aug 2009)
New Revision: 92230

Modified:
   projects/annotations/trunk/core/src/main/java/org/jboss/annotations/Annotation.java
   projects/annotations/trunk/core/src/test/java/org/jboss/annotations/test/AnnotationRepositoryTests.java
Log:
[JBANN-21] Annotation: HashCode and Equals

Modified: projects/annotations/trunk/core/src/main/java/org/jboss/annotations/Annotation.java
===================================================================
--- projects/annotations/trunk/core/src/main/java/org/jboss/annotations/Annotation.java	2009-08-11 13:39:56 UTC (rev 92229)
+++ projects/annotations/trunk/core/src/main/java/org/jboss/annotations/Annotation.java	2009-08-11 14:09:54 UTC (rev 92230)
@@ -23,6 +23,7 @@
 package org.jboss.annotations;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.logging.Logger;
 
@@ -39,6 +40,7 @@
    private String className;
    private String memberName;
    private List<String> parameterTypes;
+   private transient Integer hashCode;
 
    /**
     * Constructor
@@ -51,6 +53,15 @@
    public Annotation(Object annotation, AnnotationType type, 
                      String className, String memberName, String[] parameterTypes)
    {
+      if (annotation == null)
+         throw new IllegalArgumentException("Annotation is null");
+
+      if (type == null)
+         throw new IllegalArgumentException("Type is null");
+
+      if (className == null)
+         throw new IllegalArgumentException("ClassName is null");
+
       this.annotation = annotation;
       this.type = type;
       this.className = className;
@@ -116,6 +127,84 @@
    }
 
    /**
+    * Hash code of the object
+    * @return The hash code
+    */
+   private int calculateHashCode()
+   {
+      int result = 7;
+      
+      result += 7 * annotation.getClass().getName().hashCode();
+      result += 7 * type.hashCode();
+      result += 7 * className.hashCode();
+      result += 7 * (memberName != null ? memberName.hashCode() : 1);
+      result += 7 * (parameterTypes != null ? parameterTypes.hashCode() : 1);
+
+      return result;
+   }
+
+   /**
+    * Hash code of the object
+    * @return The hash code
+    */
+   public int hashCode()
+   {
+      if (hashCode == null)
+         hashCode = Integer.valueOf(calculateHashCode());
+
+      return hashCode.intValue();
+   }
+
+   /**
+    * Equals
+    * @param other The other object
+    * @return True if equal; otherwise false
+    */
+   public boolean equals(Object other)
+   {
+      if (other == null || !(other instanceof Annotation))
+         return false;
+
+      Annotation a = (Annotation)other;
+
+      boolean result = true;
+
+      result = annotation.getClass().getName().equals(a.getAnnotation().getClass().getName());
+
+      if (result)
+         result = type.equals(a.getType());
+         
+      if (result)
+         result = className.equals(a.getClassName());
+         
+      if (result)
+      {
+         if (memberName == null)
+         {
+            result = a.getMemberName() == null;
+         }
+         else
+         {
+            result = memberName.equals(a.getMemberName());
+         }
+      }
+
+      if (result)
+      {
+         if (parameterTypes == null)
+         {
+            result = a.getParameterTypes() == null;
+         }
+         else
+         {
+            result = Arrays.equals(getParameterTypes(), a.getParameterTypes());
+         }
+      }
+
+      return result;
+   }
+
+   /**
     * String representation
     * @return The representation
     */

Modified: projects/annotations/trunk/core/src/test/java/org/jboss/annotations/test/AnnotationRepositoryTests.java
===================================================================
--- projects/annotations/trunk/core/src/test/java/org/jboss/annotations/test/AnnotationRepositoryTests.java	2009-08-11 13:39:56 UTC (rev 92229)
+++ projects/annotations/trunk/core/src/test/java/org/jboss/annotations/test/AnnotationRepositoryTests.java	2009-08-11 14:09:54 UTC (rev 92230)
@@ -268,7 +268,7 @@
       assertNotNull(l);
       assertTrue(l.size() == 1);
 
-      Annotation annotation = new Annotation(null, AnnotationType.CLASS,
+      Annotation annotation = new Annotation(new Object(), AnnotationType.CLASS,
                                              "org.jboss.annotations.test.DE", null, (String[])null);
       try
       {




More information about the jboss-cvs-commits mailing list