mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-29 12:14:42 +02:00
Hows this?
This commit is contained in:
@ -5,8 +5,7 @@ import java.util.Map;
|
||||
/**
|
||||
* Represents a source of configurable options and settings
|
||||
*/
|
||||
public interface Configuration extends ConfigurationSection
|
||||
{
|
||||
public interface Configuration extends ConfigurationSection {
|
||||
/**
|
||||
* Sets the default value of the given path as provided.
|
||||
* <p>
|
||||
@ -23,7 +22,7 @@ public interface Configuration extends ConfigurationSection
|
||||
*/
|
||||
@Override
|
||||
public void addDefault(final String path, final Object value);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the default values of the given paths as provided.
|
||||
* <p>
|
||||
@ -35,7 +34,7 @@ public interface Configuration extends ConfigurationSection
|
||||
* @throws IllegalArgumentException Thrown if defaults is null.
|
||||
*/
|
||||
public void addDefaults(final Map<String, Object> defaults);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the default values of the given paths as provided.
|
||||
* <p>
|
||||
@ -52,7 +51,7 @@ public interface Configuration extends ConfigurationSection
|
||||
* @throws IllegalArgumentException Thrown if defaults is null or this.
|
||||
*/
|
||||
public void addDefaults(final Configuration defaults);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the source of all default values for this {@link Configuration}.
|
||||
* <p>
|
||||
@ -63,7 +62,7 @@ public interface Configuration extends ConfigurationSection
|
||||
* @throws IllegalArgumentException Thrown if defaults is null or this.
|
||||
*/
|
||||
public void setDefaults(final Configuration defaults);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the source {@link Configuration} for this configuration.
|
||||
* <p>
|
||||
@ -74,7 +73,7 @@ public interface Configuration extends ConfigurationSection
|
||||
* @return Configuration source for default values, or null if none exist.
|
||||
*/
|
||||
public Configuration getDefaults();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the {@link ConfigurationOptions} for this {@link Configuration}.
|
||||
* <p>
|
||||
|
@ -4,27 +4,24 @@ package com.intellectualcrafters.configuration;
|
||||
* Various settings for controlling the input and output of a {@link
|
||||
* Configuration}
|
||||
*/
|
||||
public class ConfigurationOptions
|
||||
{
|
||||
public class ConfigurationOptions {
|
||||
private char pathSeparator = '.';
|
||||
private boolean copyDefaults = false;
|
||||
private final Configuration configuration;
|
||||
|
||||
protected ConfigurationOptions(final Configuration configuration)
|
||||
{
|
||||
|
||||
protected ConfigurationOptions(final Configuration configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the {@link Configuration} that this object is responsible for.
|
||||
*
|
||||
* @return Parent configuration
|
||||
*/
|
||||
public Configuration configuration()
|
||||
{
|
||||
public Configuration configuration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the char that will be used to separate {@link
|
||||
* ConfigurationSection}s
|
||||
@ -34,11 +31,10 @@ public class ConfigurationOptions
|
||||
*
|
||||
* @return Path separator
|
||||
*/
|
||||
public char pathSeparator()
|
||||
{
|
||||
public char pathSeparator() {
|
||||
return pathSeparator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the char that will be used to separate {@link
|
||||
* ConfigurationSection}s
|
||||
@ -49,12 +45,11 @@ public class ConfigurationOptions
|
||||
* @param value Path separator
|
||||
* @return This object, for chaining
|
||||
*/
|
||||
public ConfigurationOptions pathSeparator(final char value)
|
||||
{
|
||||
public ConfigurationOptions pathSeparator(final char value) {
|
||||
pathSeparator = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the {@link Configuration} should copy values from its default
|
||||
* {@link Configuration} directly.
|
||||
@ -69,11 +64,10 @@ public class ConfigurationOptions
|
||||
*
|
||||
* @return Whether or not defaults are directly copied
|
||||
*/
|
||||
public boolean copyDefaults()
|
||||
{
|
||||
public boolean copyDefaults() {
|
||||
return copyDefaults;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets if the {@link Configuration} should copy values from its default
|
||||
* {@link Configuration} directly.
|
||||
@ -89,8 +83,7 @@ public class ConfigurationOptions
|
||||
* @param value Whether or not defaults are directly copied
|
||||
* @return This object, for chaining
|
||||
*/
|
||||
public ConfigurationOptions copyDefaults(final boolean value)
|
||||
{
|
||||
public ConfigurationOptions copyDefaults(final boolean value) {
|
||||
copyDefaults = value;
|
||||
return this;
|
||||
}
|
||||
|
@ -7,8 +7,7 @@ import java.util.Set;
|
||||
/**
|
||||
* Represents a section of a {@link Configuration}
|
||||
*/
|
||||
public interface ConfigurationSection
|
||||
{
|
||||
public interface ConfigurationSection {
|
||||
/**
|
||||
* Gets a set containing all keys in this section.
|
||||
* <p>
|
||||
@ -24,7 +23,7 @@ public interface ConfigurationSection
|
||||
* @return Set of keys contained within this ConfigurationSection.
|
||||
*/
|
||||
public Set<String> getKeys(final boolean deep);
|
||||
|
||||
|
||||
/**
|
||||
* Gets a Map containing all keys and their values for this section.
|
||||
* <p>
|
||||
@ -40,7 +39,7 @@ public interface ConfigurationSection
|
||||
* @return Map of keys and values of this section.
|
||||
*/
|
||||
public Map<String, Object> getValues(final boolean deep);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if this {@link ConfigurationSection} contains the given path.
|
||||
* <p>
|
||||
@ -53,7 +52,7 @@ public interface ConfigurationSection
|
||||
* @throws IllegalArgumentException Thrown when path is null.
|
||||
*/
|
||||
public boolean contains(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if this {@link ConfigurationSection} has a value set for the
|
||||
* given path.
|
||||
@ -67,7 +66,7 @@ public interface ConfigurationSection
|
||||
* @throws IllegalArgumentException Thrown when path is null.
|
||||
*/
|
||||
public boolean isSet(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the path of this {@link ConfigurationSection} from its root {@link
|
||||
* Configuration}
|
||||
@ -84,7 +83,7 @@ public interface ConfigurationSection
|
||||
* @return Path of this section relative to its root
|
||||
*/
|
||||
public String getCurrentPath();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the name of this individual {@link ConfigurationSection}, in the
|
||||
* path.
|
||||
@ -95,7 +94,7 @@ public interface ConfigurationSection
|
||||
* @return Name of this section
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the root {@link Configuration} that contains this {@link
|
||||
* ConfigurationSection}
|
||||
@ -109,7 +108,7 @@ public interface ConfigurationSection
|
||||
* @return Root configuration containing this section.
|
||||
*/
|
||||
public Configuration getRoot();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the parent {@link ConfigurationSection} that directly contains
|
||||
* this {@link ConfigurationSection}.
|
||||
@ -122,7 +121,7 @@ public interface ConfigurationSection
|
||||
* @return Parent section containing this section.
|
||||
*/
|
||||
public ConfigurationSection getParent();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested Object by path.
|
||||
* <p>
|
||||
@ -134,7 +133,7 @@ public interface ConfigurationSection
|
||||
* @return Requested Object.
|
||||
*/
|
||||
public Object get(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested Object by path, returning a default value if not
|
||||
* found.
|
||||
@ -148,7 +147,7 @@ public interface ConfigurationSection
|
||||
* @return Requested Object.
|
||||
*/
|
||||
public Object get(final String path, final Object def);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the specified path to the given value.
|
||||
* <p>
|
||||
@ -164,7 +163,7 @@ public interface ConfigurationSection
|
||||
* @param value New value to set the path to.
|
||||
*/
|
||||
public void set(final String path, final Object value);
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty {@link ConfigurationSection} at the specified path.
|
||||
* <p>
|
||||
@ -176,7 +175,7 @@ public interface ConfigurationSection
|
||||
* @return Newly created section
|
||||
*/
|
||||
public ConfigurationSection createSection(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Creates a {@link ConfigurationSection} at the specified path, with
|
||||
* specified values.
|
||||
@ -190,7 +189,7 @@ public interface ConfigurationSection
|
||||
* @return Newly created section
|
||||
*/
|
||||
public ConfigurationSection createSection(final String path, final Map<?, ?> map);
|
||||
|
||||
|
||||
// Primitives
|
||||
/**
|
||||
* Gets the requested String by path.
|
||||
@ -203,7 +202,7 @@ public interface ConfigurationSection
|
||||
* @return Requested String.
|
||||
*/
|
||||
public String getString(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested String by path, returning a default value if not
|
||||
* found.
|
||||
@ -218,7 +217,7 @@ public interface ConfigurationSection
|
||||
* @return Requested String.
|
||||
*/
|
||||
public String getString(final String path, final String def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a String.
|
||||
* <p>
|
||||
@ -231,7 +230,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a String.
|
||||
*/
|
||||
public boolean isString(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested int by path.
|
||||
* <p>
|
||||
@ -243,7 +242,7 @@ public interface ConfigurationSection
|
||||
* @return Requested int.
|
||||
*/
|
||||
public int getInt(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested int by path, returning a default value if not found.
|
||||
* <p>
|
||||
@ -257,7 +256,7 @@ public interface ConfigurationSection
|
||||
* @return Requested int.
|
||||
*/
|
||||
public int getInt(final String path, final int def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is an int.
|
||||
* <p>
|
||||
@ -270,7 +269,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is an int.
|
||||
*/
|
||||
public boolean isInt(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested boolean by path.
|
||||
* <p>
|
||||
@ -282,7 +281,7 @@ public interface ConfigurationSection
|
||||
* @return Requested boolean.
|
||||
*/
|
||||
public boolean getBoolean(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested boolean by path, returning a default value if not
|
||||
* found.
|
||||
@ -297,7 +296,7 @@ public interface ConfigurationSection
|
||||
* @return Requested boolean.
|
||||
*/
|
||||
public boolean getBoolean(final String path, final boolean def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a boolean.
|
||||
* <p>
|
||||
@ -310,7 +309,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a boolean.
|
||||
*/
|
||||
public boolean isBoolean(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested double by path.
|
||||
* <p>
|
||||
@ -322,7 +321,7 @@ public interface ConfigurationSection
|
||||
* @return Requested double.
|
||||
*/
|
||||
public double getDouble(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested double by path, returning a default value if not
|
||||
* found.
|
||||
@ -337,7 +336,7 @@ public interface ConfigurationSection
|
||||
* @return Requested double.
|
||||
*/
|
||||
public double getDouble(final String path, final double def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a double.
|
||||
* <p>
|
||||
@ -350,7 +349,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a double.
|
||||
*/
|
||||
public boolean isDouble(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested long by path.
|
||||
* <p>
|
||||
@ -362,7 +361,7 @@ public interface ConfigurationSection
|
||||
* @return Requested long.
|
||||
*/
|
||||
public long getLong(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested long by path, returning a default value if not
|
||||
* found.
|
||||
@ -377,7 +376,7 @@ public interface ConfigurationSection
|
||||
* @return Requested long.
|
||||
*/
|
||||
public long getLong(final String path, final long def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a long.
|
||||
* <p>
|
||||
@ -390,7 +389,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a long.
|
||||
*/
|
||||
public boolean isLong(final String path);
|
||||
|
||||
|
||||
// Java
|
||||
/**
|
||||
* Gets the requested List by path.
|
||||
@ -403,7 +402,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List.
|
||||
*/
|
||||
public List<?> getList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List by path, returning a default value if not
|
||||
* found.
|
||||
@ -418,7 +417,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List.
|
||||
*/
|
||||
public List<?> getList(final String path, final List<?> def);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a List.
|
||||
* <p>
|
||||
@ -431,7 +430,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a List.
|
||||
*/
|
||||
public boolean isList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of String by path.
|
||||
* <p>
|
||||
@ -446,7 +445,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of String.
|
||||
*/
|
||||
public List<String> getStringList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Integer by path.
|
||||
* <p>
|
||||
@ -461,7 +460,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Integer.
|
||||
*/
|
||||
public List<Integer> getIntegerList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Boolean by path.
|
||||
* <p>
|
||||
@ -476,7 +475,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Boolean.
|
||||
*/
|
||||
public List<Boolean> getBooleanList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Double by path.
|
||||
* <p>
|
||||
@ -491,7 +490,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Double.
|
||||
*/
|
||||
public List<Double> getDoubleList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Float by path.
|
||||
* <p>
|
||||
@ -506,7 +505,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Float.
|
||||
*/
|
||||
public List<Float> getFloatList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Long by path.
|
||||
* <p>
|
||||
@ -521,7 +520,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Long.
|
||||
*/
|
||||
public List<Long> getLongList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Byte by path.
|
||||
* <p>
|
||||
@ -536,7 +535,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Byte.
|
||||
*/
|
||||
public List<Byte> getByteList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Character by path.
|
||||
* <p>
|
||||
@ -551,7 +550,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Character.
|
||||
*/
|
||||
public List<Character> getCharacterList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Short by path.
|
||||
* <p>
|
||||
@ -566,7 +565,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Short.
|
||||
*/
|
||||
public List<Short> getShortList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested List of Maps by path.
|
||||
* <p>
|
||||
@ -581,7 +580,7 @@ public interface ConfigurationSection
|
||||
* @return Requested List of Maps.
|
||||
*/
|
||||
public List<Map<?, ?>> getMapList(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the requested ConfigurationSection by path.
|
||||
* <p>
|
||||
@ -594,7 +593,7 @@ public interface ConfigurationSection
|
||||
* @return Requested ConfigurationSection.
|
||||
*/
|
||||
public ConfigurationSection getConfigurationSection(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specified path is a ConfigurationSection.
|
||||
* <p>
|
||||
@ -608,7 +607,7 @@ public interface ConfigurationSection
|
||||
* @return Whether or not the specified path is a ConfigurationSection.
|
||||
*/
|
||||
public boolean isConfigurationSection(final String path);
|
||||
|
||||
|
||||
/**
|
||||
* Gets the equivalent {@link ConfigurationSection} from the default
|
||||
* {@link Configuration} defined in {@link #getRoot()}.
|
||||
@ -620,7 +619,7 @@ public interface ConfigurationSection
|
||||
* @return Equivalent section in root configuration
|
||||
*/
|
||||
public ConfigurationSection getDefaultSection();
|
||||
|
||||
|
||||
/**
|
||||
* Sets the default value in the root at the given path as provided.
|
||||
* <p>
|
||||
|
@ -4,38 +4,34 @@ package com.intellectualcrafters.configuration;
|
||||
* Exception thrown when attempting to load an invalid {@link Configuration}
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class InvalidConfigurationException extends Exception
|
||||
{
|
||||
|
||||
public class InvalidConfigurationException extends Exception {
|
||||
|
||||
/**
|
||||
* Creates a new instance of InvalidConfigurationException without a
|
||||
* message or cause.
|
||||
*/
|
||||
public InvalidConfigurationException()
|
||||
{}
|
||||
|
||||
public InvalidConfigurationException() {}
|
||||
|
||||
/**
|
||||
* Constructs an instance of InvalidConfigurationException with the
|
||||
* specified message.
|
||||
*
|
||||
* @param msg The details of the exception.
|
||||
*/
|
||||
public InvalidConfigurationException(final String msg)
|
||||
{
|
||||
public InvalidConfigurationException(final String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an instance of InvalidConfigurationException with the
|
||||
* specified cause.
|
||||
*
|
||||
* @param cause The cause of the exception.
|
||||
*/
|
||||
public InvalidConfigurationException(final Throwable cause)
|
||||
{
|
||||
public InvalidConfigurationException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an instance of InvalidConfigurationException with the
|
||||
* specified message and cause.
|
||||
@ -43,8 +39,7 @@ public class InvalidConfigurationException extends Exception
|
||||
* @param cause The cause of the exception.
|
||||
* @param msg The details of the exception.
|
||||
*/
|
||||
public InvalidConfigurationException(final String msg, final Throwable cause)
|
||||
{
|
||||
public InvalidConfigurationException(final String msg, final Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
||||
|
@ -7,17 +7,15 @@ import java.util.Map;
|
||||
* from any source, and stores all values in memory only.
|
||||
* This is useful for temporary Configurations for providing defaults.
|
||||
*/
|
||||
public class MemoryConfiguration extends MemorySection implements Configuration
|
||||
{
|
||||
public class MemoryConfiguration extends MemorySection implements Configuration {
|
||||
protected Configuration defaults;
|
||||
protected MemoryConfigurationOptions options;
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty {@link MemoryConfiguration} with no default values.
|
||||
*/
|
||||
public MemoryConfiguration()
|
||||
{}
|
||||
|
||||
public MemoryConfiguration() {}
|
||||
|
||||
/**
|
||||
* Creates an empty {@link MemoryConfiguration} using the specified {@link
|
||||
* Configuration} as a source for all default values.
|
||||
@ -25,70 +23,67 @@ public class MemoryConfiguration extends MemorySection implements Configuration
|
||||
* @param defaults Default value provider
|
||||
* @throws IllegalArgumentException Thrown if defaults is null
|
||||
*/
|
||||
public MemoryConfiguration(final Configuration defaults)
|
||||
{
|
||||
public MemoryConfiguration(final Configuration defaults) {
|
||||
this.defaults = defaults;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addDefault(final String path, final Object value)
|
||||
{
|
||||
if (path == null) { throw new NullPointerException("Path may not be null"); }
|
||||
if (defaults == null)
|
||||
{
|
||||
public void addDefault(final String path, final Object value) {
|
||||
if (path == null) {
|
||||
throw new NullPointerException("Path may not be null");
|
||||
}
|
||||
if (defaults == null) {
|
||||
defaults = new MemoryConfiguration();
|
||||
}
|
||||
|
||||
|
||||
defaults.set(path, value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addDefaults(final Map<String, Object> defaults)
|
||||
{
|
||||
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
|
||||
|
||||
for (final Map.Entry<String, Object> entry : defaults.entrySet())
|
||||
{
|
||||
public void addDefaults(final Map<String, Object> defaults) {
|
||||
if (defaults == null) {
|
||||
throw new NullPointerException("Defaults may not be null");
|
||||
}
|
||||
|
||||
for (final Map.Entry<String, Object> entry : defaults.entrySet()) {
|
||||
addDefault(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addDefaults(final Configuration defaults)
|
||||
{
|
||||
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
|
||||
|
||||
public void addDefaults(final Configuration defaults) {
|
||||
if (defaults == null) {
|
||||
throw new NullPointerException("Defaults may not be null");
|
||||
}
|
||||
|
||||
addDefaults(defaults.getValues(true));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setDefaults(final Configuration defaults)
|
||||
{
|
||||
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
|
||||
|
||||
public void setDefaults(final Configuration defaults) {
|
||||
if (defaults == null) {
|
||||
throw new NullPointerException("Defaults may not be null");
|
||||
}
|
||||
|
||||
this.defaults = defaults;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Configuration getDefaults()
|
||||
{
|
||||
public Configuration getDefaults() {
|
||||
return defaults;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ConfigurationSection getParent()
|
||||
{
|
||||
public ConfigurationSection getParent() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MemoryConfigurationOptions options()
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
public MemoryConfigurationOptions options() {
|
||||
if (options == null) {
|
||||
options = new MemoryConfigurationOptions(this);
|
||||
}
|
||||
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
@ -4,29 +4,24 @@ package com.intellectualcrafters.configuration;
|
||||
* Various settings for controlling the input and output of a {@link
|
||||
* MemoryConfiguration}
|
||||
*/
|
||||
public class MemoryConfigurationOptions extends ConfigurationOptions
|
||||
{
|
||||
protected MemoryConfigurationOptions(final MemoryConfiguration configuration)
|
||||
{
|
||||
public class MemoryConfigurationOptions extends ConfigurationOptions {
|
||||
protected MemoryConfigurationOptions(final MemoryConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MemoryConfiguration configuration()
|
||||
{
|
||||
public MemoryConfiguration configuration() {
|
||||
return (MemoryConfiguration) super.configuration();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MemoryConfigurationOptions copyDefaults(final boolean value)
|
||||
{
|
||||
public MemoryConfigurationOptions copyDefaults(final boolean value) {
|
||||
super.copyDefaults(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MemoryConfigurationOptions pathSeparator(final char value)
|
||||
{
|
||||
public MemoryConfigurationOptions pathSeparator(final char value) {
|
||||
super.pathSeparator(value);
|
||||
return this;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -24,8 +24,7 @@ import com.intellectualcrafters.configuration.MemoryConfiguration;
|
||||
* This is a base class for all File based implementations of {@link
|
||||
* Configuration}
|
||||
*/
|
||||
public abstract class FileConfiguration extends MemoryConfiguration
|
||||
{
|
||||
public abstract class FileConfiguration extends MemoryConfiguration {
|
||||
/**
|
||||
* This value specified that the system default encoding should be
|
||||
* completely ignored, as it cannot handle the ASCII character set, or it
|
||||
@ -50,8 +49,7 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
*/
|
||||
@Deprecated
|
||||
public static final boolean SYSTEM_UTF;
|
||||
static
|
||||
{
|
||||
static {
|
||||
final byte[] testBytes = Base64Coder.decode("ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX4NCg==");
|
||||
final String testString = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\r\n";
|
||||
final Charset defaultCharset = Charset.defaultCharset();
|
||||
@ -61,26 +59,24 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
SYSTEM_UTF = trueUTF || UTF8_OVERRIDE;
|
||||
UTF_BIG = trueUTF && UTF8_OVERRIDE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty {@link FileConfiguration} with no default values.
|
||||
*/
|
||||
public FileConfiguration()
|
||||
{
|
||||
public FileConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an empty {@link FileConfiguration} using the specified {@link
|
||||
* Configuration} as a source for all default values.
|
||||
*
|
||||
* @param defaults Default value provider
|
||||
*/
|
||||
public FileConfiguration(final Configuration defaults)
|
||||
{
|
||||
public FileConfiguration(final Configuration defaults) {
|
||||
super(defaults);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves this {@link FileConfiguration} to the specified location.
|
||||
* <p>
|
||||
@ -96,25 +92,23 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* any reason.
|
||||
* @throws IllegalArgumentException Thrown when file is null.
|
||||
*/
|
||||
public void save(final File file) throws IOException
|
||||
{
|
||||
if (file == null) { throw new NullPointerException("File cannot be null"); }
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
final String data = saveToString();
|
||||
|
||||
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? StandardCharsets.UTF_8 : Charset.defaultCharset());
|
||||
|
||||
try
|
||||
{
|
||||
writer.write(data);
|
||||
public void save(final File file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File cannot be null");
|
||||
}
|
||||
finally
|
||||
{
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
final String data = saveToString();
|
||||
|
||||
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? StandardCharsets.UTF_8 : Charset.defaultCharset());
|
||||
|
||||
try {
|
||||
writer.write(data);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves this {@link FileConfiguration} to the specified location.
|
||||
* <p>
|
||||
@ -130,20 +124,21 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* any reason.
|
||||
* @throws IllegalArgumentException Thrown when file is null.
|
||||
*/
|
||||
public void save(final String file) throws IOException
|
||||
{
|
||||
if (file == null) { throw new NullPointerException("File cannot be null"); }
|
||||
|
||||
public void save(final String file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File cannot be null");
|
||||
}
|
||||
|
||||
save(new File(file));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves this {@link FileConfiguration} to a string, and returns it.
|
||||
*
|
||||
* @return String containing this configuration.
|
||||
*/
|
||||
public abstract String saveToString();
|
||||
|
||||
|
||||
/**
|
||||
* Loads this {@link FileConfiguration} from the specified location.
|
||||
* <p>
|
||||
@ -166,15 +161,16 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* a valid Configuration.
|
||||
* @throws IllegalArgumentException Thrown when file is null.
|
||||
*/
|
||||
public void load(final File file) throws IOException, InvalidConfigurationException
|
||||
{
|
||||
if (file == null) { throw new NullPointerException("File cannot be null"); }
|
||||
|
||||
public void load(final File file) throws IOException, InvalidConfigurationException {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File cannot be null");
|
||||
}
|
||||
|
||||
final FileInputStream stream = new FileInputStream(file);
|
||||
|
||||
|
||||
load(new InputStreamReader(stream, UTF8_OVERRIDE && !UTF_BIG ? StandardCharsets.UTF_8 : Charset.defaultCharset()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads this {@link FileConfiguration} from the specified stream.
|
||||
* <p>
|
||||
@ -194,13 +190,14 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* @see #load(Reader)
|
||||
*/
|
||||
@Deprecated
|
||||
public void load(final InputStream stream) throws IOException, InvalidConfigurationException
|
||||
{
|
||||
if (stream == null) { throw new NullPointerException("Stream cannot be null"); }
|
||||
|
||||
public void load(final InputStream stream) throws IOException, InvalidConfigurationException {
|
||||
if (stream == null) {
|
||||
throw new NullPointerException("Stream cannot be null");
|
||||
}
|
||||
|
||||
load(new InputStreamReader(stream, UTF8_OVERRIDE ? StandardCharsets.UTF_8 : Charset.defaultCharset()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads this {@link FileConfiguration} from the specified reader.
|
||||
* <p>
|
||||
@ -214,30 +211,25 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* represent a valid Configuration
|
||||
* @throws IllegalArgumentException thrown when reader is null
|
||||
*/
|
||||
public void load(final Reader reader) throws IOException, InvalidConfigurationException
|
||||
{
|
||||
public void load(final Reader reader) throws IOException, InvalidConfigurationException {
|
||||
final BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
|
||||
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
String line;
|
||||
|
||||
while ((line = input.readLine()) != null)
|
||||
{
|
||||
|
||||
while ((line = input.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append('\n');
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
input.close();
|
||||
}
|
||||
|
||||
|
||||
loadFromString(builder.toString());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads this {@link FileConfiguration} from the specified location.
|
||||
* <p>
|
||||
@ -256,13 +248,14 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* a valid Configuration.
|
||||
* @throws IllegalArgumentException Thrown when file is null.
|
||||
*/
|
||||
public void load(final String file) throws IOException, InvalidConfigurationException
|
||||
{
|
||||
if (file == null) { throw new NullPointerException("File cannot be null"); }
|
||||
|
||||
public void load(final String file) throws IOException, InvalidConfigurationException {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File cannot be null");
|
||||
}
|
||||
|
||||
load(new File(file));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads this {@link FileConfiguration} from the specified string, as
|
||||
* opposed to from file.
|
||||
@ -279,7 +272,7 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* @throws IllegalArgumentException Thrown if contents is null.
|
||||
*/
|
||||
public abstract void loadFromString(final String contents) throws InvalidConfigurationException;
|
||||
|
||||
|
||||
/**
|
||||
* Compiles the header for this {@link FileConfiguration} and returns the
|
||||
* result.
|
||||
@ -291,15 +284,13 @@ public abstract class FileConfiguration extends MemoryConfiguration
|
||||
* @return Compiled header
|
||||
*/
|
||||
protected abstract String buildHeader();
|
||||
|
||||
|
||||
@Override
|
||||
public FileConfigurationOptions options()
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
public FileConfigurationOptions options() {
|
||||
if (options == null) {
|
||||
options = new FileConfigurationOptions(this);
|
||||
}
|
||||
|
||||
|
||||
return (FileConfigurationOptions) options;
|
||||
}
|
||||
}
|
||||
|
@ -7,36 +7,31 @@ import com.intellectualcrafters.configuration.MemoryConfigurationOptions;
|
||||
* Various settings for controlling the input and output of a {@link
|
||||
* FileConfiguration}
|
||||
*/
|
||||
public class FileConfigurationOptions extends MemoryConfigurationOptions
|
||||
{
|
||||
public class FileConfigurationOptions extends MemoryConfigurationOptions {
|
||||
private String header = null;
|
||||
private boolean copyHeader = true;
|
||||
|
||||
protected FileConfigurationOptions(final MemoryConfiguration configuration)
|
||||
{
|
||||
|
||||
protected FileConfigurationOptions(final MemoryConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public FileConfiguration configuration()
|
||||
{
|
||||
public FileConfiguration configuration() {
|
||||
return (FileConfiguration) super.configuration();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public FileConfigurationOptions copyDefaults(final boolean value)
|
||||
{
|
||||
public FileConfigurationOptions copyDefaults(final boolean value) {
|
||||
super.copyDefaults(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public FileConfigurationOptions pathSeparator(final char value)
|
||||
{
|
||||
public FileConfigurationOptions pathSeparator(final char value) {
|
||||
super.pathSeparator(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the header that will be applied to the top of the saved output.
|
||||
* <p>
|
||||
@ -51,11 +46,10 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public String header()
|
||||
{
|
||||
public String header() {
|
||||
return header;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the header that will be applied to the top of the saved output.
|
||||
* <p>
|
||||
@ -71,12 +65,11 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions
|
||||
* @param value New header
|
||||
* @return This object, for chaining
|
||||
*/
|
||||
public FileConfigurationOptions header(final String value)
|
||||
{
|
||||
public FileConfigurationOptions header(final String value) {
|
||||
header = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets whether or not the header should be copied from a default source.
|
||||
* <p>
|
||||
@ -95,11 +88,10 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions
|
||||
*
|
||||
* @return Whether or not to copy the header
|
||||
*/
|
||||
public boolean copyHeader()
|
||||
{
|
||||
public boolean copyHeader() {
|
||||
return copyHeader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets whether or not the header should be copied from a default source.
|
||||
* <p>
|
||||
@ -119,10 +111,9 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions
|
||||
* @param value Whether or not to copy the header
|
||||
* @return This object, for chaining
|
||||
*/
|
||||
public FileConfigurationOptions copyHeader(final boolean value)
|
||||
{
|
||||
public FileConfigurationOptions copyHeader(final boolean value) {
|
||||
copyHeader = value;
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -22,170 +22,144 @@ import com.intellectualcrafters.plot.PS;
|
||||
* An implementation of {@link Configuration} which saves all files in Yaml.
|
||||
* Note that this implementation is not synchronized.
|
||||
*/
|
||||
public class YamlConfiguration extends FileConfiguration
|
||||
{
|
||||
public class YamlConfiguration extends FileConfiguration {
|
||||
protected static final String COMMENT_PREFIX = "# ";
|
||||
protected static final String BLANK_CONFIG = "{}\n";
|
||||
private final DumperOptions yamlOptions = new DumperOptions();
|
||||
private final Representer yamlRepresenter = new YamlRepresenter();
|
||||
private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
||||
|
||||
|
||||
@Override
|
||||
public String saveToString()
|
||||
{
|
||||
public String saveToString() {
|
||||
yamlOptions.setIndent(options().indent());
|
||||
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
yamlOptions.setAllowUnicode(SYSTEM_UTF);
|
||||
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
|
||||
|
||||
final String header = buildHeader();
|
||||
String dump = yaml.dump(getValues(false));
|
||||
|
||||
if (dump.equals(BLANK_CONFIG))
|
||||
{
|
||||
|
||||
if (dump.equals(BLANK_CONFIG)) {
|
||||
dump = "";
|
||||
}
|
||||
|
||||
|
||||
return header + dump;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void loadFromString(final String contents) throws InvalidConfigurationException
|
||||
{
|
||||
if (contents == null) { throw new NullPointerException("Contents cannot be null"); }
|
||||
|
||||
public void loadFromString(final String contents) throws InvalidConfigurationException {
|
||||
if (contents == null) {
|
||||
throw new NullPointerException("Contents cannot be null");
|
||||
}
|
||||
|
||||
Map<?, ?> input;
|
||||
try
|
||||
{
|
||||
try {
|
||||
input = (Map<?, ?>) yaml.load(contents);
|
||||
}
|
||||
catch (final YAMLException e)
|
||||
{
|
||||
} catch (final YAMLException e) {
|
||||
throw new InvalidConfigurationException(e);
|
||||
}
|
||||
catch (final ClassCastException e)
|
||||
{
|
||||
} catch (final ClassCastException e) {
|
||||
throw new InvalidConfigurationException("Top level is not a Map.");
|
||||
}
|
||||
|
||||
|
||||
final String header = parseHeader(contents);
|
||||
if (header.length() > 0)
|
||||
{
|
||||
if (header.length() > 0) {
|
||||
options().header(header);
|
||||
}
|
||||
|
||||
if (input != null)
|
||||
{
|
||||
|
||||
if (input != null) {
|
||||
convertMapsToSections(input, this);
|
||||
}
|
||||
}
|
||||
|
||||
protected void convertMapsToSections(final Map<?, ?> input, final ConfigurationSection section)
|
||||
{
|
||||
for (final Map.Entry<?, ?> entry : input.entrySet())
|
||||
{
|
||||
|
||||
protected void convertMapsToSections(final Map<?, ?> input, final ConfigurationSection section) {
|
||||
for (final Map.Entry<?, ?> entry : input.entrySet()) {
|
||||
final String key = entry.getKey().toString();
|
||||
final Object value = entry.getValue();
|
||||
|
||||
if (value instanceof Map)
|
||||
{
|
||||
|
||||
if (value instanceof Map) {
|
||||
convertMapsToSections((Map<?, ?>) value, section.createSection(key));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
section.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String parseHeader(final String input)
|
||||
{
|
||||
|
||||
protected String parseHeader(final String input) {
|
||||
final String[] lines = input.split("\r?\n", -1);
|
||||
final StringBuilder result = new StringBuilder();
|
||||
boolean readingHeader = true;
|
||||
boolean foundHeader = false;
|
||||
|
||||
for (int i = 0; (i < lines.length) && (readingHeader); i++)
|
||||
{
|
||||
|
||||
for (int i = 0; (i < lines.length) && (readingHeader); i++) {
|
||||
final String line = lines[i];
|
||||
|
||||
if (line.startsWith(COMMENT_PREFIX))
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
|
||||
if (line.startsWith(COMMENT_PREFIX)) {
|
||||
if (i > 0) {
|
||||
result.append("\n");
|
||||
}
|
||||
|
||||
if (line.length() > COMMENT_PREFIX.length())
|
||||
{
|
||||
|
||||
if (line.length() > COMMENT_PREFIX.length()) {
|
||||
result.append(line.substring(COMMENT_PREFIX.length()));
|
||||
}
|
||||
|
||||
|
||||
foundHeader = true;
|
||||
}
|
||||
else if ((foundHeader) && (line.length() == 0))
|
||||
{
|
||||
} else if ((foundHeader) && (line.length() == 0)) {
|
||||
result.append("\n");
|
||||
}
|
||||
else if (foundHeader)
|
||||
{
|
||||
} else if (foundHeader) {
|
||||
readingHeader = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String buildHeader()
|
||||
{
|
||||
protected String buildHeader() {
|
||||
final String header = options().header();
|
||||
|
||||
if (options().copyHeader())
|
||||
{
|
||||
|
||||
if (options().copyHeader()) {
|
||||
final Configuration def = getDefaults();
|
||||
|
||||
if ((def != null) && (def instanceof FileConfiguration))
|
||||
{
|
||||
|
||||
if ((def != null) && (def instanceof FileConfiguration)) {
|
||||
final FileConfiguration filedefaults = (FileConfiguration) def;
|
||||
final String defaultsHeader = filedefaults.buildHeader();
|
||||
|
||||
if ((defaultsHeader != null) && (defaultsHeader.length() > 0)) { return defaultsHeader; }
|
||||
|
||||
if ((defaultsHeader != null) && (defaultsHeader.length() > 0)) {
|
||||
return defaultsHeader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (header == null) { return ""; }
|
||||
|
||||
|
||||
if (header == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
final String[] lines = header.split("\r?\n", -1);
|
||||
boolean startedHeader = false;
|
||||
|
||||
for (int i = lines.length - 1; i >= 0; i--)
|
||||
{
|
||||
|
||||
for (int i = lines.length - 1; i >= 0; i--) {
|
||||
builder.insert(0, "\n");
|
||||
|
||||
if ((startedHeader) || (lines[i].length() != 0))
|
||||
{
|
||||
|
||||
if ((startedHeader) || (lines[i].length() != 0)) {
|
||||
builder.insert(0, lines[i]);
|
||||
builder.insert(0, COMMENT_PREFIX);
|
||||
startedHeader = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfigurationOptions options()
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
public YamlConfigurationOptions options() {
|
||||
if (options == null) {
|
||||
options = new YamlConfigurationOptions(this);
|
||||
}
|
||||
|
||||
|
||||
return (YamlConfigurationOptions) options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link YamlConfiguration}, loading from the given file.
|
||||
* <p>
|
||||
@ -199,25 +173,21 @@ public class YamlConfiguration extends FileConfiguration
|
||||
* @return Resulting configuration
|
||||
* @throws IllegalArgumentException Thrown if file is null
|
||||
*/
|
||||
public static YamlConfiguration loadConfiguration(final File file)
|
||||
{
|
||||
if (file == null) { throw new NullPointerException("File cannot be null"); }
|
||||
|
||||
final YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
try
|
||||
{
|
||||
config.load(file);
|
||||
public static YamlConfiguration loadConfiguration(final File file) {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File cannot be null");
|
||||
}
|
||||
catch (final Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
final YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (final Exception ex) {
|
||||
try {
|
||||
file.getAbsolutePath();
|
||||
File dest = new File(file.getAbsolutePath() + "_broken");
|
||||
int i = 0;
|
||||
while (dest.exists())
|
||||
{
|
||||
while (dest.exists()) {
|
||||
dest = new File(file.getAbsolutePath() + "_broken_" + i++);
|
||||
}
|
||||
Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
@ -226,16 +196,14 @@ public class YamlConfiguration extends FileConfiguration
|
||||
PS.debug("&c============ Full stacktrace ============");
|
||||
ex.printStackTrace();
|
||||
PS.debug("&c=========================================");
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link YamlConfiguration}, loading from the given stream.
|
||||
* <p>
|
||||
@ -251,30 +219,26 @@ public class YamlConfiguration extends FileConfiguration
|
||||
* @see #loadConfiguration(Reader)
|
||||
*/
|
||||
@Deprecated
|
||||
public static YamlConfiguration loadConfiguration(final InputStream stream)
|
||||
{
|
||||
if (stream == null) { throw new NullPointerException("Stream cannot be null"); }
|
||||
|
||||
public static YamlConfiguration loadConfiguration(final InputStream stream) {
|
||||
if (stream == null) {
|
||||
throw new NullPointerException("Stream cannot be null");
|
||||
}
|
||||
|
||||
final YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
config.load(stream);
|
||||
}
|
||||
catch (final IOException ex)
|
||||
{
|
||||
} catch (final IOException ex) {
|
||||
PS.debug("Cannot load configuration from stream");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
catch (final InvalidConfigurationException ex)
|
||||
{
|
||||
} catch (final InvalidConfigurationException ex) {
|
||||
ex.printStackTrace();
|
||||
PS.debug("Cannot load configuration from stream");
|
||||
}
|
||||
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link YamlConfiguration}, loading from the given reader.
|
||||
* <p>
|
||||
@ -286,27 +250,23 @@ public class YamlConfiguration extends FileConfiguration
|
||||
* @return resulting configuration
|
||||
* @throws IllegalArgumentException Thrown if stream is null
|
||||
*/
|
||||
public static YamlConfiguration loadConfiguration(final Reader reader)
|
||||
{
|
||||
if (reader == null) { throw new NullPointerException("Reader cannot be null"); }
|
||||
|
||||
public static YamlConfiguration loadConfiguration(final Reader reader) {
|
||||
if (reader == null) {
|
||||
throw new NullPointerException("Reader cannot be null");
|
||||
}
|
||||
|
||||
final YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
config.load(reader);
|
||||
}
|
||||
catch (final IOException ex)
|
||||
{
|
||||
} catch (final IOException ex) {
|
||||
PS.debug("Cannot load configuration from stream");
|
||||
ex.printStackTrace();
|
||||
} catch (final InvalidConfigurationException ex) {
|
||||
PS.debug("Cannot load configuration from stream");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
catch (final InvalidConfigurationException ex)
|
||||
{
|
||||
PS.debug("Cannot load configuration from stream");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
@ -4,49 +4,42 @@ package com.intellectualcrafters.configuration.file;
|
||||
* Various settings for controlling the input and output of a {@link
|
||||
* YamlConfiguration}
|
||||
*/
|
||||
public class YamlConfigurationOptions extends FileConfigurationOptions
|
||||
{
|
||||
public class YamlConfigurationOptions extends FileConfigurationOptions {
|
||||
private int indent = 2;
|
||||
|
||||
protected YamlConfigurationOptions(final YamlConfiguration configuration)
|
||||
{
|
||||
|
||||
protected YamlConfigurationOptions(final YamlConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfiguration configuration()
|
||||
{
|
||||
public YamlConfiguration configuration() {
|
||||
return (YamlConfiguration) super.configuration();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfigurationOptions copyDefaults(final boolean value)
|
||||
{
|
||||
public YamlConfigurationOptions copyDefaults(final boolean value) {
|
||||
super.copyDefaults(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfigurationOptions pathSeparator(final char value)
|
||||
{
|
||||
public YamlConfigurationOptions pathSeparator(final char value) {
|
||||
super.pathSeparator(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfigurationOptions header(final String value)
|
||||
{
|
||||
public YamlConfigurationOptions header(final String value) {
|
||||
super.header(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public YamlConfigurationOptions copyHeader(final boolean value)
|
||||
{
|
||||
public YamlConfigurationOptions copyHeader(final boolean value) {
|
||||
super.copyHeader(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets how much spaces should be used to indent each line.
|
||||
* <p>
|
||||
@ -54,11 +47,10 @@ public class YamlConfigurationOptions extends FileConfigurationOptions
|
||||
*
|
||||
* @return How much to indent by
|
||||
*/
|
||||
public int indent()
|
||||
{
|
||||
public int indent() {
|
||||
return indent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets how much spaces should be used to indent each line.
|
||||
* <p>
|
||||
@ -67,11 +59,14 @@ public class YamlConfigurationOptions extends FileConfigurationOptions
|
||||
* @param value New indent
|
||||
* @return This object, for chaining
|
||||
*/
|
||||
public YamlConfigurationOptions indent(final int value)
|
||||
{
|
||||
if (value < 2) { throw new IllegalArgumentException("Indent must be at least 2 characters"); }
|
||||
if (value > 9) { throw new IllegalArgumentException("Indent cannot be greater than 9 characters"); }
|
||||
|
||||
public YamlConfigurationOptions indent(final int value) {
|
||||
if (value < 2) {
|
||||
throw new IllegalArgumentException("Indent must be at least 2 characters");
|
||||
}
|
||||
if (value > 9) {
|
||||
throw new IllegalArgumentException("Indent cannot be greater than 9 characters");
|
||||
}
|
||||
|
||||
indent = value;
|
||||
return this;
|
||||
}
|
||||
|
@ -10,47 +10,39 @@ import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization;
|
||||
|
||||
public class YamlConstructor extends SafeConstructor
|
||||
{
|
||||
|
||||
public YamlConstructor()
|
||||
{
|
||||
public class YamlConstructor extends SafeConstructor {
|
||||
|
||||
public YamlConstructor() {
|
||||
yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
|
||||
}
|
||||
|
||||
private class ConstructCustomObject extends ConstructYamlMap
|
||||
{
|
||||
|
||||
private class ConstructCustomObject extends ConstructYamlMap {
|
||||
@Override
|
||||
public Object construct(final Node node)
|
||||
{
|
||||
if (node.isTwoStepsConstruction()) { throw new YAMLException("Unexpected referential mapping structure. Node: " + node); }
|
||||
|
||||
public Object construct(final Node node) {
|
||||
if (node.isTwoStepsConstruction()) {
|
||||
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
|
||||
}
|
||||
|
||||
final Map<?, ?> raw = (Map<?, ?>) super.construct(node);
|
||||
|
||||
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY))
|
||||
{
|
||||
|
||||
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
final Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
|
||||
for (final Map.Entry<?, ?> entry : raw.entrySet())
|
||||
{
|
||||
for (final Map.Entry<?, ?> entry : raw.entrySet()) {
|
||||
typed.put(entry.getKey().toString(), entry.getValue());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
return ConfigurationSerialization.deserializeObject(typed);
|
||||
}
|
||||
catch (final IllegalArgumentException ex)
|
||||
{
|
||||
} catch (final IllegalArgumentException ex) {
|
||||
throw new YAMLException("Could not deserialize object", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void construct2ndStep(final Node node, final Object object)
|
||||
{
|
||||
public void construct2ndStep(final Node node, final Object object) {
|
||||
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
|
||||
}
|
||||
}
|
||||
|
@ -10,34 +10,28 @@ import com.intellectualcrafters.configuration.ConfigurationSection;
|
||||
import com.intellectualcrafters.configuration.serialization.ConfigurationSerializable;
|
||||
import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization;
|
||||
|
||||
public class YamlRepresenter extends Representer
|
||||
{
|
||||
|
||||
public YamlRepresenter()
|
||||
{
|
||||
public class YamlRepresenter extends Representer {
|
||||
|
||||
public YamlRepresenter() {
|
||||
multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection());
|
||||
multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable());
|
||||
}
|
||||
|
||||
private class RepresentConfigurationSection extends RepresentMap
|
||||
{
|
||||
|
||||
private class RepresentConfigurationSection extends RepresentMap {
|
||||
@Override
|
||||
public Node representData(final Object data)
|
||||
{
|
||||
public Node representData(final Object data) {
|
||||
return super.representData(((ConfigurationSection) data).getValues(false));
|
||||
}
|
||||
}
|
||||
|
||||
private class RepresentConfigurationSerializable extends RepresentMap
|
||||
{
|
||||
|
||||
private class RepresentConfigurationSerializable extends RepresentMap {
|
||||
@Override
|
||||
public Node representData(final Object data)
|
||||
{
|
||||
public Node representData(final Object data) {
|
||||
final ConfigurationSerializable serializable = (ConfigurationSerializable) data;
|
||||
final Map<String, Object> values = new LinkedHashMap<String, Object>();
|
||||
values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
|
||||
values.putAll(serializable.serialize());
|
||||
|
||||
|
||||
return super.representData(values);
|
||||
}
|
||||
}
|
||||
|
@ -21,9 +21,8 @@ import java.util.Map;
|
||||
* @see DelegateDeserialization
|
||||
* @see SerializableAs
|
||||
*/
|
||||
public interface ConfigurationSerializable
|
||||
{
|
||||
|
||||
public interface ConfigurationSerializable {
|
||||
|
||||
/**
|
||||
* Creates a Map representation of this class.
|
||||
* <p>
|
||||
|
@ -14,137 +14,107 @@ import com.intellectualcrafters.configuration.Configuration;
|
||||
/**
|
||||
* Utility class for storing and retrieving classes for {@link Configuration}.
|
||||
*/
|
||||
public class ConfigurationSerialization
|
||||
{
|
||||
public class ConfigurationSerialization {
|
||||
public static final String SERIALIZED_TYPE_KEY = "==";
|
||||
private final Class<? extends ConfigurationSerializable> clazz;
|
||||
private static Map<String, Class<? extends ConfigurationSerializable>> aliases = new HashMap<String, Class<? extends ConfigurationSerializable>>();
|
||||
|
||||
protected ConfigurationSerialization(final Class<? extends ConfigurationSerializable> clazz)
|
||||
{
|
||||
|
||||
protected ConfigurationSerialization(final Class<? extends ConfigurationSerializable> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
protected Method getMethod(final String name, final boolean isStatic)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
protected Method getMethod(final String name, final boolean isStatic) {
|
||||
try {
|
||||
final Method method = clazz.getDeclaredMethod(name, Map.class);
|
||||
|
||||
if (!ConfigurationSerializable.class.isAssignableFrom(method.getReturnType())) { return null; }
|
||||
if (Modifier.isStatic(method.getModifiers()) != isStatic) { return null; }
|
||||
|
||||
|
||||
if (!ConfigurationSerializable.class.isAssignableFrom(method.getReturnType())) {
|
||||
return null;
|
||||
}
|
||||
if (Modifier.isStatic(method.getModifiers()) != isStatic) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return method;
|
||||
}
|
||||
catch (final NoSuchMethodException ex)
|
||||
{
|
||||
} catch (final NoSuchMethodException ex) {
|
||||
return null;
|
||||
}
|
||||
catch (final SecurityException ex)
|
||||
{
|
||||
} catch (final SecurityException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Constructor<? extends ConfigurationSerializable> getConstructor()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
protected Constructor<? extends ConfigurationSerializable> getConstructor() {
|
||||
try {
|
||||
return clazz.getConstructor(Map.class);
|
||||
}
|
||||
catch (final NoSuchMethodException ex)
|
||||
{
|
||||
} catch (final NoSuchMethodException ex) {
|
||||
return null;
|
||||
}
|
||||
catch (final SecurityException ex)
|
||||
{
|
||||
} catch (final SecurityException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected ConfigurationSerializable deserializeViaMethod(final Method method, final Map<String, ?> args)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
protected ConfigurationSerializable deserializeViaMethod(final Method method, final Map<String, ?> args) {
|
||||
try {
|
||||
final ConfigurationSerializable result = (ConfigurationSerializable) method.invoke(null, args);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
|
||||
if (result == null) {
|
||||
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE,
|
||||
"Could not call method '" + method.toString() + "' of " + clazz + " for deserialization: method returned null");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch (final Throwable ex)
|
||||
{
|
||||
Logger.getLogger(ConfigurationSerialization.class.getName()).log(
|
||||
Level.SEVERE,
|
||||
"Could not call method '" + method.toString() + "' of " + clazz + " for deserialization",
|
||||
} catch (final Throwable ex) {
|
||||
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE, "Could not call method '" + method.toString() + "' of " + clazz + " for deserialization",
|
||||
ex instanceof InvocationTargetException ? ex.getCause() : ex);
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected ConfigurationSerializable deserializeViaCtor(final Constructor<? extends ConfigurationSerializable> ctor, final Map<String, ?> args)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
protected ConfigurationSerializable deserializeViaCtor(final Constructor<? extends ConfigurationSerializable> ctor, final Map<String, ?> args) {
|
||||
try {
|
||||
return ctor.newInstance(args);
|
||||
}
|
||||
catch (final Throwable ex)
|
||||
{
|
||||
Logger.getLogger(ConfigurationSerialization.class.getName()).log(
|
||||
Level.SEVERE,
|
||||
"Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization",
|
||||
} catch (final Throwable ex) {
|
||||
Logger.getLogger(ConfigurationSerialization.class.getName()).log(Level.SEVERE, "Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization",
|
||||
ex instanceof InvocationTargetException ? ex.getCause() : ex);
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ConfigurationSerializable deserialize(final Map<String, ?> args)
|
||||
{
|
||||
if (args == null) { throw new NullPointerException("Args must not be null"); }
|
||||
|
||||
public ConfigurationSerializable deserialize(final Map<String, ?> args) {
|
||||
if (args == null) {
|
||||
throw new NullPointerException("Args must not be null");
|
||||
}
|
||||
ConfigurationSerializable result = null;
|
||||
Method method = null;
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
|
||||
if (result == null) {
|
||||
method = getMethod("deserialize", true);
|
||||
|
||||
if (method != null)
|
||||
{
|
||||
|
||||
if (method != null) {
|
||||
result = deserializeViaMethod(method, args);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
|
||||
if (result == null) {
|
||||
method = getMethod("valueOf", true);
|
||||
|
||||
if (method != null)
|
||||
{
|
||||
|
||||
if (method != null) {
|
||||
result = deserializeViaMethod(method, args);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
|
||||
if (result == null) {
|
||||
final Constructor<? extends ConfigurationSerializable> constructor = getConstructor();
|
||||
|
||||
if (constructor != null)
|
||||
{
|
||||
|
||||
if (constructor != null) {
|
||||
result = deserializeViaCtor(constructor, args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to deserialize the given arguments into a new instance of the
|
||||
* given class.
|
||||
@ -160,11 +130,10 @@ public class ConfigurationSerialization
|
||||
* @param clazz Class to deserialize into
|
||||
* @return New instance of the specified class
|
||||
*/
|
||||
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args, final Class<? extends ConfigurationSerializable> clazz)
|
||||
{
|
||||
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args, final Class<? extends ConfigurationSerializable> clazz) {
|
||||
return new ConfigurationSerialization(clazz).deserialize(args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to deserialize the given arguments into a new instance of the
|
||||
* given class.
|
||||
@ -179,51 +148,46 @@ public class ConfigurationSerialization
|
||||
* @param args Arguments for deserialization
|
||||
* @return New instance of the specified class
|
||||
*/
|
||||
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args)
|
||||
{
|
||||
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args) {
|
||||
Class<? extends ConfigurationSerializable> clazz = null;
|
||||
|
||||
if (args.containsKey(SERIALIZED_TYPE_KEY))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (args.containsKey(SERIALIZED_TYPE_KEY)) {
|
||||
try {
|
||||
final String alias = (String) args.get(SERIALIZED_TYPE_KEY);
|
||||
|
||||
if (alias == null) { throw new IllegalArgumentException("Cannot have null alias"); }
|
||||
|
||||
if (alias == null) {
|
||||
throw new IllegalArgumentException("Cannot have null alias");
|
||||
}
|
||||
clazz = getClassByAlias(alias);
|
||||
if (clazz == null) { throw new IllegalArgumentException("Specified class does not exist ('" + alias + "')"); }
|
||||
}
|
||||
catch (final ClassCastException ex)
|
||||
{
|
||||
if (clazz == null) {
|
||||
throw new IllegalArgumentException("Specified class does not exist ('" + alias + "')");
|
||||
}
|
||||
} catch (final ClassCastException ex) {
|
||||
ex.fillInStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
throw new IllegalArgumentException("Args doesn't contain type key ('" + SERIALIZED_TYPE_KEY + "')");
|
||||
}
|
||||
|
||||
|
||||
return new ConfigurationSerialization(clazz).deserialize(args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers the given {@link ConfigurationSerializable} class by its
|
||||
* alias
|
||||
*
|
||||
* @param clazz Class to register
|
||||
*/
|
||||
public static void registerClass(final Class<? extends ConfigurationSerializable> clazz)
|
||||
{
|
||||
public static void registerClass(final Class<? extends ConfigurationSerializable> clazz) {
|
||||
final DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);
|
||||
|
||||
if (delegate == null)
|
||||
{
|
||||
|
||||
if (delegate == null) {
|
||||
registerClass(clazz, getAlias(clazz));
|
||||
registerClass(clazz, clazz.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers the given alias to the specified {@link
|
||||
* ConfigurationSerializable} class
|
||||
@ -232,33 +196,29 @@ public class ConfigurationSerialization
|
||||
* @param alias Alias to register as
|
||||
* @see SerializableAs
|
||||
*/
|
||||
public static void registerClass(final Class<? extends ConfigurationSerializable> clazz, final String alias)
|
||||
{
|
||||
public static void registerClass(final Class<? extends ConfigurationSerializable> clazz, final String alias) {
|
||||
aliases.put(alias, clazz);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unregisters the specified alias to a {@link ConfigurationSerializable}
|
||||
*
|
||||
* @param alias Alias to unregister
|
||||
*/
|
||||
public static void unregisterClass(final String alias)
|
||||
{
|
||||
public static void unregisterClass(final String alias) {
|
||||
aliases.remove(alias);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unregisters any aliases for the specified {@link
|
||||
* ConfigurationSerializable} class
|
||||
*
|
||||
* @param clazz Class to unregister
|
||||
*/
|
||||
public static void unregisterClass(final Class<? extends ConfigurationSerializable> clazz)
|
||||
{
|
||||
while (aliases.values().remove(clazz))
|
||||
{}
|
||||
public static void unregisterClass(final Class<? extends ConfigurationSerializable> clazz) {
|
||||
while (aliases.values().remove(clazz)) {}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to get a registered {@link ConfigurationSerializable} class by
|
||||
* its alias
|
||||
@ -266,11 +226,10 @@ public class ConfigurationSerialization
|
||||
* @param alias Alias of the serializable
|
||||
* @return Registered class, or null if not found
|
||||
*/
|
||||
public static Class<? extends ConfigurationSerializable> getClassByAlias(final String alias)
|
||||
{
|
||||
public static Class<? extends ConfigurationSerializable> getClassByAlias(final String alias) {
|
||||
return aliases.get(alias);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the correct alias for the given {@link ConfigurationSerializable}
|
||||
* class
|
||||
@ -278,29 +237,25 @@ public class ConfigurationSerialization
|
||||
* @param clazz Class to get alias for
|
||||
* @return Alias to use for the class
|
||||
*/
|
||||
public static String getAlias(final Class<? extends ConfigurationSerializable> clazz)
|
||||
{
|
||||
public static String getAlias(final Class<? extends ConfigurationSerializable> clazz) {
|
||||
DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);
|
||||
|
||||
if (delegate != null)
|
||||
{
|
||||
if ((delegate.value() == null) || (delegate.value() == clazz))
|
||||
{
|
||||
|
||||
if (delegate != null) {
|
||||
if ((delegate.value() == null) || (delegate.value() == clazz)) {
|
||||
delegate = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return getAlias(delegate.value());
|
||||
}
|
||||
}
|
||||
|
||||
if (delegate == null)
|
||||
{
|
||||
|
||||
if (delegate == null) {
|
||||
final SerializableAs alias = clazz.getAnnotation(SerializableAs.class);
|
||||
|
||||
if ((alias != null) && (alias.value() != null)) { return alias.value(); }
|
||||
|
||||
if ((alias != null) && (alias.value() != null)) {
|
||||
return alias.value();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return clazz.getName();
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,7 @@ import java.lang.annotation.Target;
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface DelegateDeserialization
|
||||
{
|
||||
public @interface DelegateDeserialization {
|
||||
/**
|
||||
* Which class should be used as a delegate for this classes
|
||||
* deserialization
|
||||
|
@ -21,8 +21,7 @@ import java.lang.annotation.Target;
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface SerializableAs
|
||||
{
|
||||
public @interface SerializableAs {
|
||||
/**
|
||||
* This is the name your class will be stored and retrieved as.
|
||||
* <p>
|
||||
|
Reference in New Issue
Block a user