Add methods to load configurations with defaults.

Add Configuration constructor for empty configurations.

Use defaults for the Configuration object getter.
This commit is contained in:
Minecrell 2014-09-14 18:23:57 +02:00 committed by md_5
parent 4d389df7c8
commit faf903469e
3 changed files with 57 additions and 5 deletions

View File

@ -18,6 +18,14 @@ public final class Configuration
final Map<String, Object> self;
private final Configuration defaults;
public Configuration() {
this( null );
}
public Configuration(Configuration defaults) {
this( new LinkedHashMap<String, Object>(), defaults );
}
private Configuration getSectionFor(String path)
{
int index = path.indexOf( SEPARATOR );
@ -66,7 +74,7 @@ public final class Configuration
public Object get(String path)
{
return get( path, null );
return get( path, getDefault( path ) );
}
public Object getDefault(String path)

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.config;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
@ -28,8 +29,14 @@ public abstract class ConfigurationProvider
public abstract void save(Configuration config, Writer writer);
public abstract Configuration load(File file) throws IOException;
public abstract Configuration load(File file, Configuration defaults) throws IOException;
public abstract Configuration load(Reader reader);
public abstract Configuration load(Reader reader, Configuration defaults);
public abstract Configuration load(InputStream is);
public abstract Configuration load(InputStream is, Configuration defaults);
public abstract Configuration load(String string);
public abstract Configuration load(String string, Configuration defaults);
}

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.LinkedHashMap;
@ -45,34 +46,70 @@ public class YamlConfiguration extends ConfigurationProvider
@Override
public Configuration load(File file) throws IOException
{
return load( file, null );
}
@Override
public Configuration load(File file, Configuration defaults) throws IOException
{
try ( FileReader reader = new FileReader( file ) )
{
return load( reader );
return load( reader, defaults );
}
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(Reader reader)
{
return load( reader, null );
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(Reader reader, Configuration defaults)
{
Map<String, Object> map = yaml.get().loadAs( reader, LinkedHashMap.class );
if ( map == null )
{
map = new LinkedHashMap<>();
}
return new Configuration( map, null );
return new Configuration( map, defaults );
}
@Override
public Configuration load(InputStream is)
{
return load( is, null );
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(InputStream is, Configuration defaults)
{
Map<String, Object> map = yaml.get().loadAs( is, LinkedHashMap.class );
if ( map == null )
{
map = new LinkedHashMap<>();
}
return new Configuration( map, defaults );
}
@Override
public Configuration load(String string)
{
return load( string, null );
}
@Override
@SuppressWarnings("unchecked")
public Configuration load(String string, Configuration defaults)
{
Map<String, Object> map = yaml.get().loadAs( string, LinkedHashMap.class );
if ( map == null )
{
map = new LinkedHashMap<>();
}
return new Configuration( map, null );
return new Configuration( map, defaults );
}
}