13
0
geforkt von Mirrors/Paper

Making MemorySection much more efficient; Addresses BUKKIT-1454

By: Wesley Wolfe <weswolf@aol.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-04-30 16:54:00 -05:00
Ursprung 34681566e0
Commit 8726d93ca1
6 geänderte Dateien mit 137 neuen und 386 gelöschten Zeilen

Datei anzeigen

@ -2,6 +2,8 @@ package org.bukkit.configuration;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.Validate;
/** /**
* This is a {@link Configuration} implementation that does not save or load * This is a {@link Configuration} implementation that does not save or load
* from any source, and stores all values in memory only. * from any source, and stores all values in memory only.
@ -29,9 +31,7 @@ public class MemoryConfiguration extends MemorySection implements Configuration
@Override @Override
public void addDefault(String path, Object value) { public void addDefault(String path, Object value) {
if (path == null) { Validate.notNull(path, "Path may not be null");
throw new IllegalArgumentException("Path may not be null");
}
if (defaults == null) { if (defaults == null) {
defaults = new MemoryConfiguration(); defaults = new MemoryConfiguration();
@ -41,9 +41,7 @@ public class MemoryConfiguration extends MemorySection implements Configuration
} }
public void addDefaults(Map<String, Object> defaults) { public void addDefaults(Map<String, Object> defaults) {
if (defaults == null) { Validate.notNull(defaults, "Defaults may not be null");
throw new IllegalArgumentException("Defaults may not be null");
}
for (Map.Entry<String, Object> entry : defaults.entrySet()) { for (Map.Entry<String, Object> entry : defaults.entrySet()) {
addDefault(entry.getKey(), entry.getValue()); addDefault(entry.getKey(), entry.getValue());
@ -51,17 +49,13 @@ public class MemoryConfiguration extends MemorySection implements Configuration
} }
public void addDefaults(Configuration defaults) { public void addDefaults(Configuration defaults) {
if (defaults == null) { Validate.notNull(defaults, "Defaults may not be null");
throw new IllegalArgumentException("Defaults may not be null");
}
addDefaults(defaults.getValues(true)); addDefaults(defaults.getValues(true));
} }
public void setDefaults(Configuration defaults) { public void setDefaults(Configuration defaults) {
if (defaults == null) { Validate.notNull(defaults, "Defaults may not be null");
throw new IllegalArgumentException("Defaults may not be null");
}
this.defaults = defaults; this.defaults = defaults;
} }

Datei anzeigen

@ -6,7 +6,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import org.apache.commons.lang.Validate;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; 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. * @throws IllegalArgumentException Thrown is parent or path is null, or if parent contains no root Configuration.
*/ */
protected MemorySection(ConfigurationSection parent, String path) { protected MemorySection(ConfigurationSection parent, String path) {
if (parent == null) { Validate.notNull(parent, "Parent cannot be null");
throw new IllegalArgumentException("Parent cannot be null"); Validate.notNull(path, "Path cannot be null");
}
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
this.path = path; this.path = path;
this.parent = parent; this.parent = parent;
this.root = parent.getRoot(); this.root = parent.getRoot();
if (root == null) { Validate.notNull(root, "Path cannot be orphaned");
throw new IllegalArgumentException("Path cannot be orphaned");
}
this.fullPath = createPath(parent, path); this.fullPath = createPath(parent, path);
} }
@ -70,7 +64,8 @@ public class MemorySection implements ConfigurationSection {
public Set<String> getKeys(boolean deep) { public Set<String> getKeys(boolean deep) {
Set<String> result = new LinkedHashSet<String>(); Set<String> result = new LinkedHashSet<String>();
if (getRoot().options().copyDefaults()) { Configuration root = getRoot();
if (root != null && root.options().copyDefaults()) {
ConfigurationSection defaults = getDefaultSection(); ConfigurationSection defaults = getDefaultSection();
if (defaults != null) { if (defaults != null) {
@ -86,7 +81,8 @@ public class MemorySection implements ConfigurationSection {
public Map<String, Object> getValues(boolean deep) { public Map<String, Object> getValues(boolean deep) {
Map<String, Object> result = new LinkedHashMap<String, Object>(); Map<String, Object> result = new LinkedHashMap<String, Object>();
if (getRoot().options().copyDefaults()) { Configuration root = getRoot();
if (root != null && root.options().copyDefaults()) {
ConfigurationSection defaults = getDefaultSection(); ConfigurationSection defaults = getDefaultSection();
if (defaults != null) { if (defaults != null) {
@ -100,23 +96,18 @@ public class MemorySection implements ConfigurationSection {
} }
public boolean contains(String path) { public boolean contains(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
return get(path) != null; return get(path) != null;
} }
public boolean isSet(String path) { public boolean isSet(String path) {
if (path == null) { Configuration root = getRoot();
throw new IllegalArgumentException("Path cannot be null"); if (root == null) {
return false;
} }
if (root.options().copyDefaults()) {
if (getRoot().options().copyDefaults()) {
return contains(path); return contains(path);
} else {
return get(path, null) != null;
} }
return get(path, null) != null;
} }
public String getCurrentPath() { public String getCurrentPath() {
@ -136,23 +127,21 @@ public class MemorySection implements ConfigurationSection {
} }
public void addDefault(String path, Object value) { public void addDefault(String path, Object value) {
if (path == null) { Validate.notNull(path, "Path cannot be null");
throw new IllegalArgumentException("Path cannot be null");
}
Configuration root = getRoot();
if (root == null) { if (root == null) {
throw new IllegalStateException("Cannot set default on orphaned section"); throw new IllegalStateException("Cannot add default without root");
} else {
root.addDefault(createPath(this, path), value);
} }
if (root == this) {
throw new UnsupportedOperationException("Unsupported addDefault(String, Object) implementation");
}
root.addDefault(createPath(this, path), value);
} }
public ConfigurationSection getDefaultSection() { public ConfigurationSection getDefaultSection() {
if (getRoot() == null) { Configuration root = getRoot();
return null; Configuration defaults = root == null ? null : root.getDefaults();
}
Configuration defaults = getRoot().getDefaults();
if (defaults != null) { if (defaults != null) {
if (defaults.isConfigurationSection(getCurrentPath())) { if (defaults.isConfigurationSection(getCurrentPath())) {
@ -164,25 +153,29 @@ public class MemorySection implements ConfigurationSection {
} }
public void set(String path, Object value) { public void set(String path, Object value) {
String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); Validate.notEmpty(path, "Cannot set to an empty path");
Configuration root = getRoot();
if (root == null) {
throw new IllegalStateException("Cannot use 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; ConfigurationSection section = this;
while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
if (path.length() == 0) { String node = path.substring(i2, i1);
throw new IllegalArgumentException("Cannot set to an empty path"); ConfigurationSection subSection = section.getConfigurationSection(node);
} if (subSection == null) {
section = section.createSection(node);
for (int i = 0; i < split.length - 1; i++) { } else {
ConfigurationSection last = section; section = subSection;
section = last.getConfigurationSection(split[i]);
if (section == null) {
section = last.createSection(split[i]);
} }
} }
String key = split[split.length - 1]; String key = path.substring(i2);
if (section == this) { if (section == this) {
if (value == null) { if (value == null) {
map.remove(key); map.remove(key);
@ -195,70 +188,70 @@ public class MemorySection implements ConfigurationSection {
} }
public Object get(String path) { public Object get(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
return get(path, getDefault(path)); return get(path, getDefault(path));
} }
public Object get(String path, Object def) { public Object get(String path, Object def) {
if (path == null) { Validate.notNull(path, "Path cannot be null");
throw new IllegalArgumentException("Path cannot be null");
} else if (path.length() == 0) { if (path.length() == 0) {
return this; return this;
} }
Object result = null; Configuration root = getRoot();
String[] split = path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator()))); 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; ConfigurationSection section = this;
while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
for (int i = 0; i < split.length - 1; i++) { section = section.getConfigurationSection(path.substring(i2, i1));
section = section.getConfigurationSection(split[i]);
if (section == null) { if (section == null) {
return def; return def;
} }
} }
String key = split[split.length - 1]; String key = path.substring(i2);
if (section == this) { if (section == this) {
result = map.get(key); Object result = map.get(key);
return (result == null) ? def : result; return (result == null) ? def : result;
} }
return section.get(key, def); return section.get(key, def);
} }
public ConfigurationSection createSection(String path) { public ConfigurationSection createSection(String path) {
if (path == null) { Validate.notEmpty(path, "Cannot create section at empty path");
throw new IllegalArgumentException("Path cannot be null"); Configuration root = getRoot();
} else if (path.length() == 0) { if (root == null) {
throw new IllegalArgumentException("Cannot create section at empty path"); 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; ConfigurationSection section = this;
while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
for (int i = 0; i < split.length - 1; i++) { String node = path.substring(i2, i1);
ConfigurationSection last = section; ConfigurationSection subSection = section.getConfigurationSection(node);
if (subSection == null) {
section = last.getConfigurationSection(split[i]); section = section.createSection(node);
} else {
if (section == null) { section = subSection;
section = last.createSection(split[i]);
} }
} }
String key = split[split.length - 1]; String key = path.substring(i2);
if (section == this) { if (section == this) {
ConfigurationSection result = new MemorySection(this, key); ConfigurationSection result = new MemorySection(this, key);
map.put(key, result); map.put(key, result);
return result; return result;
} else {
return section.createSection(key);
} }
return section.createSection(key);
} }
public ConfigurationSection createSection(String path, Map<?, ?> map) { public ConfigurationSection createSection(String path, Map<?, ?> map) {
@ -277,173 +270,97 @@ public class MemorySection implements ConfigurationSection {
// Primitives // Primitives
public String getString(String path) { public String getString(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getString(path, def != null ? def.toString() : null); return getString(path, def != null ? def.toString() : null);
} }
public String getString(String path, String def) { public String getString(String path, String def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val != null) ? val.toString() : def; return (val != null) ? val.toString() : def;
} }
public boolean isString(String path) { public boolean isString(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof String; return val instanceof String;
} }
public int getInt(String path) { public int getInt(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getInt(path, (def instanceof Number) ? toInt(def) : 0); return getInt(path, (def instanceof Number) ? toInt(def) : 0);
} }
public int getInt(String path, int def) { public int getInt(String path, int def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof Number) ? toInt(val) : def; return (val instanceof Number) ? toInt(val) : def;
} }
public boolean isInt(String path) { public boolean isInt(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof Integer; return val instanceof Integer;
} }
public boolean getBoolean(String path) { public boolean getBoolean(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getBoolean(path, (def instanceof Boolean) ? (Boolean) def : false); return getBoolean(path, (def instanceof Boolean) ? (Boolean) def : false);
} }
public boolean getBoolean(String path, boolean def) { public boolean getBoolean(String path, boolean def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof Boolean) ? (Boolean) val : def; return (val instanceof Boolean) ? (Boolean) val : def;
} }
public boolean isBoolean(String path) { public boolean isBoolean(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof Boolean; return val instanceof Boolean;
} }
public double getDouble(String path) { public double getDouble(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getDouble(path, (def instanceof Number) ? toDouble(def) : 0); return getDouble(path, (def instanceof Number) ? toDouble(def) : 0);
} }
public double getDouble(String path, double def) { public double getDouble(String path, double def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof Number) ? toDouble(val) : def; return (val instanceof Number) ? toDouble(val) : def;
} }
public boolean isDouble(String path) { public boolean isDouble(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof Double; return val instanceof Double;
} }
public long getLong(String path) { public long getLong(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getLong(path, (def instanceof Number) ? toLong(def) : 0); return getLong(path, (def instanceof Number) ? toLong(def) : 0);
} }
public long getLong(String path, long def) { public long getLong(String path, long def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof Number) ? toLong(val) : def; return (val instanceof Number) ? toLong(val) : def;
} }
public boolean isLong(String path) { public boolean isLong(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof Long; return val instanceof Long;
} }
// Java // Java
public List<?> getList(String path) { public List<?> getList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getList(path, (def instanceof List) ? (List<?>) def : null); return getList(path, (def instanceof List) ? (List<?>) def : null);
} }
public List<?> getList(String path, List<?> def) { public List<?> getList(String path, List<?> def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (List<?>) ((val instanceof List) ? val : def); return (List<?>) ((val instanceof List) ? val : def);
} }
public boolean isList(String path) { public boolean isList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof List; return val instanceof List;
} }
public List<String> getStringList(String path) { public List<String> getStringList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -462,10 +379,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Integer> getIntegerList(String path) { public List<Integer> getIntegerList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -482,20 +395,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Integer.valueOf((String) object)); result.add(Integer.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Integer) (int) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Integer) (int) (char) (Character) object); result.add((int) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Integer) (int) (short) (Short) object); result.add(((Number) object).intValue());
} 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);
} }
} }
@ -503,10 +406,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Boolean> getBooleanList(String path) { public List<Boolean> getBooleanList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -531,10 +430,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Double> getDoubleList(String path) { public List<Double> getDoubleList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -551,20 +446,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Double.valueOf((String) object)); result.add(Double.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Double) (double) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Double) (double) (char) (Character) object); result.add((double) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Double) (double) (short) (Short) object); result.add(((Number) object).doubleValue());
} 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);
} }
} }
@ -572,10 +457,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Float> getFloatList(String path) { public List<Float> getFloatList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -592,20 +473,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Float.valueOf((String) object)); result.add(Float.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Float) (float) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Float) (float) (char) (Character) object); result.add((float) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Float) (float) (short) (Short) object); result.add(((Number) object).floatValue());
} 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);
} }
} }
@ -613,10 +484,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Long> getLongList(String path) { public List<Long> getLongList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -633,20 +500,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Long.valueOf((String) object)); result.add(Long.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Long) (long) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Long) (long) (char) (Character) object); result.add((long) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Long) (long) (short) (Short) object); result.add(((Number) object).longValue());
} 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);
} }
} }
@ -654,10 +511,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Byte> getByteList(String path) { public List<Byte> getByteList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -674,20 +527,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Byte.valueOf((String) object)); result.add(Byte.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Byte) (byte) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Byte) (byte) (char) (Character) object); result.add((byte) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Byte) (byte) (short) (Short) object); result.add(((Number) object).byteValue());
} 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);
} }
} }
@ -695,10 +538,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Character> getCharacterList(String path) { public List<Character> getCharacterList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -716,20 +555,8 @@ public class MemorySection implements ConfigurationSection {
if (str.length() == 1) { if (str.length() == 1) {
result.add(str.charAt(0)); result.add(str.charAt(0));
} }
} else if (object instanceof Byte) { } else if (object instanceof Number) {
result.add((Character) (char) (byte) (Byte) object); result.add((char) ((Number) object).intValue());
} 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);
} }
} }
@ -737,10 +564,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Short> getShortList(String path) { public List<Short> getShortList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
if (list == null) { if (list == null) {
@ -757,20 +580,10 @@ public class MemorySection implements ConfigurationSection {
result.add(Short.valueOf((String) object)); result.add(Short.valueOf((String) object));
} catch (Exception ex) { } catch (Exception ex) {
} }
} else if (object instanceof Byte) {
result.add((Short) (short) (byte) (Byte) object);
} else if (object instanceof Character) { } else if (object instanceof Character) {
result.add((Short) (short) (char) (Character) object); result.add((short) ((Character) object).charValue());
} else if (object instanceof Short) { } else if (object instanceof Number) {
result.add((Short) (short) (short) (Short) object); result.add(((Number) object).shortValue());
} 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);
} }
} }
@ -778,10 +591,6 @@ public class MemorySection implements ConfigurationSection {
} }
public List<Map<?, ?>> getMapList(String path) { public List<Map<?, ?>> getMapList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<?> list = getList(path); List<?> list = getList(path);
List<Map<?, ?>> result = new ArrayList<Map<?, ?>>(); List<Map<?, ?>> result = new ArrayList<Map<?, ?>>();
@ -800,91 +609,51 @@ public class MemorySection implements ConfigurationSection {
// Bukkit // Bukkit
public Vector getVector(String path) { public Vector getVector(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getVector(path, (def instanceof Vector) ? (Vector) def : null); return getVector(path, (def instanceof Vector) ? (Vector) def : null);
} }
public Vector getVector(String path, Vector def) { public Vector getVector(String path, Vector def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof Vector) ? (Vector) val : def; return (val instanceof Vector) ? (Vector) val : def;
} }
public boolean isVector(String path) { public boolean isVector(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof Vector; return val instanceof Vector;
} }
public OfflinePlayer getOfflinePlayer(String path) { public OfflinePlayer getOfflinePlayer(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getOfflinePlayer(path, (def instanceof OfflinePlayer) ? (OfflinePlayer) def : null); return getOfflinePlayer(path, (def instanceof OfflinePlayer) ? (OfflinePlayer) def : null);
} }
public OfflinePlayer getOfflinePlayer(String path, OfflinePlayer def) { public OfflinePlayer getOfflinePlayer(String path, OfflinePlayer def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof OfflinePlayer) ? (OfflinePlayer) val : def; return (val instanceof OfflinePlayer) ? (OfflinePlayer) val : def;
} }
public boolean isOfflinePlayer(String path) { public boolean isOfflinePlayer(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof OfflinePlayer; return val instanceof OfflinePlayer;
} }
public ItemStack getItemStack(String path) { public ItemStack getItemStack(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path); Object def = getDefault(path);
return getItemStack(path, (def instanceof ItemStack) ? (ItemStack) def : null); return getItemStack(path, (def instanceof ItemStack) ? (ItemStack) def : null);
} }
public ItemStack getItemStack(String path, ItemStack def) { public ItemStack getItemStack(String path, ItemStack def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def); Object val = get(path, def);
return (val instanceof ItemStack) ? (ItemStack) val : def; return (val instanceof ItemStack) ? (ItemStack) val : def;
} }
public boolean isItemStack(String path) { public boolean isItemStack(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof ItemStack; return val instanceof ItemStack;
} }
public ConfigurationSection getConfigurationSection(String path) { public ConfigurationSection getConfigurationSection(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, null); Object val = get(path, null);
if (val != null) { if (val != null) {
return (val instanceof ConfigurationSection) ? (ConfigurationSection) val : null; return (val instanceof ConfigurationSection) ? (ConfigurationSection) val : null;
@ -895,10 +664,6 @@ public class MemorySection implements ConfigurationSection {
} }
public boolean isConfigurationSection(String path) { public boolean isConfigurationSection(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path); Object val = get(path);
return val instanceof ConfigurationSection; return val instanceof ConfigurationSection;
} }
@ -911,11 +676,10 @@ public class MemorySection implements ConfigurationSection {
} }
protected Object getDefault(String path) { protected Object getDefault(String path) {
if (path == null) { Validate.notNull(path, "Path cannot be null");
throw new IllegalArgumentException("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)); 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. * @return Full path of the section from its root.
*/ */
public static String createPath(ConfigurationSection section, String key, ConfigurationSection relativeTo) { 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) { if (section != null) {
for (ConfigurationSection parent = section; (parent != null) && (parent != relativeTo); parent = parent.getParent()) { for (ConfigurationSection parent = section; (parent != null) && (parent != relativeTo); parent = parent.getParent()) {
if (builder.length() > 0) { if (builder.length() > 0) {
builder.insert(0, section.getRoot().options().pathSeparator()); builder.insert(0, separator);
} }
builder.insert(0, parent.getName()); builder.insert(0, parent.getName());
@ -1000,7 +770,7 @@ public class MemorySection implements ConfigurationSection {
if ((key != null) && (key.length() > 0)) { if ((key != null) && (key.length() > 0)) {
if (builder.length() > 0) { if (builder.length() > 0) {
builder.append(section.getRoot().options().pathSeparator()); builder.append(separator);
} }
builder.append(key); builder.append(key);
@ -1011,15 +781,14 @@ public class MemorySection implements ConfigurationSection {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); Configuration root = getRoot();
return new StringBuilder()
builder.append(getClass().getSimpleName()); .append(getClass().getSimpleName())
builder.append("[path='"); .append("[path='")
builder.append(getCurrentPath()); .append(getCurrentPath())
builder.append("', root='"); .append("', root='")
builder.append(root.getClass().getSimpleName()); .append(root == null ? null : root.getClass().getSimpleName())
builder.append("']"); .append("']")
.toString();
return builder.toString();
} }
} }

Datei anzeigen

@ -1,6 +1,8 @@
package org.bukkit.configuration.file; package org.bukkit.configuration.file;
import com.google.common.io.Files; import com.google.common.io.Files;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -45,9 +47,7 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown when file is null. * @throws IllegalArgumentException Thrown when file is null.
*/ */
public void save(File file) throws IOException { public void save(File file) throws IOException {
if (file == null) { Validate.notNull(file, "File cannot be null");
throw new IllegalArgumentException("File cannot be null");
}
Files.createParentDirs(file); Files.createParentDirs(file);
@ -73,9 +73,7 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown when file is null. * @throws IllegalArgumentException Thrown when file is null.
*/ */
public void save(String file) throws IOException { public void save(String file) throws IOException {
if (file == null) { Validate.notNull(file, "File cannot be null");
throw new IllegalArgumentException("File cannot be null");
}
save(new File(file)); save(new File(file));
} }
@ -102,9 +100,7 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown when file is null. * @throws IllegalArgumentException Thrown when file is null.
*/ */
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException { public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
if (file == null) { Validate.notNull(file, "File cannot be null");
throw new IllegalArgumentException("File cannot be null");
}
load(new FileInputStream(file)); load(new FileInputStream(file));
} }
@ -121,14 +117,13 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown when stream is null. * @throws IllegalArgumentException Thrown when stream is null.
*/ */
public void load(InputStream stream) throws IOException, InvalidConfigurationException { public void load(InputStream stream) throws IOException, InvalidConfigurationException {
if (stream == null) { Validate.notNull(stream, "Stream cannot be null");
throw new IllegalArgumentException("Stream cannot be null");
}
InputStreamReader reader = new InputStreamReader(stream); InputStreamReader reader = new InputStreamReader(stream);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
BufferedReader input = new BufferedReader(reader); BufferedReader input = new BufferedReader(reader);
try { try {
String line; String line;
@ -158,9 +153,7 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown when file is null. * @throws IllegalArgumentException Thrown when file is null.
*/ */
public void load(String file) throws FileNotFoundException, IOException, InvalidConfigurationException { public void load(String file) throws FileNotFoundException, IOException, InvalidConfigurationException {
if (file == null) { Validate.notNull(file, "File cannot be null");
throw new IllegalArgumentException("File cannot be null");
}
load(new File(file)); load(new File(file));
} }

Datei anzeigen

@ -7,6 +7,7 @@ import java.io.InputStream;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -44,9 +45,7 @@ public class YamlConfiguration extends FileConfiguration {
@Override @Override
public void loadFromString(String contents) throws InvalidConfigurationException { public void loadFromString(String contents) throws InvalidConfigurationException {
if (contents == null) { Validate.notNull(contents, "Contents cannot be null");
throw new IllegalArgumentException("Contents cannot be null");
}
Map<?, ?> input; Map<?, ?> input;
try { try {
@ -167,9 +166,7 @@ public class YamlConfiguration extends FileConfiguration {
* @throws IllegalArgumentException Thrown is file is null * @throws IllegalArgumentException Thrown is file is null
*/ */
public static YamlConfiguration loadConfiguration(File file) { public static YamlConfiguration loadConfiguration(File file) {
if (file == null) { Validate.notNull(file, "File cannot be null");
throw new IllegalArgumentException("File cannot be null");
}
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();
@ -196,9 +193,7 @@ public class YamlConfiguration extends FileConfiguration {
* @throws IllegalArgumentException Thrown is stream is null * @throws IllegalArgumentException Thrown is stream is null
*/ */
public static YamlConfiguration loadConfiguration(InputStream stream) { public static YamlConfiguration loadConfiguration(InputStream stream) {
if (stream == null) { Validate.notNull(stream, "Stream cannot be null");
throw new IllegalArgumentException("Stream cannot be null");
}
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();

Datei anzeigen

@ -1,5 +1,7 @@
package org.bukkit.configuration.file; package org.bukkit.configuration.file;
import org.apache.commons.lang.Validate;
/** /**
* Various settings for controlling the input and output of a {@link YamlConfiguration} * 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 * @return This object, for chaining
*/ */
public YamlConfigurationOptions indent(int value) { public YamlConfigurationOptions indent(int value) {
if ((indent < 2) || (value > 9)) { Validate.isTrue(value >= 2, "Indent must be at least 2 characters");
throw new IllegalArgumentException("Indent must be between 1 and 10 characters"); Validate.isTrue(value <= 9, "Indent cannot be greater than 9 characters");
}
this.indent = value; this.indent = value;
return this; return this;

Datei anzeigen

@ -9,6 +9,7 @@ import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockVector; import org.bukkit.util.BlockVector;
@ -94,9 +95,7 @@ public class ConfigurationSerialization {
} }
public ConfigurationSerializable deserialize(Map<String, Object> args) { public ConfigurationSerializable deserialize(Map<String, Object> args) {
if (args == null) { Validate.notNull(args, "Args must not be null");
throw new IllegalArgumentException("Args must not be null");
}
ConfigurationSerializable result = null; ConfigurationSerializable result = null;
Method method = null; Method method = null;