From 73ca237810cc34caf5009e4ad2f11867009d2a9d Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 2 Mar 2013 20:01:33 +1100 Subject: [PATCH] Add sneak peek of upcoming config API. This will need to be throughly unit tested. --- config/pom.xml | 20 ++ .../net/md_5/bungee/config/Configuration.java | 329 ++++++++++++++++++ 2 files changed, 349 insertions(+) create mode 100644 config/pom.xml create mode 100644 config/src/main/java/net/md_5/bungee/config/Configuration.java diff --git a/config/pom.xml b/config/pom.xml new file mode 100644 index 00000000..9df70007 --- /dev/null +++ b/config/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + + net.md-5 + bungeecord-parent + 1.4.7-SNAPSHOT + ../pom.xml + + + net.md-5 + bungeecord-config + 1.4.7-SNAPSHOT + jar + + BungeeCord-Config + Generic java configuration API intended for use with BungeeCord + diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java new file mode 100644 index 00000000..f9d32c82 --- /dev/null +++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java @@ -0,0 +1,329 @@ +package net.md_5.bungee.config; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor(access = AccessLevel.PACKAGE) +public final class Configuration +{ + + private static final char SEPARATOR = '.'; + private final Map self; + private final Map comments; + private final Map defaults; + + private Map getHolder(String path, Map parent, boolean create) + { + return null; + } + + private Object get(String path, Map holder) + { + int index = path.indexOf( SEPARATOR ); + String first, second; + if ( index == -1 ) + { + second = path; + } else + { + first = path.substring( 0, index ); + second = path.substring( index + 1, path.length() ); + } + return null; + } + + /*------------------------------------------------------------------------*/ + @SuppressWarnings("unchecked") + public T get(String path, T def) + { + Object val = get( path, self ); + return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) get( path, defaults ); + } + + public Object getDefault(String path) + { + return get( path, defaults ); + } + + public void set(String path, Object value, String comment) + { + String child = path.substring( path.indexOf( SEPARATOR ) + 1 ); + getHolder( path, self, true ).put( child, value ); + getHolder( path, comments, true ).put( child, value ); + } + + public void set(String path, Object value) + { + set( path, value, null ); + } + + /*------------------------------------------------------------------------*/ + public byte getByte(String path) + { + Object def = getDefault( path ); + return getByte( path, ( def instanceof Number ) ? ( (Number) def ).byteValue() : 0 ); + } + + public byte getByte(String path, byte def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).byteValue() : def; + } + + public List getByteList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).byteValue() ); + } + } + + return result; + } + + public short getShort(String path) + { + Object def = getDefault( path ); + return getShort( path, ( def instanceof Number ) ? ( (Number) def ).shortValue() : 0 ); + } + + public short getShort(String path, short def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).shortValue() : def; + } + + public List getShortList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).shortValue() ); + } + } + + return result; + } + + public int getInt(String path) + { + Object def = getDefault( path ); + return getInt( path, ( def instanceof Number ) ? ( (Number) def ).intValue() : 0 ); + } + + public int getInt(String path, int def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).intValue() : def; + } + + public List getIntList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).intValue() ); + } + } + + return result; + } + + public long getLong(String path) + { + Object def = getDefault( path ); + return getLong( path, ( def instanceof Number ) ? ( (Number) def ).longValue() : 0 ); + } + + public long getLong(String path, long def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).longValue() : def; + } + + public List getLongList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).longValue() ); + } + } + + return result; + } + + public float getFloat(String path) + { + Object def = getDefault( path ); + return getFloat( path, ( def instanceof Number ) ? ( (Number) def ).floatValue() : 0 ); + } + + public float getFloat(String path, float def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).floatValue() : def; + } + + public List getFloatList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).floatValue() ); + } + } + + return result; + } + + public double getDouble(String path) + { + Object def = getDefault( path ); + return getDouble( path, ( def instanceof Number ) ? ( (Number) def ).doubleValue() : 0 ); + } + + public double getDouble(String path, double def) + { + Object val = get( path, def ); + return ( val instanceof Number ) ? ( (Number) val ).doubleValue() : def; + } + + public List getDoubleList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Number ) + { + result.add( ( (Number) object ).doubleValue() ); + } + } + + return result; + } + + public boolean getBoolean(String path) + { + Object def = getDefault( path ); + return getBoolean( path, ( def instanceof Boolean ) ? (Boolean) def : false ); + } + + public boolean getBoolean(String path, boolean def) + { + Object val = get( path, def ); + return ( val instanceof Boolean ) ? (Boolean) val : def; + } + + public List getBooleanList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Boolean ) + { + result.add( (Boolean) object ); + } + } + + return result; + } + + public char getChar(String path) + { + Object def = getDefault( path ); + return getChar( path, ( def instanceof Character ) ? (Character) def : '\u0000' ); + } + + public char getChar(String path, char def) + { + Object val = get( path, def ); + return ( val instanceof Character ) ? (Character) val : def; + } + + public List getCharList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof Character ) + { + result.add( (Character) object ); + } + } + + return result; + } + + public String getString(String path) + { + Object def = getDefault( path ); + return getString( path, ( def instanceof String ) ? (String) def : "" ); + } + + public String getString(String path, String def) + { + Object val = get( path, def ); + return ( val instanceof String ) ? (String) val : def; + } + + public List getStringList(String path) + { + List list = getList( path ); + List result = new ArrayList<>(); + + for ( Object object : list ) + { + if ( object instanceof String ) + { + result.add( (String) object ); + } + } + + return result; + } + + /*------------------------------------------------------------------------*/ + public List getList(String path) + { + Object def = getDefault( path ); + return getList( path, ( def instanceof List ) ? (List) def : Collections.EMPTY_LIST ); + } + + public List getList(String path, List def) + { + Object val = get( path, def ); + return ( val instanceof List ) ? (List) val : def; + } +}