geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r155261 - in geronimo/trunk/modules/kernel/src: java/org/apache/geronimo/gbean/GBeanName.java test/org/apache/geronimo/gbean/GBeanNameTest.java
Date Thu, 24 Feb 2005 22:21:14 GMT
Author: jboynes
Date: Thu Feb 24 14:21:12 2005
New Revision: 155261

URL: http://svn.apache.org/viewcvs?view=rev&rev=155261
Log:
Added method to String format name with keys in a specified order
Changed (String,Map) constructor to generate name in iteration order


Modified:
    geronimo/trunk/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanName.java
    geronimo/trunk/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanNameTest.java

Modified: geronimo/trunk/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanName.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanName.java?view=diff&r1=155260&r2=155261
==============================================================================
--- geronimo/trunk/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanName.java (original)
+++ geronimo/trunk/modules/kernel/src/java/org/apache/geronimo/gbean/GBeanName.java Thu Feb
24 14:21:12 2005
@@ -18,6 +18,7 @@
 
 import java.io.Serializable;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -50,10 +51,10 @@
 
     /**
      * Construct a GBeanName by combining a domain with explicit properties.
-     * The string representation of this name is generated by combining the properties in
sorted order.
+     * The string representation of this name is generated by combining the properties in
iteration order.
      *
      * @param domain the domain
-     * @param props the properties used to qualify this name; a Map<String,String>
+     * @param props  the properties used to qualify this name; a Map<String,String>
      */
     public GBeanName(String domain, Map props) {
         if (domain == null) {
@@ -65,12 +66,26 @@
         }
         this.domain = domain;
         this.props = new HashMap(props);
-        this.name = sortName(domain, props);
+        this.name = buildName(domain, props);
         this.hashCode = domain.hashCode() + 37 * props.hashCode();
     }
 
+    private static String buildName(String domain, Map props) {
+        StringBuffer buf = new StringBuffer(128);
+        buf.append(domain).append(':');
+        Iterator i = props.entrySet().iterator();
+        Map.Entry entry = (Map.Entry) i.next();
+        buf.append(entry.getKey()).append('=').append(entry.getValue());
+        while (i.hasNext()) {
+            entry = (Map.Entry) i.next();
+            buf.append(',').append(entry.getKey()).append('=').append(entry.getValue());
+        }
+        return buf.toString();
+    }
+
     /**
      * Construct a GBeanName by parsing a string.
+     *
      * @param name the name to parse
      */
     public GBeanName(String name) {
@@ -105,19 +120,6 @@
         return props;
     }
 
-    private static String sortName(String domain, Map props) {
-        String[] names = (String[]) props.keySet().toArray(new String[props.size()]);
-        Arrays.sort(names);
-        StringBuffer buf = new StringBuffer(128);
-        buf.append(domain).append(':');
-        buf.append(names[0]).append('=').append(props.get(names[0]));
-        for (int i = 1; i < names.length; i++) {
-            String name = names[i];
-            buf.append(',').append(name).append('=').append(props.get(name));
-        }
-        return buf.toString();
-    }
-
     /**
      * Determine if this name matches the supplied pattern.
      * This performs a fast but simplistic pattern match which is true if:
@@ -129,7 +131,7 @@
      * and always match; in other words GBeanName.match(null, new Properties()) will
      * always evaluate to true.
      *
-     * @param domain the domain to match
+     * @param domain  the domain to match
      * @param pattern the set properties to match; a Map<String,String>
      * @return true if this instance matches the pattern
      */
@@ -156,6 +158,7 @@
      * Test for equality.
      * This instance will be equal if the supplied object is a GBeanName with
      * equal domain and properties.
+     *
      * @param obj
      * @return
      */
@@ -173,12 +176,35 @@
     /**
      * Return a human readable version of this GBeanName. If the instance was created
      * by parsing a String, this will be the supplied value; it it was created by
-     * supplying properties, the name will contain properties in sorted order.
+     * supplying properties, the name will contain properties in an unspecified order.
      *
      * @return a readable name
      */
     public String toString() {
         return name;
+    }
+
+    /**
+     * Return a String representation of ths GBeanName.
+     * The format will be <domain> ':' <key> '=' <value> ( ',' <key>
'=' <value> )*
+     * Keys are appended in the order determined by the supplied Comparator.
+     *
+     * @param keySorter the Comparator to use to order the keys.
+     * @return a String representation of this GBean
+     */
+    public String toString(Comparator keySorter) {
+        String[] keyList = (String[]) props.keySet().toArray(new String[props.keySet().size()]);
+        Arrays.sort(keyList, keySorter);
+
+        StringBuffer buf = new StringBuffer(128);
+        buf.append(domain).append(':');
+        String key = keyList[0];
+        buf.append(key).append('=').append(props.get(key));
+        for (int i = 1; i < keyList.length; i++) {
+            key = keyList[i];
+            buf.append(',').append(key).append('=').append(props.get(key));
+        }
+        return buf.toString();
     }
 
     private Object readResolve() {

Modified: geronimo/trunk/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanNameTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanNameTest.java?view=diff&r1=155260&r2=155261
==============================================================================
--- geronimo/trunk/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanNameTest.java (original)
+++ geronimo/trunk/modules/kernel/src/test/org/apache/geronimo/gbean/GBeanNameTest.java Thu
Feb 24 14:21:12 2005
@@ -19,6 +19,9 @@
 import java.util.Properties;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Comparator;
 import java.rmi.MarshalledObject;
 
 import junit.framework.TestCase;
@@ -31,6 +34,7 @@
 
     public void testPropertyConstruction() {
         String domain = "testDomain";
+        Map props = new LinkedHashMap();
         props.put("prop1", "value1");
         props.put("prop2", "value2");
         GBeanName name = new GBeanName(domain, props);
@@ -63,6 +67,20 @@
         assertTrue(name.matches("testDomain", props));
         props.setProperty("prop3", "value3");
         assertFalse(name.matches("testDomain", props));
+    }
+
+    public void testStringForms() {
+        GBeanName name = new GBeanName("testDomain:prop2=value2,prop3=value3,prop1=value1");
+        assertEquals("testDomain:prop1=value1,prop2=value2,prop3=value3", name.toString(new
Comparator() {
+            public int compare(Object o1, Object o2) {
+                return ((String)o1).compareTo(o2);
+            }
+        }));
+        assertEquals("testDomain:prop3=value3,prop2=value2,prop1=value1", name.toString(new
Comparator() {
+            public int compare(Object o1, Object o2) {
+                return - ((String)o1).compareTo(o2);
+            }
+        }));
     }
 
     public void testInvalidNames() {



Mime
View raw message