geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r568268 - in /geronimo/server/branches/2.0/modules/geronimo-j2ee/src: main/java/org/apache/geronimo/j2ee/annotation/ test/java/org/apache/geronimo/j2ee/annotation/
Date Tue, 21 Aug 2007 20:46:15 GMT
Author: gawor
Date: Tue Aug 21 13:46:15 2007
New Revision: 568268

URL: http://svn.apache.org/viewvc?rev=568268&view=rev
Log:
switch from List to Set to eliminate duplicate injections (GERONIMO-3426)

Modified:
    geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Holder.java
    geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Injection.java
    geronimo/server/branches/2.0/modules/geronimo-j2ee/src/test/java/org/apache/geronimo/j2ee/annotation/HolderTest.java

Modified: geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Holder.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Holder.java?rev=568268&r1=568267&r2=568268&view=diff
==============================================================================
--- geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Holder.java
(original)
+++ geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Holder.java
Tue Aug 21 13:46:15 2007
@@ -23,9 +23,12 @@
 import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -42,7 +45,7 @@
     public static final Holder EMPTY = new Holder() {
     };
 
-    private Map<String, List<Injection>> injectionMap;
+    private Map<String, Set<Injection>> injectionMap;
     private Map<String, LifecycleMethod> postConstruct;
     private Map<String, LifecycleMethod> preDestroy;
 
@@ -52,7 +55,7 @@
 
     public Holder(Holder source) {
         if (source.getInjectionMap() != null) {
-            this.injectionMap = new HashMap<String, List<Injection>>();
+            this.injectionMap = new HashMap<String, Set<Injection>>();
             addInjectionMap(source.getInjectionMap());
         }
         
@@ -67,25 +70,25 @@
         }
     }
     
-    private List<Injection> getInjectionList(String className) {
+    private Set<Injection> getInjectionList(String className) {
         if (injectionMap == null) {
-            injectionMap = new HashMap<String, List<Injection>>();
+            injectionMap = new HashMap<String, Set<Injection>>();
         }
-        List<Injection> injections = injectionMap.get(className);
+        Set<Injection> injections = injectionMap.get(className);
         if (injections == null) {
-            injections = new ArrayList<Injection>();
+            injections = new HashSet<Injection>();
             injectionMap.put(className, injections);
         }
         return injections;
     }
     
     public void addInjection(String className, Injection newInjection) {
-        List<Injection> injections = getInjectionList(className);
+        Set<Injection> injections = getInjectionList(className);
         injections.add(newInjection);
     }
     
-    public void addInjections(String className, List<Injection> newInjections) {
-        List<Injection> injections = getInjectionList(className);        
+    public void addInjections(String className, Collection<Injection> newInjections)
{
+        Set<Injection> injections = getInjectionList(className);        
         for (Injection injection : newInjections) {
             injections.add(injection);
         }
@@ -110,25 +113,28 @@
         return old;
     }
 
-    public void addInjectionMap(Map<String, List<Injection>> injectionMap) {
+    public void addInjectionMap(Map<String, Set<Injection>> injectionMap) {
         if (injectionMap == null) {
             return;
         }
-        for (Map.Entry<String, List<Injection>> entry : injectionMap.entrySet())
{
+        for (Map.Entry<String, Set<Injection>> entry : injectionMap.entrySet())
{
             String className = entry.getKey();
-            List<Injection> injections = entry.getValue();
+            Set<Injection> injections = entry.getValue();
             addInjections(className, injections);            
         }
     }
     
     public List<Injection> getInjections(String className) {
-        if (injectionMap == null) {
-            return null;
+        if (injectionMap != null) {                  
+            Set<Injection> injections = injectionMap.get(className);
+            if (injections != null) {
+                return new ArrayList<Injection>(injections);
+            }
         }
-        return injectionMap.get(className);
+        return null;
     }
 
-    public Map<String, List<Injection>> getInjectionMap() {
+    public Map<String, Set<Injection>> getInjectionMap() {
         return injectionMap;
     }
     

Modified: geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Injection.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Injection.java?rev=568268&r1=568267&r2=568268&view=diff
==============================================================================
--- geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Injection.java
(original)
+++ geronimo/server/branches/2.0/modules/geronimo-j2ee/src/main/java/org/apache/geronimo/j2ee/annotation/Injection.java
Tue Aug 21 13:46:15 2007
@@ -23,7 +23,7 @@
 import java.io.Serializable;
 
 /**
- * @version $Rev:$ $Date:$
+ * @version $Rev$ $Date$
  */
 public class Injection implements Serializable {
 
@@ -49,6 +49,59 @@
 
     public String getJndiName() {
         return jndiName;
+    }
+    
+    public String toString() {
+        StringBuffer buf = new StringBuffer();
+        buf.append(targetClassName).append(" ");
+        buf.append(targetName).append(" ");
+        buf.append(jndiName);
+        return buf.toString();
+    }
+
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((jndiName == null) ? 0 : jndiName.hashCode());
+        result = prime * result + ((targetClassName == null) ? 0 : targetClassName.hashCode());
+        result = prime * result + ((targetName == null) ? 0 : targetName.hashCode());
+        return result;
+    }
+
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final Injection other = (Injection) obj;        
+        if (jndiName == null) {
+            if (other.jndiName != null) {
+                return false;
+            }
+        } else if (!jndiName.equals(other.jndiName)) {
+            return false;
+        }        
+        if (targetClassName == null) {
+            if (other.targetClassName != null) { 
+                return false;
+            }
+        } else if (!targetClassName.equals(other.targetClassName)) {
+            return false;
+        }        
+        if (targetName == null) {
+            if (other.targetName != null) {
+                return false;
+            }
+        } else if (!targetName.equals(other.targetName)) {
+            return false;
+        }
+        
+        return true;
     }
     
 }

Modified: geronimo/server/branches/2.0/modules/geronimo-j2ee/src/test/java/org/apache/geronimo/j2ee/annotation/HolderTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/2.0/modules/geronimo-j2ee/src/test/java/org/apache/geronimo/j2ee/annotation/HolderTest.java?rev=568268&r1=568267&r2=568268&view=diff
==============================================================================
--- geronimo/server/branches/2.0/modules/geronimo-j2ee/src/test/java/org/apache/geronimo/j2ee/annotation/HolderTest.java
(original)
+++ geronimo/server/branches/2.0/modules/geronimo-j2ee/src/test/java/org/apache/geronimo/j2ee/annotation/HolderTest.java
Tue Aug 21 13:46:15 2007
@@ -19,11 +19,25 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
 public class HolderTest extends TestCase {
 
+    public void testDuplicates() throws Exception {
+        Holder holder = new Holder();
+        holder.addInjection("a1", new Injection("a1", "1", "2"));
+        holder.addInjection("a1", new Injection("a1", "1", "2"));
+        
+        holder.addInjection("a1", new Injection("a1", "1", "X"));
+        holder.addInjection("a1", new Injection("a1", "X", "2"));
+        
+        List<Injection> injections = holder.getInjections("a1");
+        assertEquals(3, injections.size());        
+    }
+    
+    
     public void testCopy() throws Exception {
         Holder source = new Holder();
         source.addInjection("a1", new Injection("a1", "1", "2"));
@@ -50,29 +64,29 @@
         compareLifecycleMethod(source.getPreDestroy(), copy.getPreDestroy());
     }
     
-    private void compareInjection(Map<String, List<Injection>> expected, Map<String,
List<Injection>> actual) {
+    private void compareInjection(Map<String, Set<Injection>> expected, Map<String,
Set<Injection>> actual) {
         assertNotNull(expected);
         assertNotNull(actual);
         assertTrue(expected != actual);
         assertEquals(expected.size(), actual.size());
         
-        for (Map.Entry<String, List<Injection>> entry : expected.entrySet())
{
+        for (Map.Entry<String, Set<Injection>> entry : expected.entrySet()) {
             String className = entry.getKey();
-            List<Injection> expectedInjections = entry.getValue();
+            Set<Injection> expectedInjections = entry.getValue();
             
-            List<Injection> actualInjections = actual.get(className);
+            Set<Injection> actualInjections = actual.get(className);
             compare(expectedInjections, actualInjections);
         }                            
     }
     
-    private void compare(List<Injection> expected, List<Injection> actual) {
+    private void compare(Set<Injection> expected, Set<Injection> actual) {
         assertNotNull(expected);
         assertNotNull(actual);
         assertTrue(expected != actual);
         assertEquals(expected.size(), actual.size());
         
-        for (int i = 0; i < expected.size(); i++) {
-            assertEquals(expected.get(i), actual.get(i));
+        for (Injection injection : expected) {
+            assertTrue(actual.contains(injection));
         }
     }
     



Mime
View raw message