Add uncommited config stuffs, just to get a clean working tree.

This commit is contained in:
md_5 2013-03-07 18:17:49 +11:00
parent 87e78bae7e
commit e937dcb4bb
4 changed files with 106 additions and 4 deletions

View File

@ -17,4 +17,18 @@
<name>BungeeCord-Config</name>
<description>Generic java configuration API intended for use with BungeeCord</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
</project>

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
@ -13,8 +14,8 @@ public final class Configuration
private static final char SEPARATOR = '.';
private final Map<String, Object> self;
private final Map<String, Object> comments;
private final Map<String, Object> defaults;
private Map<String, Object> comments = new HashMap<>();
private final Configuration defaults;
private Map<String, Object> getHolder(String path, Map<String, Object> parent, boolean create)
{
@ -41,12 +42,17 @@ public final class Configuration
public <T> T get(String path, T def)
{
Object val = get( path, self );
return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) get( path, defaults );
return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) defaults.get( path );
}
public Object get(String path)
{
return get( path, null );
}
public Object getDefault(String path)
{
return get( path, defaults );
return defaults.get( path );
}
public void set(String path, Object value, String comment)

View File

@ -0,0 +1,29 @@
package net.md_5.bungee.config;
import java.io.File;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
public abstract class ConfigurationProvider
{
private static final Map<Class<? extends ConfigurationProvider>, ConfigurationProvider> providers = new HashMap<>();
static
{
providers.put( YamlConfiguration.class, new YamlConfiguration() );
}
public ConfigurationProvider getProvider(Class<? extends ConfigurationProvider> provider)
{
return providers.get( provider );
}
/*------------------------------------------------------------------------*/
public abstract Configuration load(File file);
public abstract Configuration load(Reader reader);
public abstract Configuration load(String string);
}

View File

@ -0,0 +1,53 @@
package net.md_5.bungee.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
public class YamlConfiguration extends ConfigurationProvider
{
private final ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>()
{
@Override
protected Yaml initialValue()
{
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK );
return new Yaml( options );
}
};
@Override
public Configuration load(File file)
{
try ( FileReader reader = new FileReader( file ) )
{
return load( reader );
} catch ( IOException ex )
{
return null;
}
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(Reader reader)
{
Configuration conf = new Configuration( (Map<String, Object>) yaml.get().loadAs( reader, Map.class ), null );
return conf;
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(String string)
{
Configuration conf = new Configuration( (Map<String, Object>) yaml.get().loadAs( string, Map.class ), null );
return conf;
}
}