You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2012/09/19 22:01:23 UTC

svn commit: r1387730 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java

Author: oheger
Date: Wed Sep 19 20:01:22 2012
New Revision: 1387730

URL: http://svn.apache.org/viewvc?rev=1387730&view=rev
Log:
Added missing functionality to DefaultConfigurationKey so it is now a full replacement of ConfigurationKey.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java?rev=1387730&r1=1387729&r2=1387730&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/DefaultConfigurationKey.java Wed Sep 19 20:01:22 2012
@@ -206,6 +206,78 @@ public class DefaultConfigurationKey
     {
         keyBuffer.setLength(len);
     }
+    /**
+     * Returns a configuration key object that is initialized with the part
+     * of the key that is common to this key and the passed in key.
+     *
+     * @param other the other key
+     * @return a key object with the common key part
+     */
+    public DefaultConfigurationKey commonKey(DefaultConfigurationKey other)
+    {
+        if (other == null)
+        {
+            throw new IllegalArgumentException("Other key must no be null!");
+        }
+
+        DefaultConfigurationKey result = new DefaultConfigurationKey(getExpressionEngine());
+        KeyIterator it1 = iterator();
+        KeyIterator it2 = other.iterator();
+
+        while (it1.hasNext() && it2.hasNext() && partsEqual(it1, it2))
+        {
+            if (it1.isAttribute())
+            {
+                result.appendAttribute(it1.currentKey());
+            }
+            else
+            {
+                result.append(it1.currentKey());
+                if (it1.hasIndex)
+                {
+                    result.appendIndex(it1.getIndex());
+                }
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * Returns the "difference key" to a given key. This value
+     * is the part of the passed in key that differs from this key. There is
+     * the following relation:
+     * {@code other = key.commonKey(other) + key.differenceKey(other)}
+     * for an arbitrary configuration key {@code key}.
+     *
+     * @param other the key for which the difference is to be calculated
+     * @return the difference key
+     */
+    public DefaultConfigurationKey differenceKey(DefaultConfigurationKey other)
+    {
+        DefaultConfigurationKey common = commonKey(other);
+        DefaultConfigurationKey result = new DefaultConfigurationKey(getExpressionEngine());
+
+        if (common.length() < other.length())
+        {
+            String k = other.toString().substring(common.length());
+            // skip trailing delimiters
+            int i = 0;
+            while (i < k.length()
+                    && String.valueOf(k.charAt(i)).equals(
+                            getExpressionEngine().getPropertyDelimiter()))
+            {
+                i++;
+            }
+
+            if (i < k.length())
+            {
+                result.append(k.substring(i));
+            }
+        }
+
+        return result;
+    }
 
     /**
      * Checks if two {@code ConfigurationKey} objects are equal. The
@@ -456,6 +528,20 @@ public class DefaultConfigurationKey
     }
 
     /**
+     * Helper method for comparing two key parts.
+     *
+     * @param it1 the iterator with the first part
+     * @param it2 the iterator with the second part
+     * @return a flag if both parts are equal
+     */
+    private static boolean partsEqual(KeyIterator it1, KeyIterator it2)
+    {
+        return it1.nextKey().equals(it2.nextKey())
+                && it1.getIndex() == it2.getIndex()
+                && it1.isAttribute() == it2.isAttribute();
+    }
+
+    /**
      * A specialized iterator class for tokenizing a configuration key. This
      * class implements the normal iterator interface. In addition it provides
      * some specific methods for configuration keys.

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java?rev=1387730&r1=1387729&r2=1387730&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/TestDefaultConfigurationKey.java Wed Sep 19 20:01:22 2012
@@ -45,10 +45,10 @@ public class TestDefaultConfigurationKey
     private static final String TESTKEY = TESTPROPS + TESTATTR;
 
     /** Stores the expression engine of the key to test. */
-    DefaultExpressionEngine expressionEngine;
+    private DefaultExpressionEngine expressionEngine;
 
     /** Stores the object to be tested. */
-    DefaultConfigurationKey key;
+    private DefaultConfigurationKey key;
 
     @Before
     public void setUp() throws Exception
@@ -58,6 +58,17 @@ public class TestDefaultConfigurationKey
     }
 
     /**
+     * Helper method to create a key instance with the given content.
+     *
+     * @param k the key for initialization
+     * @return the newly created {@code DefaultConfigurationKey} instance
+     */
+    private DefaultConfigurationKey key(String k)
+    {
+        return new DefaultConfigurationKey(expressionEngine, k);
+    }
+
+    /**
      * Tests setting the expression engine to null. This should not be allowed.
      */
     @Test(expected = IllegalArgumentException.class)
@@ -292,10 +303,8 @@ public class TestDefaultConfigurationKey
     @Test
     public void testEquals()
     {
-        DefaultConfigurationKey k1 = new DefaultConfigurationKey(
-                expressionEngine, TESTKEY);
-        DefaultConfigurationKey k2 = new DefaultConfigurationKey(
-                expressionEngine, TESTKEY);
+        DefaultConfigurationKey k1 = key(TESTKEY);
+        DefaultConfigurationKey k2 = key(TESTKEY);
         assertTrue("Keys are not equal", k1.equals(k2));
         assertTrue("Not reflexiv", k2.equals(k1));
         assertEquals("Hash codes not equal", k1.hashCode(), k2.hashCode());
@@ -513,4 +522,71 @@ public class TestDefaultConfigurationKey
         assertTrue("Third part is not a property key", kit.isPropertyKey());
         assertEquals("Wrong decorated key part", "key", kit.currentKey(true));
     }
+
+    /**
+     * Tests whether common key parts can be extracted.
+     */
+    @Test
+    public void testCommonKey()
+    {
+        DefaultConfigurationKey k1 = key(TESTKEY);
+        DefaultConfigurationKey k2 = key("tables.table(0).name");
+        DefaultConfigurationKey kc = k1.commonKey(k2);
+        assertEquals("Wrong common key (1)", key("tables.table(0)"), kc);
+        assertEquals("Not symmetric", kc, k2.commonKey(k1));
+
+        k2 = key("tables.table(1).fields.field(1)");
+        kc = k1.commonKey(k2);
+        assertEquals("Wrong common key (2)", key("tables"), kc);
+
+        k2 = key("completely.different.key");
+        kc = k1.commonKey(k2);
+        assertEquals("Got a common key for different keys", 0, kc.length());
+
+        kc = k1.commonKey(key);
+        assertEquals("Got a common key for empty key", 0, kc.length());
+
+        kc = k1.commonKey(k1);
+        assertEquals("Wrong result for reflexiv invocation", kc, k1);
+    }
+
+    /**
+     * Tries to call commonKey() with null input.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCommonKeyNull()
+    {
+        key.commonKey(null);
+    }
+
+    /**
+     * Tests differenceKey() on the same object.
+     */
+    @Test
+    public void testDifferenceKeySame()
+    {
+        DefaultConfigurationKey k1 = key(TESTKEY);
+        DefaultConfigurationKey kd = k1.differenceKey(k1);
+        assertEquals("Got difference for same keys", 0, kd.length());
+    }
+
+    /**
+     * Tests the differenceKey() method.
+     */
+    @Test
+    public void testDifferenceKey()
+    {
+        DefaultConfigurationKey k1 = key(TESTKEY);
+        DefaultConfigurationKey k2 = key("tables.table(0).name");
+        DefaultConfigurationKey kd = k1.differenceKey(k2);
+        assertEquals("Wrong difference (1)", "name", kd.toString());
+
+        k2 = key("tables.table(1).fields.field(1)");
+        kd = k1.differenceKey(k2);
+        assertEquals("Wrong difference (2)", "table(1).fields.field(1)", kd.toString());
+
+        k2 = key("completely.different.key");
+        kd = k1.differenceKey(k2);
+        assertEquals("Wrong difference (3)", k2, kd);
+    }
 }