13
0
geforkt von Mirrors/Paper

Added helper methods such as getStringList to ConfigurationSection

By: Nathan Adams <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2011-10-26 09:47:51 +01:00
Ursprung 04d50c0f67
Commit ccbf3b22ed
3 geänderte Dateien mit 472 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -390,6 +390,156 @@ public interface ConfigurationSection {
public boolean isList(String path);
/**
* Gets the requested List of String by path.
* <p>
* 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.
* <p>
* 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<String> getStringList(String path);
/**
* Gets the requested List of Integer by path.
* <p>
* 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.
* <p>
* 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<Integer> getIntegerList(String path);
/**
* Gets the requested List of Boolean by path.
* <p>
* 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.
* <p>
* 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<Boolean> getBooleanList(String path);
/**
* Gets the requested List of Double by path.
* <p>
* 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.
* <p>
* 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<Double> getDoubleList(String path);
/**
* Gets the requested List of Float by path.
* <p>
* 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.
* <p>
* 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<Float> getFloatList(String path);
/**
* Gets the requested List of Long by path.
* <p>
* 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.
* <p>
* 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<Long> getLongList(String path);
/**
* Gets the requested List of Byte by path.
* <p>
* 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.
* <p>
* 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<Byte> getByteList(String path);
/**
* Gets the requested List of Character by path.
* <p>
* 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.
* <p>
* 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<Character> getCharacterList(String path);
/**
* Gets the requested List of Short by path.
* <p>
* 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.
* <p>
* 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<Short> getShortList(String path);
/**
* Gets the requested List of Maps by path.
* <p>
* 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.
* <p>
* 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<Map<String, Object>> getMapList(String path);
// Bukkit
/**

Datei anzeigen

@ -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<Object> list = getList(path);
List<String> 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<Object> list = getList(path);
List<Integer> 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<Object> list = getList(path);
List<Boolean> 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<Object> list = getList(path);
List<Double> 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<Object> list = getList(path);
List<Float> 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<Object> list = getList(path);
List<Long> 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<Object> list = getList(path);
List<Byte> 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<Object> list = getList(path);
List<Character> 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<Object> list = getList(path);
List<Short> 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<Map<String, Object>> getMapList(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
List<Object> list = getList(path);
List<Map<String, Object>> result = new ArrayList();
for (Object object : list) {
if (object instanceof Map) {
result.add((Map<String, Object>)object);
}
}
return result;
}
// Bukkit
public Vector getVector(String path) {
if (path == null) {

Datei anzeigen

@ -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<String, Object> map = new HashMap<String, Object>();
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"));
}