From 5c32e54226efab50bb45494d66c114ffd409afc5 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Mon, 30 Apr 2012 16:54:00 -0500 Subject: Making MemorySection much more efficient; Addresses BUKKIT-1454 --- .../bukkit/configuration/MemoryConfiguration.java | 18 +- .../org/bukkit/configuration/MemorySection.java | 457 +++++---------------- .../configuration/file/FileConfiguration.java | 23 +- .../configuration/file/YamlConfiguration.java | 13 +- .../file/YamlConfigurationOptions.java | 7 +- .../serialization/ConfigurationSerialization.java | 5 +- 6 files changed, 137 insertions(+), 386 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/bukkit/configuration/MemoryConfiguration.java b/src/main/java/org/bukkit/configuration/MemoryConfiguration.java index d26a2984..7eaeff34 100644 --- a/src/main/java/org/bukkit/configuration/MemoryConfiguration.java +++ b/src/main/java/org/bukkit/configuration/MemoryConfiguration.java @@ -2,6 +2,8 @@ package org.bukkit.configuration; import java.util.Map; +import org.apache.commons.lang.Validate; + /** * This is a {@link Configuration} implementation that does not save or load * from any source, and stores all values in memory only. @@ -29,9 +31,7 @@ public class MemoryConfiguration extends MemorySection implements Configuration @Override public void addDefault(String path, Object value) { - if (path == null) { - throw new IllegalArgumentException("Path may not be null"); - } + Validate.notNull(path, "Path may not be null"); if (defaults == null) { defaults = new MemoryConfiguration(); @@ -41,9 +41,7 @@ public class MemoryConfiguration extends MemorySection implements Configuration } public void addDefaults(Map defaults) { - if (defaults == null) { - throw new IllegalArgumentException("Defaults may not be null"); - } + Validate.notNull(defaults, "Defaults may not be null"); for (Map.Entry entry : defaults.entrySet()) { addDefault(entry.getKey(), entry.getValue()); @@ -51,17 +49,13 @@ public class MemoryConfiguration extends MemorySection implements Configuration } public void addDefaults(Configuration defaults) { - if (defaults == null) { - throw new IllegalArgumentException("Defaults may not be null"); - } + Validate.notNull(defaults, "Defaults may not be null"); addDefaults(defaults.getValues(true)); } public void setDefaults(Configuration defaults) { - if (defaults == null) { - throw new IllegalArgumentException("Defaults may not be null"); - } + Validate.notNull(defaults, "Defaults may not be null"); this.defaults = defaults; } diff --git a/src/main/java/org/bukkit/configuration/MemorySection.java b/src/main/java/org/bukkit/configuration/MemorySection.java index eb1a387f..24a317e9 100644 --- a/src/main/java/org/bukkit/configuration/MemorySection.java +++ b/src/main/java/org/bukkit/configuration/MemorySection.java @@ -6,7 +6,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; +import org.apache.commons.lang.Validate; import org.bukkit.OfflinePlayer; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; @@ -49,20 +49,14 @@ public class MemorySection implements ConfigurationSection { * @throws IllegalArgumentException Thrown is parent or path is null, or if parent contains no root Configuration. */ protected MemorySection(ConfigurationSection parent, String path) { - if (parent == null) { - throw new IllegalArgumentException("Parent cannot be null"); - } - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } + Validate.notNull(parent, "Parent cannot be null"); + Validate.notNull(path, "Path cannot be null"); this.path = path; this.parent = parent; this.root = parent.getRoot(); - if (root == null) { - throw new IllegalArgumentException("Path cannot be orphaned"); - } + Validate.notNull(root, "Path cannot be orphaned"); this.fullPath = createPath(parent, path); } @@ -70,7 +64,8 @@ public class MemorySection implements ConfigurationSection { public Set getKeys(boolean deep) { Set result = new LinkedHashSet(); - if (getRoot().options().copyDefaults()) { + Configuration root = getRoot(); + if (root != null && root.options().copyDefaults()) { ConfigurationSection defaults = getDefaultSection(); if (defaults != null) { @@ -86,7 +81,8 @@ public class MemorySection implements ConfigurationSection { public Map getValues(boolean deep) { Map result = new LinkedHashMap(); - if (getRoot().options().copyDefaults()) { + Configuration root = getRoot(); + if (root != null && root.options().copyDefaults()) { ConfigurationSection defaults = getDefaultSection(); if (defaults != null) { @@ -100,23 +96,18 @@ public class MemorySection implements ConfigurationSection { } public boolean contains(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - return get(path) != null; } public boolean isSet(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); + Configuration root = getRoot(); + if (root == null) { + return false; } - - if (getRoot().options().copyDefaults()) { + if (root.options().copyDefaults()) { return contains(path); - } else { - return get(path, null) != null; } + return get(path, null) != null; } public String getCurrentPath() { @@ -136,23 +127,21 @@ public class MemorySection implements ConfigurationSection { } public void addDefault(String path, Object value) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } + Validate.notNull(path, "Path cannot be null"); + Configuration root = getRoot(); if (root == null) { - throw new IllegalStateException("Cannot set default on orphaned section"); - } else { - root.addDefault(createPath(this, path), value); + throw new IllegalStateException("Cannot add default without root"); } + if (root == this) { + throw new UnsupportedOperationException("Unsupported addDefault(String, Object) implementation"); + } + root.addDefault(createPath(this, path), value); } public ConfigurationSection getDefaultSection() { - if (getRoot() == null) { - return null; - } - - Configuration defaults = getRoot().getDefaults(); + Configuration root = getRoot(); + Configuration defaults = root == null ? null : root.getDefaults(); if (defaults != null) { if (defaults.isConfigurationSection(getCurrentPath())) { @@ -164,25 +153,29 @@ public class MemorySection implements ConfigurationSection { } public void set(String path, Object value) { - String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); - ConfigurationSection section = this; + Validate.notEmpty(path, "Cannot set to an empty path"); - if (path.length() == 0) { - throw new IllegalArgumentException("Cannot set to an empty path"); + Configuration root = getRoot(); + if (root == null) { + throw new IllegalStateException("Cannot use section without a root"); } - for (int i = 0; i < split.length - 1; i++) { - ConfigurationSection last = section; - - section = last.getConfigurationSection(split[i]); - - if (section == null) { - section = last.createSection(split[i]); + final char separator = root.options().pathSeparator(); + // i1 is the leading (higher) index + // i2 is the trailing (lower) index + int i1 = -1, i2; + ConfigurationSection section = this; + while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { + String node = path.substring(i2, i1); + ConfigurationSection subSection = section.getConfigurationSection(node); + if (subSection == null) { + section = section.createSection(node); + } else { + section = subSection; } } - String key = split[split.length - 1]; - + String key = path.substring(i2); if (section == this) { if (value == null) { map.remove(key); @@ -195,70 +188,70 @@ public class MemorySection implements ConfigurationSection { } public Object get(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - return get(path, getDefault(path)); } public Object get(String path, Object def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } else if (path.length() == 0) { + Validate.notNull(path, "Path cannot be null"); + + if (path.length() == 0) { return this; } - Object result = null; - String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); - ConfigurationSection section = this; - - for (int i = 0; i < split.length - 1; i++) { - section = section.getConfigurationSection(split[i]); + Configuration root = getRoot(); + if (root == null) { + throw new IllegalStateException("Cannot access section without a root"); + } + final char separator = root.options().pathSeparator(); + // i1 is the leading (higher) index + // i2 is the trailing (lower) index + int i1 = -1, i2; + ConfigurationSection section = this; + while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { + section = section.getConfigurationSection(path.substring(i2, i1)); if (section == null) { return def; } } - String key = split[split.length - 1]; - + String key = path.substring(i2); if (section == this) { - result = map.get(key); + Object result = map.get(key); return (result == null) ? def : result; } return section.get(key, def); } public ConfigurationSection createSection(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } else if (path.length() == 0) { - throw new IllegalArgumentException("Cannot create section at empty path"); + Validate.notEmpty(path, "Cannot create section at empty path"); + Configuration root = getRoot(); + if (root == null) { + throw new IllegalStateException("Cannot create section without a root"); } - String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); + final char separator = root.options().pathSeparator(); + // i1 is the leading (higher) index + // i2 is the trailing (lower) index + int i1 = -1, i2; ConfigurationSection section = this; - - for (int i = 0; i < split.length - 1; i++) { - ConfigurationSection last = section; - - section = last.getConfigurationSection(split[i]); - - if (section == null) { - section = last.createSection(split[i]); + while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) { + String node = path.substring(i2, i1); + ConfigurationSection subSection = section.getConfigurationSection(node); + if (subSection == null) { + section = section.createSection(node); + } else { + section = subSection; } } - String key = split[split.length - 1]; - + String key = path.substring(i2); if (section == this) { ConfigurationSection result = new MemorySection(this, key); map.put(key, result); return result; - } else { - return section.createSection(key); } + return section.createSection(key); } public ConfigurationSection createSection(String path, Map map) { @@ -277,173 +270,97 @@ public class MemorySection implements ConfigurationSection { // Primitives public String getString(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getString(path, def != null ? def.toString() : null); } public String getString(String path, String def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val != null) ? val.toString() : def; } public boolean isString(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof String; } public int getInt(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getInt(path, (def instanceof Number) ? toInt(def) : 0); } public int getInt(String path, int def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof Number) ? toInt(val) : def; } public boolean isInt(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof Integer; } public boolean getBoolean(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getBoolean(path, (def instanceof Boolean) ? (Boolean) def : false); } public boolean getBoolean(String path, boolean def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof Boolean) ? (Boolean) val : def; } public boolean isBoolean(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof Boolean; } public double getDouble(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getDouble(path, (def instanceof Number) ? toDouble(def) : 0); } public double getDouble(String path, double def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof Number) ? toDouble(val) : def; } public boolean isDouble(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof Double; } public long getLong(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getLong(path, (def instanceof Number) ? toLong(def) : 0); } public long getLong(String path, long def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof Number) ? toLong(val) : def; } public boolean isLong(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof Long; } // Java public List getList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getList(path, (def instanceof List) ? (List) def : null); } public List getList(String path, List def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (List) ((val instanceof List) ? val : def); } public boolean isList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof List; } public List getStringList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -462,10 +379,6 @@ public class MemorySection implements ConfigurationSection { } public List getIntegerList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -482,20 +395,10 @@ public class MemorySection implements ConfigurationSection { result.add(Integer.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Integer) (int) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Integer) (int) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Integer) (int) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Integer) (int) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Integer) (int) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Integer) (int) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Integer) (int) (double) (Double) object); + result.add((int) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).intValue()); } } @@ -503,10 +406,6 @@ public class MemorySection implements ConfigurationSection { } public List getBooleanList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -531,10 +430,6 @@ public class MemorySection implements ConfigurationSection { } public List getDoubleList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -551,20 +446,10 @@ public class MemorySection implements ConfigurationSection { result.add(Double.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Double) (double) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Double) (double) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Double) (double) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Double) (double) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Double) (double) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Double) (double) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Double) (double) (double) (Double) object); + result.add((double) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).doubleValue()); } } @@ -572,10 +457,6 @@ public class MemorySection implements ConfigurationSection { } public List getFloatList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -592,20 +473,10 @@ public class MemorySection implements ConfigurationSection { result.add(Float.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Float) (float) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Float) (float) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Float) (float) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Float) (float) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Float) (float) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Float) (float) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Float) (float) (double) (Double) object); + result.add((float) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).floatValue()); } } @@ -613,10 +484,6 @@ public class MemorySection implements ConfigurationSection { } public List getLongList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -633,20 +500,10 @@ public class MemorySection implements ConfigurationSection { result.add(Long.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Long) (long) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Long) (long) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Long) (long) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Long) (long) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Long) (long) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Long) (long) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Long) (long) (double) (Double) object); + result.add((long) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).longValue()); } } @@ -654,10 +511,6 @@ public class MemorySection implements ConfigurationSection { } public List getByteList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -674,20 +527,10 @@ public class MemorySection implements ConfigurationSection { result.add(Byte.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Byte) (byte) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Byte) (byte) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Byte) (byte) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Byte) (byte) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Byte) (byte) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Byte) (byte) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Byte) (byte) (double) (Double) object); + result.add((byte) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).byteValue()); } } @@ -695,10 +538,6 @@ public class MemorySection implements ConfigurationSection { } public List getCharacterList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -716,20 +555,8 @@ public class MemorySection implements ConfigurationSection { if (str.length() == 1) { result.add(str.charAt(0)); } - } else if (object instanceof Byte) { - result.add((Character) (char) (byte) (Byte) object); - } else if (object instanceof Character) { - result.add((Character) (char) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Character) (char) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Character) (char) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Character) (char) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Character) (char) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Character) (char) (double) (Double) object); + } else if (object instanceof Number) { + result.add((char) ((Number) object).intValue()); } } @@ -737,10 +564,6 @@ public class MemorySection implements ConfigurationSection { } public List getShortList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); if (list == null) { @@ -757,20 +580,10 @@ public class MemorySection implements ConfigurationSection { result.add(Short.valueOf((String) object)); } catch (Exception ex) { } - } else if (object instanceof Byte) { - result.add((Short) (short) (byte) (Byte) object); } else if (object instanceof Character) { - result.add((Short) (short) (char) (Character) object); - } else if (object instanceof Short) { - result.add((Short) (short) (short) (Short) object); - } else if (object instanceof Integer) { - result.add((Short) (short) (int) (Integer) object); - } else if (object instanceof Long) { - result.add((Short) (short) (long) (Long) object); - } else if (object instanceof Float) { - result.add((Short) (short) (float) (Float) object); - } else if (object instanceof Double) { - result.add((Short) (short) (double) (Double) object); + result.add((short) ((Character) object).charValue()); + } else if (object instanceof Number) { + result.add(((Number) object).shortValue()); } } @@ -778,10 +591,6 @@ public class MemorySection implements ConfigurationSection { } public List> getMapList(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - List list = getList(path); List> result = new ArrayList>(); @@ -800,91 +609,51 @@ public class MemorySection implements ConfigurationSection { // Bukkit public Vector getVector(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getVector(path, (def instanceof Vector) ? (Vector) def : null); } public Vector getVector(String path, Vector def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof Vector) ? (Vector) val : def; } public boolean isVector(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof Vector; } public OfflinePlayer getOfflinePlayer(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getOfflinePlayer(path, (def instanceof OfflinePlayer) ? (OfflinePlayer) def : null); } public OfflinePlayer getOfflinePlayer(String path, OfflinePlayer def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof OfflinePlayer) ? (OfflinePlayer) val : def; } public boolean isOfflinePlayer(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof OfflinePlayer; } public ItemStack getItemStack(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object def = getDefault(path); return getItemStack(path, (def instanceof ItemStack) ? (ItemStack) def : null); } public ItemStack getItemStack(String path, ItemStack def) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, def); return (val instanceof ItemStack) ? (ItemStack) val : def; } public boolean isItemStack(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof ItemStack; } public ConfigurationSection getConfigurationSection(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path, null); if (val != null) { return (val instanceof ConfigurationSection) ? (ConfigurationSection) val : null; @@ -895,10 +664,6 @@ public class MemorySection implements ConfigurationSection { } public boolean isConfigurationSection(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - Object val = get(path); return val instanceof ConfigurationSection; } @@ -911,11 +676,10 @@ public class MemorySection implements ConfigurationSection { } protected Object getDefault(String path) { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } + Validate.notNull(path, "Path cannot be null"); - Configuration defaults = root.getDefaults(); + Configuration root = getRoot(); + Configuration defaults = root == null ? null : root.getDefaults(); return (defaults == null) ? null : defaults.get(createPath(this, path)); } @@ -986,12 +750,18 @@ public class MemorySection implements ConfigurationSection { * @return Full path of the section from its root. */ public static String createPath(ConfigurationSection section, String key, ConfigurationSection relativeTo) { - StringBuilder builder = new StringBuilder(); + Validate.notNull(section, "Cannot create path without a section"); + Configuration root = section.getRoot(); + if (root == null) { + throw new IllegalStateException("Cannot create path without a root"); + } + char separator = root.options().pathSeparator(); + StringBuilder builder = new StringBuilder(); if (section != null) { for (ConfigurationSection parent = section; (parent != null) && (parent != relativeTo); parent = parent.getParent()) { if (builder.length() > 0) { - builder.insert(0, section.getRoot().options().pathSeparator()); + builder.insert(0, separator); } builder.insert(0, parent.getName()); @@ -1000,7 +770,7 @@ public class MemorySection implements ConfigurationSection { if ((key != null) && (key.length() > 0)) { if (builder.length() > 0) { - builder.append(section.getRoot().options().pathSeparator()); + builder.append(separator); } builder.append(key); @@ -1011,15 +781,14 @@ public class MemorySection implements ConfigurationSection { @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append(getClass().getSimpleName()); - builder.append("[path='"); - builder.append(getCurrentPath()); - builder.append("', root='"); - builder.append(root.getClass().getSimpleName()); - builder.append("']"); - - return builder.toString(); + Configuration root = getRoot(); + return new StringBuilder() + .append(getClass().getSimpleName()) + .append("[path='") + .append(getCurrentPath()) + .append("', root='") + .append(root == null ? null : root.getClass().getSimpleName()) + .append("']") + .toString(); } } diff --git a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java b/src/main/java/org/bukkit/configuration/file/FileConfiguration.java index 4c192639..33aac313 100644 --- a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/FileConfiguration.java @@ -1,6 +1,8 @@ package org.bukkit.configuration.file; import com.google.common.io.Files; + +import org.apache.commons.lang.Validate; import org.bukkit.configuration.InvalidConfigurationException; import java.io.BufferedReader; import java.io.File; @@ -45,9 +47,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown when file is null. */ public void save(File file) throws IOException { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } + Validate.notNull(file, "File cannot be null"); Files.createParentDirs(file); @@ -73,9 +73,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown when file is null. */ public void save(String file) throws IOException { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } + Validate.notNull(file, "File cannot be null"); save(new File(file)); } @@ -102,9 +100,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown when file is null. */ public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } + Validate.notNull(file, "File cannot be null"); load(new FileInputStream(file)); } @@ -121,14 +117,13 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown when stream is null. */ public void load(InputStream stream) throws IOException, InvalidConfigurationException { - if (stream == null) { - throw new IllegalArgumentException("Stream cannot be null"); - } + Validate.notNull(stream, "Stream cannot be null"); InputStreamReader reader = new InputStreamReader(stream); StringBuilder builder = new StringBuilder(); BufferedReader input = new BufferedReader(reader); + try { String line; @@ -158,9 +153,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { * @throws IllegalArgumentException Thrown when file is null. */ public void load(String file) throws FileNotFoundException, IOException, InvalidConfigurationException { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } + Validate.notNull(file, "File cannot be null"); load(new File(file)); } diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index 99bfa1f8..70cb7bdb 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -7,6 +7,7 @@ import java.io.InputStream; import java.util.Map; import java.util.logging.Level; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; @@ -44,9 +45,7 @@ public class YamlConfiguration extends FileConfiguration { @Override public void loadFromString(String contents) throws InvalidConfigurationException { - if (contents == null) { - throw new IllegalArgumentException("Contents cannot be null"); - } + Validate.notNull(contents, "Contents cannot be null"); Map input; try { @@ -167,9 +166,7 @@ public class YamlConfiguration extends FileConfiguration { * @throws IllegalArgumentException Thrown is file is null */ public static YamlConfiguration loadConfiguration(File file) { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } + Validate.notNull(file, "File cannot be null"); YamlConfiguration config = new YamlConfiguration(); @@ -196,9 +193,7 @@ public class YamlConfiguration extends FileConfiguration { * @throws IllegalArgumentException Thrown is stream is null */ public static YamlConfiguration loadConfiguration(InputStream stream) { - if (stream == null) { - throw new IllegalArgumentException("Stream cannot be null"); - } + Validate.notNull(stream, "Stream cannot be null"); YamlConfiguration config = new YamlConfiguration(); diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java b/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java index 46e4d6f9..d2dd712f 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java @@ -1,5 +1,7 @@ package org.bukkit.configuration.file; +import org.apache.commons.lang.Validate; + /** * Various settings for controlling the input and output of a {@link YamlConfiguration} */ @@ -59,9 +61,8 @@ public class YamlConfigurationOptions extends FileConfigurationOptions { * @return This object, for chaining */ public YamlConfigurationOptions indent(int value) { - if ((indent < 2) || (value > 9)) { - throw new IllegalArgumentException("Indent must be between 1 and 10 characters"); - } + Validate.isTrue(value >= 2, "Indent must be at least 2 characters"); + Validate.isTrue(value <= 9, "Indent cannot be greater than 9 characters"); this.indent = value; return this; diff --git a/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java b/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java index b63d1c8b..abe446c2 100644 --- a/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java +++ b/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.commons.lang.Validate; import org.bukkit.configuration.Configuration; import org.bukkit.inventory.ItemStack; import org.bukkit.util.BlockVector; @@ -94,9 +95,7 @@ public class ConfigurationSerialization { } public ConfigurationSerializable deserialize(Map args) { - if (args == null) { - throw new IllegalArgumentException("Args must not be null"); - } + Validate.notNull(args, "Args must not be null"); ConfigurationSerializable result = null; Method method = null; -- cgit v1.2.3