From 34152213226d6407fd922127408a1a44456fe657 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 19 Jan 2014 02:24:33 +0100 Subject: [configuration] only allow a keyname to be registered once --- .../core/configuration/ConfigurationKey.java | 77 +++++----------------- .../configuration/StringConfigurationSource.java | 57 ++++++++-------- 2 files changed, 48 insertions(+), 86 deletions(-) (limited to 'src') 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 { private static final Pattern VALID_NAMES = Pattern.compile("[\\-_a-zA-Z][\\-\\.\\w]*(? registeredKeys = new TreeMap(String.CASE_INSENSITIVE_ORDER); - private static Map copy; + private static final TreeMap> registeredKeys = new TreeMap>(String.CASE_INSENSITIVE_ORDER); + private static Map> copy; private final String keyName; private final ConfigurationDataType type; @@ -51,12 +50,7 @@ public abstract class ConfigurationKey { ConfigurationDataType type = ConfigurationDataType.toDataType((Class>)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 { 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 { * Returns a copy of the currently registered keys. */ @SuppressWarnings("unchecked") - public static Map registeredKeysAsMap() { + public static Map> registeredKeysMap() { synchronized (registeredKeys) { - if (copy == null) copy = Collections.unmodifiableMap((Map) registeredKeys.clone()); + if (copy == null) copy = Collections.unmodifiableMap((Map>) registeredKeys.clone()); return copy; } } + + /** + * Returns a copy of the currently registered keys. + */ public static Iterable> registeredKeys() { - class LocalConfigurationKey extends ConfigurationKey { - public LocalConfigurationKey(Entry entry) { - super(entry.getKey(), entry.getValue()); - } - } - final Map map = registeredKeysAsMap(); + final Map> map = registeredKeysMap(); return new Iterable>() { @Override public Iterator> iterator() { - final Iterator> entries = map.entrySet().iterator(); - return new Iterator>() { - @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 values = new TreeMap(String.CASE_INSENSITIVE_ORDER); - Map registeredKeys = ConfigurationKey.registeredKeysAsMap(); + Map> 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); -- cgit