Author: oheger Date: Thu Sep 20 19:52:44 2012 New Revision: 1388185 URL: http://svn.apache.org/viewvc?rev=1388185&view=rev Log: Some improvements of DefaultConfigurationKey: - Made fields final. - Made equals() implementation comply to the general contract of equals().
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=1388185&r1=1388184&r2=1388185&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 Thu Sep 20 19:52:44 2012 @@ -57,34 +57,47 @@ public class DefaultConfigurationKey private static final int INITIAL_SIZE = 32; /** Stores a reference to the associated expression engine. */ - private DefaultExpressionEngine expressionEngine; + private final DefaultExpressionEngine expressionEngine; /** Holds a buffer with the so far created key. */ - private StringBuilder keyBuffer; + private final StringBuilder keyBuffer; /** * Creates a new instance of {@code DefaultConfigurationKey} and sets * the associated expression engine. * - * @param engine the expression engine + * @param engine the expression engine (must not be <b>null</b>) + * @throws IllegalArgumentException if the expression engine is <b>null</b> */ public DefaultConfigurationKey(DefaultExpressionEngine engine) { - keyBuffer = new StringBuilder(INITIAL_SIZE); - setExpressionEngine(engine); + this(engine, null); } /** - * Creates a new instance of {@code DefaultConfigurationKey} and sets - * the associated expression engine and an initial key. + * Creates a new instance of {@code DefaultConfigurationKey} and sets the + * associated expression engine and an initial key. * - * @param engine the expression engine + * @param engine the expression engine (must not be <b>null</b>) * @param key the key to be wrapped + * @throws IllegalArgumentException if the expression engine is <b>null</b> */ public DefaultConfigurationKey(DefaultExpressionEngine engine, String key) { - setExpressionEngine(engine); - keyBuffer = new StringBuilder(trim(key)); + if (engine == null) + { + throw new IllegalArgumentException( + "Expression engine must not be null!"); + } + expressionEngine = engine; + if (key != null) + { + keyBuffer = new StringBuilder(trim(key)); + } + else + { + keyBuffer = new StringBuilder(INITIAL_SIZE); + } } /** @@ -98,21 +111,6 @@ public class DefaultConfigurationKey } /** - * Sets the associated expression engine. - * - * @param expressionEngine the expression engine (must not be <b>null</b>) - */ - public void setExpressionEngine(DefaultExpressionEngine expressionEngine) - { - if (expressionEngine == null) - { - throw new IllegalArgumentException( - "Expression engine must not be null!"); - } - this.expressionEngine = expressionEngine; - } - - /** * Appends the name of a property to this key. If necessary, a property * delimiter will be added. If the boolean argument is set to <b>true</b>, * property delimiters contained in the property name will be escaped. @@ -280,20 +278,27 @@ public class DefaultConfigurationKey } /** - * Checks if two {@code ConfigurationKey} objects are equal. The - * method can be called with strings or other objects, too. + * Checks if two {@code ConfigurationKey} objects are equal. Two instances + * of this class are considered equal if they have the same content (i.e. + * their internal string representation is equal). The expression engine + * property is not taken into account. * - * @param c the object to compare + * @param obj the object to compare * @return a flag if both objects are equal */ @Override - public boolean equals(Object c) + public boolean equals(Object obj) { - if (c == null) + if (this == obj) + { + return true; + } + if (!(obj instanceof DefaultConfigurationKey)) { return false; } + DefaultConfigurationKey c = (DefaultConfigurationKey) obj; return keyBuffer.toString().equals(c.toString()); } 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=1388185&r1=1388184&r2=1388185&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 Thu Sep 20 19:52:44 2012 @@ -74,7 +74,7 @@ public class TestDefaultConfigurationKey @Test(expected = IllegalArgumentException.class) public void testSetNullExpressionEngine() { - key.setExpressionEngine(null); + new DefaultConfigurationKey(null); } /** @@ -304,15 +304,16 @@ public class TestDefaultConfigurationKey public void testEquals() { DefaultConfigurationKey k1 = key(TESTKEY); + assertTrue("Key not equal to itself", k1.equals(k1)); 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()); k2.append("anotherPart"); assertFalse("Keys considered equal", k1.equals(k2)); - assertFalse("Keys considered equal", k2.equals(k1)); + assertFalse("Keys considered equal (2)", k2.equals(k1)); assertFalse("Key equals null key", k1.equals(null)); - assertTrue("Faild comparison with string", k1.equals(TESTKEY)); + assertFalse("Equal with string", k1.equals(TESTKEY)); } /**