harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r512169 [1/2] - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/javax/naming/ldap/ test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/
Date Tue, 27 Feb 2007 08:05:39 GMT
Author: mloenko
Date: Tue Feb 27 00:05:38 2007
New Revision: 512169

URL: http://svn.apache.org/viewvc?view=rev&rev=512169
Log:
fixing style and simplifying Rdn and RdnTest

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/RdnTest.java
      - copied, changed from r511744, harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/TestRdn.java
Removed:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/TestRdn.java
Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/ldap/Rdn.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/ldap/Rdn.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/ldap/Rdn.java?view=diff&rev=512169&r1=512168&r2=512169
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/ldap/Rdn.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/javax/naming/ldap/Rdn.java
Tue Feb 27 00:05:38 2007
@@ -49,8 +49,9 @@
      * @ar.org.fitc.spec_ref
      */
     public static String escapeValue(Object val) {
-        if (val == null)
+        if (val == null) {
             throw new NullPointerException("val "+Messages.getString("ldap.00"));
+        }
         return LdapRdnParser.escapeValue(val);
     }
 
@@ -58,12 +59,13 @@
      * @ar.org.fitc.spec_ref
      */
     public static Object unescapeValue(String val) {
-        if (val == null)
+        if (val == null) {
             throw new NullPointerException("val "+Messages.getString("ldap.00"));
+        }
         return LdapRdnParser.unescapeValue(val);
     }
 
-    private List list = new ArrayList();
+    private List<Attribute> list;
 
     private transient LdapRdnParser parser;
 
@@ -71,30 +73,36 @@
      * @ar.org.fitc.spec_ref
      */
     public Rdn(Attributes attrSet) throws InvalidNameException {
-        if (attrSet == null)
+        if (attrSet == null) {
             throw new NullPointerException("attrSet "+Messages.getString("ldap.00"));
-        if (attrSet.size() == 0)
+        }
+
+        if (attrSet.size() == 0) {
             throw new InvalidNameException("atrrSet "+Messages.getString("ldap.03"));
+        }
 
-        NamingEnumeration ne = attrSet.getAll();
+        //check all the elements to follow RI's behavior
+        NamingEnumeration<? extends Attribute> ne = attrSet.getAll();
         while (ne.hasMoreElements()) {
-            Attribute at = (Attribute) ne.nextElement();
+            Attribute at = ne.nextElement();
             try {
                 at.get();
             } catch (NamingException e) {
             }
         }
-        list = convertToAttributeArrayList((Attributes) attrSet.clone());
+
+        list = convertToAttributeArrayList(attrSet);
     }
 
     /**
      * @ar.org.fitc.spec_ref
      */
     public Rdn(Rdn rdn) {
-        if (rdn == null)
+        if (rdn == null) {
             throw new NullPointerException("rdn "+Messages.getString("ldap.00"));
-        list = convertToAttributeArrayList((Attributes) rdn.toAttributes()
-                .clone());
+        }
+
+        list = convertToAttributeArrayList(rdn.toAttributes());
     }
 
     /**
@@ -108,6 +116,8 @@
         if (rdnString.length() != 0) {
             parser = new LdapRdnParser(rdnString);
             list = parser.getList();
+        } else {
+            list = new ArrayList<Attribute>();
         }
     }
 
@@ -115,16 +125,26 @@
      * @ar.org.fitc.spec_ref
      */
     public Rdn(String type, Object value) throws InvalidNameException {
-        if (type == null)
-            throw new NullPointerException("type "+Messages.getString("ldap.00"));
-        if (value == null)
-            throw new NullPointerException("value "+Messages.getString("ldap.00"));
-        if (type.length() == 0)
-            throw new InvalidNameException("type "+Messages.getString("ldap.04"));
-        if (value instanceof String) {
-            if (((String) value).length() == 0)
-                throw new InvalidNameException("value "+Messages.getString("ldap.04"));
+        if (type == null) {
+            throw new NullPointerException("type "
+                    + Messages.getString("ldap.00"));
         }
+
+        if (value == null) {
+            throw new NullPointerException("value "
+                    + Messages.getString("ldap.00"));
+        }
+
+        if (type.length() == 0) {
+            throw new InvalidNameException("type "
+                    + Messages.getString("ldap.04"));
+        }
+
+        if (value instanceof String && ((String) value).length() == 0) {
+            throw new InvalidNameException("value "
+                    + Messages.getString("ldap.04"));
+        }
+
         list = convertToAttributeArrayList(new BasicAttributes(type, value,
                 true));
     }
@@ -133,33 +153,37 @@
      * @ar.org.fitc.spec_ref
      */
     public int compareTo(Object obj) {
-
         if (!(obj instanceof Rdn)) {
-            throw new ClassCastException(
-                    Messages.getString("ldap.06"));
+            throw new ClassCastException(Messages.getString("ldap.06"));
         }
         Rdn rdn = (Rdn) obj;
-        String s1 = new String(), s2 = new String();
+        String s1 = "", s2 = "";
 
-        for (Enumeration iter = toAttributes().getAll(); iter.hasMoreElements();) {
+        for (Enumeration<?> iter = toAttributes().getAll(); iter.hasMoreElements();)
{
             s1 = s1 + escapeValue(iter.nextElement().toString());
-            if (iter.hasMoreElements())
-                s1 = s1 + ",";
+            
+            //this one does not seem necessary. Spec does not require it, if there are apps
that depend on commas, uncomment it
+            //if (iter.hasMoreElements()) {
+            //    s1 = s1 + ",";
+            //}
         }
-        for (Enumeration iter = rdn.toAttributes().getAll(); iter
-                .hasMoreElements();) {
+        for (Enumeration<?> iter = rdn.toAttributes().getAll(); iter.hasMoreElements();)
{
             s2 = s2 + escapeValue(iter.nextElement().toString());
-            if (iter.hasMoreElements())
-                s2 = s2 + ",";
+
+            //this one does not seem necessary. Spec does not require it, if there are apps
that depend on commas, uncomment it
+            //if (iter.hasMoreElements()) {
+            //    s2 = s2 + ",";
+            //}
         }
         return s1.toLowerCase().compareTo(s2.toLowerCase());
     }
 
-    private List convertToAttributeArrayList(Attributes attrList) {
-        List myList = new ArrayList();
-        NamingEnumeration ne = attrList.getAll();
+    private List<Attribute> convertToAttributeArrayList(Attributes attrList) {
+        List<Attribute> myList = new ArrayList<Attribute>();
+
+        NamingEnumeration<? extends Attribute> ne = attrList.getAll();
         while (ne.hasMoreElements()) {
-            myList.add((Attribute) ne.nextElement());
+            myList.add((Attribute)ne.nextElement().clone());
         }
         return myList;
     }
@@ -172,64 +196,36 @@
         if (!(obj instanceof Rdn) || this.size() != ((Rdn) obj).size()) {
             return false;
         }
+
         if (this == obj) {
             return true;
         }
 
-        NamingEnumeration iter1 = toAttributes().getAll();
-        NamingEnumeration iter2 = ((Rdn) obj).toAttributes().getAll();
+        NamingEnumeration<? extends Attribute> iter1 = toAttributes().getAll();
+        NamingEnumeration<? extends Attribute> iter2 = ((Rdn) obj).toAttributes().getAll();
+
         while (iter1.hasMoreElements()) {
-            Attribute a1 = (Attribute) iter1.nextElement();
-            Attribute a2 = (Attribute) iter2.nextElement();
-            if (!(a1.getID().toLowerCase().equals(a2.getID().toLowerCase())))
+            Attribute a1 = iter1.nextElement();
+            Attribute a2 = iter2.nextElement();
+
+            if (!(a1.getID().toLowerCase().equals(a2.getID().toLowerCase()))
+                    || a1.size() != a2.size()) {
                 return false;
-            Enumeration en1 = null;
-            Enumeration en2 = null;
+            }
+
+            Enumeration<?> en1 = null;
+            Enumeration<?> en2 = null;
             try {
                 en1 = a1.getAll();
                 en2 = a2.getAll();
             } catch (NamingException e) {
+                //what is the correct way for this?
+                return false;
             }
 
             while (en1.hasMoreElements()) {
-                Object obj1 = en1.nextElement();
-                Object obj2 = en2.nextElement();
-                byte[] array1 = null;
-                byte[] array2 = null;
-
-                if (obj1 instanceof String && obj2 instanceof String) {
-                    String s1 = (String) obj1;
-                    String s2 = (String) obj2;
-                    if (!(escapeValue(s1.toLowerCase()).equals(escapeValue(s2
-                            .toLowerCase()))))
-                        return false;
-                    continue;
-                } else if (obj1 instanceof byte[] && obj2 instanceof byte[]) {
-                    array1 = (byte[]) obj1;
-                    array2 = (byte[]) obj2;
-                } else if (obj1 instanceof String && obj2 instanceof byte[]) {
-                    try {
-                        array1 = (byte[]) Rdn.unescapeValue((String) obj1);
-                    } catch (ClassCastException e) {
-                        return false;
-                    }
-                    array2 = (byte[]) obj2;
-                } else if (obj1 instanceof byte[] && obj2 instanceof String) {
-                    array1 = (byte[]) obj1;
-                    try {
-                        array2 = (byte[]) Rdn.unescapeValue((String) obj2);
-                    } catch (ClassCastException e) {
-                        return false;
-                    }
-                } else {
-                    throw new ClassCastException();
-                }
-                if (array1.length == array2.length)
-                    for (int i = 0; i < array1.length; i++) {
-                        if (array1[i] != array2[i])
-                            return false;
-                    }
-                else {
+                if (!(escapeValue(en1.nextElement()).toLowerCase()
+                        .equals(escapeValue(en2.nextElement()).toLowerCase()))) {
                     return false;
                 }
             }
@@ -241,18 +237,16 @@
      * @ar.org.fitc.spec_ref
      */
     public String getType() {
-        Attribute b = (Attribute) list.get(0);
-        return b.getID();
+        return list.get(0).getID();
     }
 
     /**
      * @ar.org.fitc.spec_ref
      */
     public Object getValue() {
-        Attribute b = (Attribute) list.get(0);
         Object a = null;
         try {
-            a = b.get();
+            a = list.get(0).get();
         } catch (NamingException e) {
         } catch (NullPointerException e) {
         }
@@ -265,14 +259,17 @@
     public int hashCode() {
         int sum = 0;
 
-        for (Iterator attr = list.iterator(); attr.hasNext();) {
-            Attribute a = (Attribute) attr.next();
-            NamingEnumeration en = null;
+        for (Iterator<Attribute> attr = list.iterator(); attr.hasNext();) {
+            Attribute a = attr.next();
+            NamingEnumeration<?> en = null;
             sum += a.getID().toLowerCase().hashCode();
+
             try {
                 en = a.getAll();
             } catch (NamingException e) {
+                continue;
             }
+
             while (en.hasMoreElements()) {
                 Object obj = en.nextElement();
                 try {
@@ -291,9 +288,8 @@
      */
     public int size() {
         int result = 0;
-        for (Iterator iter = list.iterator(); iter.hasNext();) {
-            Attribute element = (Attribute) iter.next();
-            result += element.size();
+        for (Iterator<Attribute> iter = list.iterator(); iter.hasNext();) {
+            result += iter.next().size();
         }
         return result;
     }
@@ -303,9 +299,8 @@
      */
     public Attributes toAttributes() {
         BasicAttributes ba = new BasicAttributes(true);
-        for (Iterator iter = list.iterator(); iter.hasNext();) {
-            Attribute element = (Attribute) iter.next();
-            ba.put((Attribute) element.clone());
+        for (Iterator<Attribute> iter = list.iterator(); iter.hasNext();) {
+            ba.put((Attribute) iter.next().clone());
         }
         return ba;
     }
@@ -315,24 +310,29 @@
      */
     public String toString() {
         StringBuffer sb = new StringBuffer();
-        for (Iterator iter = list.iterator(); iter.hasNext();) {
-            Attribute element = (Attribute) iter.next();
-            NamingEnumeration ne = null;
+        for (Iterator<Attribute> iter = list.iterator(); iter.hasNext();) {
+            Attribute element = iter.next();
+            NamingEnumeration<?> ne = null;
+
             try {
                 ne = element.getAll();
             } catch (NamingException e) {
             }
+
             while (ne.hasMoreElements()) {
-                sb
-                        .append(element.getID() + "="
-                                + escapeValue(ne.nextElement()));
-                if (ne.hasMoreElements())
-                    sb.append("+");
+                sb.append(element.getID());
+                sb.append('=');
+                sb.append(escapeValue(ne.nextElement()));
+
+                if (ne.hasMoreElements()) {
+                    sb.append('+');
+                }
+            }
+
+            if (iter.hasNext()) {
+                sb.append('+');
             }
-            if (iter.hasNext())
-                sb.append("+");
         }
         return sb.toString();
     }
-
-}
\ No newline at end of file
+}



Mime
View raw message