From f79248364ebcc4d9aa24b1bc0e418bceaf57805a Mon Sep 17 00:00:00 2001 From: Rafael Chaves <> Date: Sat, 29 Dec 2007 09:09:17 +0000 Subject: [PATCH] honoring contract w.r.t type checking --- .../umlgraph/engine/tests/SettingTests.java | 3 +- .../umlgraph/settings/SettingDefinitions.java | 4 +- .../src/org/umlgraph/settings/Settings.java | 419 +++++++++--------- 3 files changed, 213 insertions(+), 213 deletions(-) diff --git a/umlgraph_ng/org.umlgraph.engine.tests/src/org/umlgraph/engine/tests/SettingTests.java b/umlgraph_ng/org.umlgraph.engine.tests/src/org/umlgraph/engine/tests/SettingTests.java index ac11369..28babaa 100644 --- a/umlgraph_ng/org.umlgraph.engine.tests/src/org/umlgraph/engine/tests/SettingTests.java +++ b/umlgraph_ng/org.umlgraph.engine.tests/src/org/umlgraph/engine/tests/SettingTests.java @@ -88,7 +88,7 @@ public class SettingTests extends TestCase { Settings root = new Settings(TestBasicDefinitions.class); root.set("@option1", "foo"); root.set("context1@option2", "bar"); - root.set("context1/context2@option3", "zoo"); + root.set("context1/context2@option3", true); assertTrue(root.nodeExists("")); assertTrue(root.nodeExists("/")); @@ -113,7 +113,6 @@ public class SettingTests extends TestCase { Settings root = new Settings(TestBasicDefinitions.class); root.set("@option2", "foo"); root.set("context1/@option3", Boolean.FALSE); - root.set("context1/@option4", 42); root.set("context1/context2/@option1", "zoo"); root.set("context1/context2/@option2", "bar"); root.set("context1/context2/@option3", Boolean.TRUE); diff --git a/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/SettingDefinitions.java b/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/SettingDefinitions.java index 8c23c00..bc028c0 100644 --- a/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/SettingDefinitions.java +++ b/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/SettingDefinitions.java @@ -3,8 +3,8 @@ package org.umlgraph.settings; /** * A tag interface for classes defining settings. - * Classes implementing tis interface are expected to - * define possible settings as public fields. Names of fields + * Classes implementing this interface are expected to + * define possible settings as public static fields. Names of fields * are expected to be unique across different definition classes. * In case a default value is desired, Java's field default value * mechanism can be used. diff --git a/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/Settings.java b/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/Settings.java index d32a3d7..ee2be9b 100644 --- a/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/Settings.java +++ b/umlgraph_ng/org.umlgraph.settings/src/org/umlgraph/settings/Settings.java @@ -1,6 +1,5 @@ package org.umlgraph.settings; - import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; @@ -28,237 +27,239 @@ import java.util.Map; */ public class Settings { - private Class[] definitions; + private Class[] definitions; - /* - * The child setting nodes. - */ - private Map children = new HashMap(); - /* - * The setting values defined for this setting node. - */ - private Map values = new HashMap(); + /* + * The child setting nodes. + */ + private Map children = new HashMap(); + /* + * The setting values defined for this setting node. + */ + private Map values = new HashMap(); - private Settings parent; + private Settings parent; - /** - * Constructs a root settings object. - * - * @param definitions - * the setting definition classes - */ - public Settings(Class... definitions) { - if (definitions.length == 0) - throw new IllegalArgumentException("no definitions provided"); - this.definitions = definitions; - } + /** + * Constructs a root settings object. + * + * @param definitions + * the setting definition classes + */ + public Settings(Class... definitions) { + if (definitions.length == 0) + throw new IllegalArgumentException("no definitions provided"); + this.definitions = definitions; + } - private Settings(Settings parent) { - this.parent = parent; - } + private Settings(Settings parent) { + this.parent = parent; + } - /** - * Returns the setting node corresponding to the given path. If the node - * does not exist, returns a new empty node. - * - * @param path - * the setting node path - * @return the setting node corresponding to the given path - */ - public Settings node(String[] path) { - return createNode(path, 0); - } + /** + * Returns the setting node corresponding to the given path. If the node + * does not exist, returns a new empty node. + * + * @param path + * the setting node path + * @return the setting node corresponding to the given path + */ + public Settings node(String[] path) { + return createNode(path, 0); + } - public Settings node(String path) { - return node(new SettingKey(path).getNodePath()); - } + public Settings node(String path) { + return node(new SettingKey(path).getNodePath()); + } - /** - * Creates a setting node under the given path, or returns the existing one. - */ - private Settings createNode(String[] path, int start) { - if (path.length == start) - return this; - Settings found = children.get(path[start]); - if (found == null) - children.put(path[start], found = new Settings(this)); - if (start == path.length - 1) - return found; - return found.createNode(path, start + 1); - } + /** + * Creates a setting node under the given path, or returns the existing one. + */ + private Settings createNode(String[] path, int start) { + if (path.length == start) + return this; + Settings found = children.get(path[start]); + if (found == null) + children.put(path[start], found = new Settings(this)); + if (start == path.length - 1) + return found; + return found.createNode(path, start + 1); + } - /** - * Returns whether the given node exists. - */ - public boolean nodeExists(String[] path) { - return existingNode(path) != null; - } + /** + * Returns whether the given node exists. + */ + public boolean nodeExists(String[] path) { + return existingNode(path) != null; + } - public boolean nodeExists(String path) { - return nodeExists(new SettingKey(path).getNodePath()); - } + public boolean nodeExists(String path) { + return nodeExists(new SettingKey(path).getNodePath()); + } - /** - * Returns an existing setting node corresponding to the given path. - * - * @param path - * the setting path - * @return the settings object, or null - */ - public Settings existingNode(String[] segments) { - Settings current = this; - for (String segment : segments) { - current = current.children.get(segment); - if (current == null) - return null; - } - return current; - } + /** + * Returns an existing setting node corresponding to the given path. + * + * @param path + * the setting path + * @return the settings object, or null + */ + public Settings existingNode(String[] segments) { + Settings current = this; + for (String segment : segments) { + current = current.children.get(segment); + if (current == null) + return null; + } + return current; + } - public Settings existingNode(String path) { - return existingNode(new SettingKey(path).getNodePath()); - } + public Settings existingNode(String path) { + return existingNode(new SettingKey(path).getNodePath()); + } - /** - * Returns the value of the option corresponding to the given key. If - * acceptDefault is true and a value for the option does not - * exist, returns the default value. Otherwise, returns null. - * - * @param key - * the option key - * @param acceptDefault - * whether a default value should be returned - * @return the value for the given key, or null - */ - public T get(SettingKey key, boolean acceptDefault) { - if (!acceptDefault) { - Settings node = existingNode(key.getNodePath()); - return node == null ? null : (T) node.values.get(key.getKey()); - } - Settings node = lastExistingNode(key.getNodePath(), 0); - return node.searchValue(key.getKey()); - } + /** + * Returns the value of the option corresponding to the given key. If + * acceptDefault is true and a value for the option does not + * exist, returns the default value. Otherwise, returns null. + * + * @param key + * the option key + * @param acceptDefault + * whether a default value should be returned + * @return the value for the given key, or null + */ + public T get(SettingKey key, boolean acceptDefault) { + if (!acceptDefault) { + Settings node = existingNode(key.getNodePath()); + return node == null ? null : (T) node.values.get(key.getKey()); + } + Settings node = lastExistingNode(key.getNodePath(), 0); + return node.searchValue(key.getKey()); + } - private T searchValue(String key) { - if (!isDefined(key)) - throw new IllegalArgumentException("unknown setting: " + key); - T ownValue = (T) values.get(key); - if (ownValue != null) - return ownValue; - return (T) (parent == null ? getDefaultValue(key) : parent.searchValue(key)); - } + private T searchValue(String key) { + if (!isDefined(key)) + throw new IllegalArgumentException("unknown setting: " + key); + T ownValue = (T) values.get(key); + if (ownValue != null) + return ownValue; + return (T) (parent == null ? getDefaultValue(key) : parent + .searchValue(key)); + } - private boolean isDefined(String key) { - return findField(key) != null; - } + private boolean isDefined(String key) { + return findField(key) != null; + } - private T getDefaultValue(String key) { - Field field = findField(key); - if (field == null) - throw new IllegalArgumentException( - "unknown setting or undefined default: " + key); - try { - return (T) field.get(null); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException("unknown setting: " + key); - } - } + private T getDefaultValue(String key) { + Field field = findField(key); + if (field == null) + throw new IllegalArgumentException( + "unknown setting or undefined default: " + key); + try { + return (T) field.get(null); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException("unknown setting: " + key); + } + } - private Field findField(String key) { - if (parent != null) - return parent.findField(key); - for (Class definitionClass : definitions) - try { - Field field = definitionClass.getDeclaredField(key); - if (!field.getType().isPrimitive() && Modifier.isStatic(field.getModifiers())) - return field; - } catch (NoSuchFieldException e) { - // keep looking... - } - return null; - } + private Field findField(String key) { + if (parent != null) + return parent.findField(key); + for (Class definitionClass : definitions) + try { + Field field = definitionClass.getDeclaredField(key); + if (!field.getType().isPrimitive() + && Modifier.isStatic(field.getModifiers())) + return field; + } catch (NoSuchFieldException e) { + // keep looking... + } + return null; + } - private Settings lastExistingNode(String[] nodePath, int start) { - if (start == nodePath.length) - return this; - Settings immediateChild = children.get(nodePath[start]); - return immediateChild == null ? this : immediateChild.lastExistingNode( - nodePath, start + 1); - } + private Settings lastExistingNode(String[] nodePath, int start) { + if (start == nodePath.length) + return this; + Settings immediateChild = children.get(nodePath[start]); + return immediateChild == null ? this : immediateChild.lastExistingNode( + nodePath, start + 1); + } - /** - * Returns the value of the option corresponding to the given path. If - * acceptDefault is true and a value for the option does not - * exist, returns the default value. Otherwise, returns null. - * This is just a convenience method for {@link #get(SettingKey, boolean)}. - * - * @param path - * the option path - * @param acceptDefault - * whether a default value should be returned - * @return the value for the given path, or null - */ + /** + * Returns the value of the option corresponding to the given path. If + * acceptDefault is true and a value for the option does not + * exist, returns the default value. Otherwise, returns null. + * This is just a convenience method for {@link #get(SettingKey, boolean)}. + * + * @param path + * the option path + * @param acceptDefault + * whether a default value should be returned + * @return the value for the given path, or null + */ - public T get(String path, boolean acceptDefault) { - return get(new SettingKey(path), acceptDefault); - } + public T get(String path, boolean acceptDefault) { + return get(new SettingKey(path), acceptDefault); + } - public T get(String path) { - return get(new SettingKey(path), true); - } + public T get(String path) { + return get(new SettingKey(path), true); + } - /** - * Sets the value for the given option. - * - * @param key - * the option key - * @param value - * the new value, or null - */ - public void set(SettingKey key, T value) { - if (value == null) { - Settings node = existingNode(key.getNodePath()); - if (node != null) - node.values.remove(key.getKey()); - } else { - Settings node = createNode(key.getNodePath(), 0); - node.values.put(key.getKey(), value); - } - } + /** + * Sets the value for the given option. + * + * @param key + * the option key + * @param value + * the new value, or null + */ + public void set(SettingKey key, T value) { + if (!isDefined(key.getKey())) + throw new IllegalArgumentException("unknown setting: " + key); + if (value == null) { + Settings node = existingNode(key.getNodePath()); + if (node != null) + node.values.remove(key.getKey()); + } else { + Field field = findField(key.getKey()); + if (!field.getType().isAssignableFrom(value.getClass())) + throw new ClassCastException(value.getClass().getName()); + Settings node = createNode(key.getNodePath(), 0); + node.values.put(key.getKey(), value); + } + } - public void set(String path, T value) { - set(new SettingKey(path), value); - } + public void set(String path, T value) { + set(new SettingKey(path), value); + } - public void setToDefault(String path) { - setToDefault(new SettingKey(path)); - } + public void setToDefault(String path) { + setToDefault(new SettingKey(path)); + } - /** - * Sets the given option to its current default value. - * - * @param settingKey - * the key to set to default - */ - public void setToDefault(SettingKey key) { - Object defaultValue = getDefaultValue(key.getKey()); - if (defaultValue == null) - throw new IllegalStateException("no default value for " + key); - set(key, defaultValue); - } + /** + * Sets the given option to its current default value. + * + * @param settingKey + * the key to set to default + */ + public void setToDefault(SettingKey key) { + Object defaultValue = getDefaultValue(key.getKey()); + if (defaultValue == null) + throw new IllegalStateException("no default value for " + key); + set(key, defaultValue); + } - /** - * Returns the parent settings, or null if this is a root - * setting. - * - * @return the parent settings, or null - */ - public Settings getParent() { - return parent; - } - - public static void registerContext( - Class contextType) { - // TODO Auto-generated method stub - } + /** + * Returns the parent settings, or null if this is a root + * setting. + * + * @return the parent settings, or null + */ + public Settings getParent() { + return parent; + } }