From ccbf3b22ed62b50ef259425ff54eec023b199aa3 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Wed, 26 Oct 2011 09:47:51 +0100 Subject: [PATCH] Added helper methods such as getStringList to ConfigurationSection By: Nathan Adams --- .../configuration/ConfigurationSection.java | 150 +++++++++ .../bukkit/configuration/MemorySection.java | 305 ++++++++++++++++++ .../ConfigurationSectionTest.java | 18 +- 3 files changed, 472 insertions(+), 1 deletion(-) diff --git a/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java b/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java index 739a33598d..0843c04ec4 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java +++ b/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java @@ -390,6 +390,156 @@ public interface ConfigurationSection { public boolean isList(String path); + /** + * Gets the requested List of String by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a String if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of String. + */ + public List getStringList(String path); + + /** + * Gets the requested List of Integer by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Integer if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Integer. + */ + public List getIntegerList(String path); + + /** + * Gets the requested List of Boolean by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Boolean if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Boolean. + */ + public List getBooleanList(String path); + + /** + * Gets the requested List of Double by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Double if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Double. + */ + public List getDoubleList(String path); + + /** + * Gets the requested List of Float by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Float if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Float. + */ + public List getFloatList(String path); + + /** + * Gets the requested List of Long by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Long if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Long. + */ + public List getLongList(String path); + + /** + * Gets the requested List of Byte by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Byte if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Byte. + */ + public List getByteList(String path); + + /** + * Gets the requested List of Character by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Character if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Character. + */ + public List getCharacterList(String path); + + /** + * Gets the requested List of Short by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Short if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Short. + */ + public List getShortList(String path); + + /** + * Gets the requested List of Maps by path. + *

+ * If the List does not exist but a default value has been specified, this + * will return the default value. If the List does not exist and no default + * value was specified, this will return null. + *

+ * This method will attempt to cast any values into a Map if possible, but may + * miss any values out if they are not compatible. + * + * @param path Path of the List to get. + * @return Requested List of Maps. + */ + public List> getMapList(String path); + // Bukkit /** diff --git a/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java b/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java index fa330e64f9..46965c2312 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java +++ b/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java @@ -2,6 +2,7 @@ package org.bukkit.configuration; import org.bukkit.configuration.serialization.ConfigurationSerializable; import java.io.File; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -430,6 +431,310 @@ public class MemorySection implements ConfigurationSection { return val instanceof List; } + public List getStringList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if ((object instanceof String) || (isPrimitiveWrapper(object))) { + result.add(String.valueOf(object)); + } + } + + return result; + } + + public List getIntegerList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Integer) { + result.add((Integer)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List getBooleanList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Boolean) { + result.add((Boolean)object); + } else if (object instanceof String) { + if (Boolean.TRUE.toString().equals(object)) { + result.add(true); + } else if (Boolean.FALSE.toString().equals(object)) { + result.add(false); + } + } + } + + return result; + } + + public List getDoubleList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Double) { + result.add((Double)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List getFloatList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Float) { + result.add((Float)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List getLongList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Long) { + result.add((Long)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List getByteList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Byte) { + result.add((Byte)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List getCharacterList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Character) { + result.add((Character)object); + } else if (object instanceof String) { + String str = (String)object; + + 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); + } + } + + return result; + } + + public List getShortList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Short) { + result.add((Short)object); + } else if (object instanceof String) { + try { + 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); + } + } + + return result; + } + + public List> getMapList(String path) { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + + List list = getList(path); + List> result = new ArrayList(); + + for (Object object : list) { + if (object instanceof Map) { + result.add((Map)object); + } + } + + return result; + } + // Bukkit public Vector getVector(String path) { if (path == null) { diff --git a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java index fea435a879..6834bfda8d 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java @@ -2,6 +2,7 @@ package org.bukkit.configuration; import org.bukkit.Material; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.bukkit.inventory.ItemStack; @@ -375,11 +376,26 @@ public abstract class ConfigurationSectionTest { public void testGetList_String() { ConfigurationSection section = getConfigurationSection(); String key = "exists"; - List value = Arrays.asList("One", "Two", "Three"); + Map map = new HashMap(); + + map.put("one", 1); + map.put("two", "two"); + map.put("three", 3.14); + + List value = Arrays.asList((Object)"One", "Two", "Three", 4, "5", 6.0, true, "false", map); section.set(key, value); assertEquals(value, section.getList(key)); + assertEquals(Arrays.asList((Object)"One", "Two", "Three", "4", "5", "6.0", "true", "false"), section.getStringList(key)); + assertEquals(Arrays.asList((Object)4, 5, 6), section.getIntegerList(key)); + assertEquals(Arrays.asList((Object)true, false), section.getBooleanList(key)); + assertEquals(Arrays.asList((Object)4.0, 5.0, 6.0), section.getDoubleList(key)); + assertEquals(Arrays.asList((Object)4.0f, 5.0f, 6.0f), section.getFloatList(key)); + assertEquals(Arrays.asList((Object)4l, 5l, 6l), section.getLongList(key)); + assertEquals(Arrays.asList((Object)(byte)4, (byte)5, (byte)6), section.getByteList(key)); + assertEquals(Arrays.asList((Object)(short)4, (short)5, (short)6), section.getShortList(key)); + assertEquals(map, section.getMapList(key).get(0)); assertNull(section.getString("doesntExist")); }