diff options
author | Roel Spilker <r.spilker@gmail.com> | 2014-01-19 02:24:33 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2014-01-19 02:24:33 +0100 |
commit | 34152213226d6407fd922127408a1a44456fe657 (patch) | |
tree | 9316b9d11a886fe6f79a7de6390bb3518214b89c /src/core | |
parent | 513a27f746e5744fe800147e274ed6cfac90f45c (diff) | |
download | lombok-34152213226d6407fd922127408a1a44456fe657.tar.gz lombok-34152213226d6407fd922127408a1a44456fe657.tar.bz2 lombok-34152213226d6407fd922127408a1a44456fe657.zip |
[configuration] only allow a keyname to be registered once
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lombok/core/configuration/ConfigurationKey.java | 77 | ||||
-rw-r--r-- | src/core/lombok/core/configuration/StringConfigurationSource.java | 57 |
2 files changed, 48 insertions, 86 deletions
diff --git a/src/core/lombok/core/configuration/ConfigurationKey.java b/src/core/lombok/core/configuration/ConfigurationKey.java index 694a0054..c22ec022 100644 --- a/src/core/lombok/core/configuration/ConfigurationKey.java +++ b/src/core/lombok/core/configuration/ConfigurationKey.java @@ -24,7 +24,6 @@ package lombok.core.configuration; import java.util.Collections; import java.util.Iterator; import java.util.Map; -import java.util.Map.Entry; import java.util.TreeMap; import java.util.regex.Pattern; @@ -39,8 +38,8 @@ import java.util.regex.Pattern; public abstract class ConfigurationKey<T> { private static final Pattern VALID_NAMES = Pattern.compile("[\\-_a-zA-Z][\\-\\.\\w]*(?<![\\.\\-])"); - private static final TreeMap<String, ConfigurationDataType> registeredKeys = new TreeMap<String, ConfigurationDataType>(String.CASE_INSENSITIVE_ORDER); - private static Map<String, ConfigurationDataType> copy; + private static final TreeMap<String, ConfigurationKey<?>> registeredKeys = new TreeMap<String, ConfigurationKey<?>>(String.CASE_INSENSITIVE_ORDER); + private static Map<String, ConfigurationKey<?>> copy; private final String keyName; private final ConfigurationDataType type; @@ -51,12 +50,7 @@ public abstract class ConfigurationKey<T> { ConfigurationDataType type = ConfigurationDataType.toDataType((Class<? extends ConfigurationKey<?>>)getClass()); this.type = type; - registerKey(keyName, type); - } - - private ConfigurationKey(String keyName, ConfigurationDataType type) { - this.keyName = keyName; - this.type = type; + registerKey(keyName, this); } public final String getKeyName() { @@ -67,24 +61,8 @@ public abstract class ConfigurationKey<T> { return type; } - @Override - public final int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + keyName.hashCode(); - result = prime * result + type.hashCode(); - return result; - } - - /** - * Two configuration are considered equal if and only if their {@code keyName} and {@code type} are equal. - */ - @Override - public final boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof ConfigurationKey)) return false; - ConfigurationKey<?> other = (ConfigurationKey<?>) obj; - return keyName.equals(other.keyName) && type.equals(other.type); + @Override public String toString() { + return keyName + " : " + type; } private static String checkName(String keyName) { @@ -97,52 +75,31 @@ public abstract class ConfigurationKey<T> { * Returns a copy of the currently registered keys. */ @SuppressWarnings("unchecked") - public static Map<String, ConfigurationDataType> registeredKeysAsMap() { + public static Map<String, ConfigurationKey<?>> registeredKeysMap() { synchronized (registeredKeys) { - if (copy == null) copy = Collections.unmodifiableMap((Map<String, ConfigurationDataType>) registeredKeys.clone()); + if (copy == null) copy = Collections.unmodifiableMap((Map<String, ConfigurationKey<?>>) registeredKeys.clone()); return copy; } } + + /** + * Returns a copy of the currently registered keys. + */ public static Iterable<ConfigurationKey<?>> registeredKeys() { - class LocalConfigurationKey extends ConfigurationKey<Object> { - public LocalConfigurationKey(Entry<String, ConfigurationDataType> entry) { - super(entry.getKey(), entry.getValue()); - } - } - final Map<String, ConfigurationDataType> map = registeredKeysAsMap(); + final Map<String, ConfigurationKey<?>> map = registeredKeysMap(); return new Iterable<ConfigurationKey<?>>() { @Override public Iterator<ConfigurationKey<?>> iterator() { - final Iterator<Entry<String, ConfigurationDataType>> entries = map.entrySet().iterator(); - return new Iterator<ConfigurationKey<?>>() { - @Override - public boolean hasNext() { - return entries.hasNext(); - } - - @Override public ConfigurationKey<?> next() { - return new LocalConfigurationKey(entries.next()); - } - - @Override public void remove() { - throw new UnsupportedOperationException(); - } - }; + return map.values().iterator(); } }; } - private static void registerKey(String keyName, ConfigurationDataType type) { + private static void registerKey(String keyName, ConfigurationKey<?> key) { synchronized (registeredKeys) { - ConfigurationDataType existingType = registeredKeys.get(keyName); - if (existingType == null) { - registeredKeys.put(keyName, type); - copy = null; - return; - } - if (!existingType.equals(type)) { - throw new IllegalArgumentException("Key '" + keyName + "' already registered with a different type, existing " + existingType + " != provided " + type); - } + if (registeredKeys.containsKey(keyName)) throw new IllegalArgumentException("Key '" + keyName + "' already registered"); + registeredKeys.put(keyName, key); + copy = null; } } }
\ No newline at end of file diff --git a/src/core/lombok/core/configuration/StringConfigurationSource.java b/src/core/lombok/core/configuration/StringConfigurationSource.java index d4de8ae1..25889055 100644 --- a/src/core/lombok/core/configuration/StringConfigurationSource.java +++ b/src/core/lombok/core/configuration/StringConfigurationSource.java @@ -41,44 +41,49 @@ public class StringConfigurationSource implements ConfigurationSource { Map<String, Result> values = new TreeMap<String, Result>(String.CASE_INSENSITIVE_ORDER); - Map<String, ConfigurationDataType> registeredKeys = ConfigurationKey.registeredKeysAsMap(); + Map<String, ConfigurationKey<?>> registeredKeys = ConfigurationKey.registeredKeysMap(); int lineNumber = 0; for (String line : content.split("\\n")) { line = line.trim(); lineNumber++; if (line.isEmpty() || line.startsWith("#")) continue; + Matcher matcher = LINE.matcher(line); + if (!matcher.matches()) { + reporter.report("No valid line", lineNumber, line); + continue; + } String operator = null; String keyName = null; String value; - - if (matcher.matches()) { - if (matcher.group(1) == null) { - keyName = matcher.group(2); - operator = matcher.group(3); - value = matcher.group(4); - } else { - keyName = matcher.group(1); - operator = "clear"; - value = null; - } - ConfigurationDataType type = registeredKeys.get(keyName); - if (type == null) { - reporter.report("Unknown key '" + keyName + "'", lineNumber, line); - } else { - boolean listOperator = operator.equals("+=") || operator.equals("-="); - if (listOperator && !type.isList()) { - reporter.report("'" + keyName + "' is not a list and doesn't support " + operator + " (only = and clear)", lineNumber, line); - } else if (operator.equals("=") && type.isList()) { - reporter.report("'" + keyName + "' is a list and cannot be assigned to (use +=, -= and clear instead)", lineNumber, line); - } else { - processResult(values, keyName, operator, value, type, reporter, lineNumber, line); - } - } + if (matcher.group(1) == null) { + keyName = matcher.group(2); + operator = matcher.group(3); + value = matcher.group(4); } else { - reporter.report("No valid line", lineNumber, line); + keyName = matcher.group(1); + operator = "clear"; + value = null; } + ConfigurationKey<?> key = registeredKeys.get(keyName); + if (key == null) { + reporter.report("Unknown key '" + keyName + "'", lineNumber, line); + continue; + } + + ConfigurationDataType type = key.getType(); + boolean listOperator = operator.equals("+=") || operator.equals("-="); + if (listOperator && !type.isList()) { + reporter.report("'" + keyName + "' is not a list and doesn't support " + operator + " (only = and clear)", lineNumber, line); + continue; + } + if (operator.equals("=") && type.isList()) { + reporter.report("'" + keyName + "' is a list and cannot be assigned to (use +=, -= and clear instead)", lineNumber, line); + continue; + } + + processResult(values, keyName, operator, value, type, reporter, lineNumber, line); } return new StringConfigurationSource(values); |