Author: oheger Date: Sat Oct 25 09:02:42 2008 New Revision: 707855 URL: http://svn.apache.org/viewvc?rev=707855&view=rev Log: Pulled some methods from BaseConfiguration up to AbstractFlatConfiguration. They can serve as default implementations.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java?rev=707855&r1=707854&r2=707855&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java Sat Oct 25 09:02:42 2008 @@ -16,7 +16,9 @@ */ package org.apache.commons.configuration2.flat; +import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -107,10 +109,13 @@ * value (identified by its 0-based index) without affecting the other * values. If the index is invalid (i.e. less than 0 or greater than the * number of existing values), the value will be added to the existing - * values of this property. This method takes care of firing the appropriate - * events and delegates to <code>setPropertyValueDirect()</code>. It - * generates a <code>EVENT_PROPERTY_CHANGED</code> event that contains the - * key of the affected property. + * values of this property. Note that this method expects a scalar value + * rather than an array or a collection. In the latter case the behavior is + * undefined and may vary for different derived classes. This method takes + * care of firing the appropriate events and delegates to + * <code>setPropertyValueDirect()</code>. It generates a + * <code>EVENT_PROPERTY_CHANGED</code> event that contains the key of the + * affected property. * * @param key the key of the property * @param index the index of the value to change @@ -120,7 +125,15 @@ public void setPropertyValue(String key, int index, Object value) { fireEvent(EVENT_PROPERTY_CHANGED, key, null, true); - setPropertyValueDirect(key, index, value); + setDetailEvents(true); + try + { + setPropertyValueDirect(key, index, value); + } + finally + { + setDetailEvents(false); + } fireEvent(EVENT_PROPERTY_CHANGED, key, null, false); } @@ -141,7 +154,18 @@ public boolean clearPropertyValue(String key, int index) { fireEvent(EVENT_PROPERTY_CHANGED, key, null, true); - boolean result = clearPropertyValueDirect(key, index); + + boolean result = false; + setDetailEvents(true); + try + { + result = clearPropertyValueDirect(key, index); + } + finally + { + setDetailEvents(false); + } + fireEvent(EVENT_PROPERTY_CHANGED, key, null, false); return result; } @@ -201,12 +225,30 @@ * can be used to find out how many values are stored for a given property. * A return value of -1 means that this property is unknown. A value of 0 * indicates that there is a single value, 1 means there are two values, - * etc. + * etc. This base implementation uses [EMAIL PROTECTED] getProperty()} to obtain the + * property value(s). Derived classes should override this method if they + * can provide a more efficient implementation. * * @param key the key of the property * @return the maximum index of a value of this property */ - public abstract int getMaxIndex(String key); + public int getMaxIndex(String key) + { + Object value = getProperty(key); + + if (value == null) + { + return -1; + } + else if (value instanceof Collection) + { + return ((Collection<?>) value).size() - 1; + } + else + { + return 0; + } + } /** * Creates a hierarchy of <code>FlatNode</code> objects that corresponds @@ -254,14 +296,50 @@ /** * Performs the actual modification of the specified property value. This - * method is called by <code>setPropertyValue()</code>. + * method is called by <code>setPropertyValue()</code>. The base + * implementation provided by this class uses [EMAIL PROTECTED] getProperty()} for + * obtaining the current value(s) of the specified property. If the property + * does not exist or the index is invalid, the value is added as if + * <code>addProperty()</code> was called. If the property has a single + * value, the passed in index determines what happens: if it is 0, the + * single value is replaced by the new one; all other indices cause the new + * value to be added to the old one. The method delegates to [EMAIL PROTECTED] + * setProperty()} or [EMAIL PROTECTED] addPropertyDirect()} for storing the new + * property value. Derived classes should override it if they can provide a + * more efficient implementation. * * @param key the key of the property * @param index the index of the value to change * @param value the new value */ - protected abstract void setPropertyValueDirect(String key, int index, - Object value); + protected void setPropertyValueDirect(String key, int index, Object value) + { + Object oldValue = getProperty(key); + + if (oldValue instanceof List) + { + @SuppressWarnings("unchecked") + List<Object> col = (List<Object>) oldValue; + if (index >= 0 && index < col.size()) + { + col.set(index, value); + setProperty(key, col); + } + else + { + addPropertyDirect(key, value); + } + } + + else if (oldValue == null || index != 0) + { + addPropertyDirect(key, value); + } + else + { + setProperty(key, value); + } + } /** * Initializes the node handler of this configuration. @@ -273,13 +351,34 @@ /** * Performs the actual remove property value operation. This method is - * called by <code>clearPropertyValue()</code>. + * called by <code>clearPropertyValue()</code>. The base implementation + * provided by this class uses [EMAIL PROTECTED] getProperty()} for obtaining the + * value(s) of the property. If there are multiple values and the index is + * in the allowed range, the value with the given index is removed, and the + * new values are stored using [EMAIL PROTECTED] setProperty()}. Derived classes should + * override this method if they can provide a more efficient implementation. * * @param key the key of the property * @param index the index of the value to delete * @return a flag whether the value could be removed */ - protected abstract boolean clearPropertyValueDirect(String key, int index); + protected boolean clearPropertyValueDirect(String key, int index) + { + Object value = getProperty(key); + + if (value instanceof List && index >= 0) + { + List<?> col = (List<?>) value; + if (index < col.size()) + { + col.remove(index); + setProperty(key, col); + return true; + } + } + + return false; + } /** * Registers a change listener at this configuration that causes the node Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java?rev=707855&r1=707854&r2=707855&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java Sat Oct 25 09:02:42 2008 @@ -18,7 +18,6 @@ package org.apache.commons.configuration2.flat; import java.util.ArrayList; -import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -103,6 +102,7 @@ * @param key key to use for mapping * @param value object to store */ + @Override @SuppressWarnings("unchecked") protected void addPropertyDirect(String key, Object value) { @@ -169,6 +169,7 @@ * * @param key the key to remove along with corresponding value. */ + @Override protected void clearPropertyDirect(String key) { if (containsKey(key)) @@ -177,6 +178,7 @@ } } + @Override public void clear() { fireEvent(EVENT_CLEAR, null, null, true); @@ -203,6 +205,7 @@ * @return the copy * @since 1.3 */ + @Override @SuppressWarnings("unchecked") public Object clone() { @@ -218,102 +221,4 @@ throw new ConfigurationRuntimeException(cex); } } - - /** - * Returns the maximum index for the property with the given index. If the - * property has multiple values, the maximum index is the number of values - * minus 1 (indices are 0-based). If the property has a single value, the - * maximum index is 0. If the passed in key cannot be resolved, result is - * -1. - * - * @param key the key of the property in question - * @return the maximum index for this property - */ - @Override - public int getMaxIndex(String key) - { - Object value = getProperty(key); - - if (value == null) - { - return -1; - } - else if (value instanceof Collection) - { - return ((Collection<?>) value).size() - 1; - } - else - { - return 0; - } - } - - /** - * Removes a value of a property with multiple values. The value to remove - * is specified using its (0-based) index. If the index is invalid or if the - * property does not have multiple values, this method has no effect. - * - * @param key the key of the property - * @param index the index of the value to be removed - * @return a flag whether the value could be removed - */ - @Override - protected boolean clearPropertyValueDirect(String key, int index) - { - Object value = getProperty(key); - - if (value instanceof List && index >= 0) - { - List<?> col = (List<?>) value; - if (index < col.size()) - { - col.remove(index); - return true; - } - } - - return false; - } - - /** - * Modifies a value of a property with multiple values. The value to be - * changed is specified using its (0-based) index. If the property does not - * exist or the index is invalid, the value is added as if - * <code>addProperty()</code> was called. If the property has a single - * value, the passed in index determines what happens: if it is 0, the - * single value is replaced by the new one; all other indices cause the new - * value to be added to the old one. - * - * @param key the key of the property - * @param index the index of the value to be manipulated - * @param value the new value - */ - @SuppressWarnings("unchecked") - @Override - protected void setPropertyValueDirect(String key, int index, Object value) - { - Object oldValue = getProperty(key); - - if (oldValue instanceof List) - { - List<Object> col = (List<Object>) oldValue; - if (index >= 0 && index < col.size()) - { - col.set(index, value); - } - else - { - addPropertyDirect(key, value); - } - } - - else if (oldValue == null || index != 0) - { - addPropertyDirect(key, value); - } - else - { - store.put(key, value); - } - } }