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:
parent
4d389df7c8
commit
faf903469e
@ -18,6 +18,14 @@ public final class Configuration
|
|||||||
final Map<String, Object> self;
|
final Map<String, Object> self;
|
||||||
private final Configuration defaults;
|
private final Configuration defaults;
|
||||||
|
|
||||||
|
public Configuration() {
|
||||||
|
this( null );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Configuration(Configuration defaults) {
|
||||||
|
this( new LinkedHashMap<String, Object>(), defaults );
|
||||||
|
}
|
||||||
|
|
||||||
private Configuration getSectionFor(String path)
|
private Configuration getSectionFor(String path)
|
||||||
{
|
{
|
||||||
int index = path.indexOf( SEPARATOR );
|
int index = path.indexOf( SEPARATOR );
|
||||||
@ -66,7 +74,7 @@ public final class Configuration
|
|||||||
|
|
||||||
public Object get(String path)
|
public Object get(String path)
|
||||||
{
|
{
|
||||||
return get( path, null );
|
return get( path, getDefault( path ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getDefault(String path)
|
public Object getDefault(String path)
|
||||||
|
@ -2,6 +2,7 @@ package net.md_5.bungee.config;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -28,8 +29,14 @@ public abstract class ConfigurationProvider
|
|||||||
public abstract void save(Configuration config, Writer writer);
|
public abstract void save(Configuration config, Writer writer);
|
||||||
|
|
||||||
public abstract Configuration load(File file) throws IOException;
|
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);
|
||||||
|
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);
|
||||||
|
public abstract Configuration load(String string, Configuration defaults);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import java.io.File;
|
|||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@ -45,34 +46,70 @@ public class YamlConfiguration extends ConfigurationProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Configuration load(File file) throws IOException
|
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 ) )
|
try ( FileReader reader = new FileReader( file ) )
|
||||||
{
|
{
|
||||||
return load( reader );
|
return load( reader, defaults );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Configuration load(Reader reader)
|
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 );
|
Map<String, Object> map = yaml.get().loadAs( reader, LinkedHashMap.class );
|
||||||
if ( map == null )
|
if ( map == null )
|
||||||
{
|
{
|
||||||
map = new LinkedHashMap<>();
|
map = new LinkedHashMap<>();
|
||||||
}
|
}
|
||||||
return new Configuration( map, null );
|
return new Configuration( map, defaults );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Configuration load(InputStream is)
|
||||||
|
{
|
||||||
|
return load( is, null );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@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)
|
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 );
|
Map<String, Object> map = yaml.get().loadAs( string, LinkedHashMap.class );
|
||||||
if ( map == null )
|
if ( map == null )
|
||||||
{
|
{
|
||||||
map = new LinkedHashMap<>();
|
map = new LinkedHashMap<>();
|
||||||
}
|
}
|
||||||
return new Configuration( map, null );
|
return new Configuration( map, defaults );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user