Index: lutinutil/src/java/org/codelutin/option/ConfigPropertyKey.java diff -u lutinutil/src/java/org/codelutin/option/ConfigPropertyKey.java:1.1 lutinutil/src/java/org/codelutin/option/ConfigPropertyKey.java:1.2 --- lutinutil/src/java/org/codelutin/option/ConfigPropertyKey.java:1.1 Sun Dec 30 22:50:46 2007 +++ lutinutil/src/java/org/codelutin/option/ConfigPropertyKey.java Thu Jan 3 06:04:31 2008 @@ -18,9 +18,12 @@ * ##% */ package org.codelutin.option; +import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; -import org.codelutin.option.def.ConfigPropertyModifier; -import static org.codelutin.option.def.ConfigPropertyModifier.*; +import static org.codelutin.option.def.ConfigPropertyModifier.FINAL; +import static org.codelutin.option.def.ConfigPropertyModifier.MANDATORY; +import static org.codelutin.option.def.ConfigPropertyModifier.STATIC; +import static org.codelutin.option.def.ConfigPropertyModifier.TRANSIENT; import org.codelutin.util.ConverterUtil; import java.lang.reflect.Modifier; @@ -52,7 +55,7 @@ * * @author chemit */ -public class ConfigPropertyKey { +public class ConfigPropertyKey implements Comparable { public static ConfigPropertyKey newConfigPropertyKey(String key, Class type, int modifiers, String description) { return newConfigPropertyKey(key, type, modifiers, description, null); @@ -69,19 +72,24 @@ } /** la clef non typée de la propriété */ - final String key; + protected final String key; /** Le type de la valeur de la propriété */ - final Class type; + protected final Class type; /** les modifiers de la propriété */ - final int modifiers; + protected final int modifiers; /** la description de la propriété */ protected final String description; /** La valeur par défaut (si elle existe) pour cette propriété */ - final T defaultValue; + protected final T defaultValue; + + /** L'ancienne valeur pour cette propriété (depuis dernière sauvegarde) */ + protected T oldValue; + /** La valeur courante pour cette propriété */ + protected T currentValue; /** @return le type de la valeur de la propriété */ public Class getType() { @@ -103,6 +111,26 @@ return defaultValue; } + public boolean isModified() { + return !equals(oldValue, currentValue); + } + + public T getCurrentValue() { + return currentValue; + } + + public T getOldValue() { + return oldValue; + } + + public void setCurrentValue(T newValue) { + if (equals(currentValue, newValue)) { + return; + } + setOldValue(currentValue); + currentValue = newValue; + } + /** * @return true si la propriété est final (i.e ne * peut être modifiée) @@ -129,6 +157,79 @@ return MANDATORY.isUsed(modifiers); } + public int compareTo(ConfigPropertyKey o) { + return key.compareTo(o.getKey()); + } + + @Override + public String toString() { + return '[' + key + ']' + (isModified() ? " *" : "") + " "; + } + + public String toString2() { + return toString() + (isModified() && oldValue != null ? "(old:" + oldValue + ") " : "") + "= " + currentValue; + } + + public void clear(boolean clearCurrentValue) { + oldValue = null; + if (clearCurrentValue) { + currentValue = null; + } + } + + public void clearModified() { + oldValue = currentValue; + } + + public void reset() { + if (isModified()) { + currentValue = oldValue; + clear(false); + } + } + + /** + * Convert the given o object to the type klass + * + * @param klass the class of value to extract + * @param o the original value + * @return the converted value + */ + @SuppressWarnings({"unchecked"}) + protected T convert(Class klass, Object o) { + if (o == null) { + return null; + } + T result; + if (klass == String.class) { + result = (T) o; + } else { + if (o.getClass() == String.class) { + result = (T) ConvertUtils.convert(String.valueOf(o), klass); + } else { + result = (T) o; + } + } + if (result != null && !klass.isAssignableFrom(result.getClass())) { + result = null; + } + return result; + } + + private void setOldValue(T oldValue) { + if (equals(this.oldValue, oldValue)) { + return; + } + this.oldValue = oldValue; + System.out.println("setOldValue " + oldValue + " : " + this); + } + + private boolean equals(T oldV, T newV) { + if (oldV == null) { + return newV == null; + } + return oldV.equals(newV); + } private ConfigPropertyKey(String key, Class type, int modifiers, String description, T defaultValue) { this.key = key;