This commit is contained in:
boy0001
2015-09-11 20:09:22 +10:00
parent 37a8861fa0
commit c386f33df8
380 changed files with 43490 additions and 33913 deletions

View File

@ -5,7 +5,8 @@ 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>
@ -20,7 +21,8 @@ public interface Configuration extends ConfigurationSection {
* @param value Value to set the default to.
* @throws IllegalArgumentException Thrown if path is null.
*/
public void addDefault(String path, Object value);
@Override
public void addDefault(final String path, final Object value);
/**
* Sets the default values of the given paths as provided.
@ -32,7 +34,7 @@ public interface Configuration extends ConfigurationSection {
* @param defaults A map of Path->Values to add to defaults.
* @throws IllegalArgumentException Thrown if defaults is null.
*/
public void addDefaults(Map<String, Object> defaults);
public void addDefaults(final Map<String, Object> defaults);
/**
* Sets the default values of the given paths as provided.
@ -49,7 +51,7 @@ public interface Configuration extends ConfigurationSection {
* @param defaults A configuration holding a list of defaults to copy.
* @throws IllegalArgumentException Thrown if defaults is null or this.
*/
public void addDefaults(Configuration defaults);
public void addDefaults(final Configuration defaults);
/**
* Sets the source of all default values for this {@link Configuration}.
@ -60,7 +62,7 @@ public interface Configuration extends ConfigurationSection {
* @param defaults New source of default values for this configuration.
* @throws IllegalArgumentException Thrown if defaults is null or this.
*/
public void setDefaults(Configuration defaults);
public void setDefaults(final Configuration defaults);
/**
* Gets the source {@link Configuration} for this configuration.

View File

@ -4,12 +4,14 @@ 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(Configuration configuration) {
protected ConfigurationOptions(final Configuration configuration)
{
this.configuration = configuration;
}
@ -18,7 +20,8 @@ public class ConfigurationOptions {
*
* @return Parent configuration
*/
public Configuration configuration() {
public Configuration configuration()
{
return configuration;
}
@ -31,7 +34,8 @@ public class ConfigurationOptions {
*
* @return Path separator
*/
public char pathSeparator() {
public char pathSeparator()
{
return pathSeparator;
}
@ -45,8 +49,9 @@ public class ConfigurationOptions {
* @param value Path separator
* @return This object, for chaining
*/
public ConfigurationOptions pathSeparator(char value) {
this.pathSeparator = value;
public ConfigurationOptions pathSeparator(final char value)
{
pathSeparator = value;
return this;
}
@ -64,7 +69,8 @@ public class ConfigurationOptions {
*
* @return Whether or not defaults are directly copied
*/
public boolean copyDefaults() {
public boolean copyDefaults()
{
return copyDefaults;
}
@ -83,8 +89,9 @@ public class ConfigurationOptions {
* @param value Whether or not defaults are directly copied
* @return This object, for chaining
*/
public ConfigurationOptions copyDefaults(boolean value) {
this.copyDefaults = value;
public ConfigurationOptions copyDefaults(final boolean value)
{
copyDefaults = value;
return this;
}
}

View File

@ -7,7 +7,8 @@ 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>
@ -22,7 +23,7 @@ public interface ConfigurationSection {
* list.
* @return Set of keys contained within this ConfigurationSection.
*/
public Set<String> getKeys(boolean deep);
public Set<String> getKeys(final boolean deep);
/**
* Gets a Map containing all keys and their values for this section.
@ -38,7 +39,7 @@ public interface ConfigurationSection {
* list.
* @return Map of keys and values of this section.
*/
public Map<String, Object> getValues(boolean deep);
public Map<String, Object> getValues(final boolean deep);
/**
* Checks if this {@link ConfigurationSection} contains the given path.
@ -51,7 +52,7 @@ public interface ConfigurationSection {
* default or being set.
* @throws IllegalArgumentException Thrown when path is null.
*/
public boolean contains(String path);
public boolean contains(final String path);
/**
* Checks if this {@link ConfigurationSection} has a value set for the
@ -65,7 +66,7 @@ public interface ConfigurationSection {
* having a default.
* @throws IllegalArgumentException Thrown when path is null.
*/
public boolean isSet(String path);
public boolean isSet(final String path);
/**
* Gets the path of this {@link ConfigurationSection} from its root {@link
@ -132,7 +133,7 @@ public interface ConfigurationSection {
* @param path Path of the Object to get.
* @return Requested Object.
*/
public Object get(String path);
public Object get(final String path);
/**
* Gets the requested Object by path, returning a default value if not
@ -146,7 +147,7 @@ public interface ConfigurationSection {
* @param def The default value to return if the path is not found.
* @return Requested Object.
*/
public Object get(String path, Object def);
public Object get(final String path, final Object def);
/**
* Sets the specified path to the given value.
@ -162,7 +163,7 @@ public interface ConfigurationSection {
* @param path Path of the object to set.
* @param value New value to set the path to.
*/
public void set(String path, Object value);
public void set(final String path, final Object value);
/**
* Creates an empty {@link ConfigurationSection} at the specified path.
@ -174,7 +175,7 @@ public interface ConfigurationSection {
* @param path Path to create the section at.
* @return Newly created section
*/
public ConfigurationSection createSection(String path);
public ConfigurationSection createSection(final String path);
/**
* Creates a {@link ConfigurationSection} at the specified path, with
@ -188,7 +189,7 @@ public interface ConfigurationSection {
* @param map The values to used.
* @return Newly created section
*/
public ConfigurationSection createSection(String path, Map<?, ?> map);
public ConfigurationSection createSection(final String path, final Map<?, ?> map);
// Primitives
/**
@ -201,7 +202,7 @@ public interface ConfigurationSection {
* @param path Path of the String to get.
* @return Requested String.
*/
public String getString(String path);
public String getString(final String path);
/**
* Gets the requested String by path, returning a default value if not
@ -216,7 +217,7 @@ public interface ConfigurationSection {
* not a String.
* @return Requested String.
*/
public String getString(String path, String def);
public String getString(final String path, final String def);
/**
* Checks if the specified path is a String.
@ -229,7 +230,7 @@ public interface ConfigurationSection {
* @param path Path of the String to check.
* @return Whether or not the specified path is a String.
*/
public boolean isString(String path);
public boolean isString(final String path);
/**
* Gets the requested int by path.
@ -241,7 +242,7 @@ public interface ConfigurationSection {
* @param path Path of the int to get.
* @return Requested int.
*/
public int getInt(String path);
public int getInt(final String path);
/**
* Gets the requested int by path, returning a default value if not found.
@ -255,7 +256,7 @@ public interface ConfigurationSection {
* not an int.
* @return Requested int.
*/
public int getInt(String path, int def);
public int getInt(final String path, final int def);
/**
* Checks if the specified path is an int.
@ -268,7 +269,7 @@ public interface ConfigurationSection {
* @param path Path of the int to check.
* @return Whether or not the specified path is an int.
*/
public boolean isInt(String path);
public boolean isInt(final String path);
/**
* Gets the requested boolean by path.
@ -280,7 +281,7 @@ public interface ConfigurationSection {
* @param path Path of the boolean to get.
* @return Requested boolean.
*/
public boolean getBoolean(String path);
public boolean getBoolean(final String path);
/**
* Gets the requested boolean by path, returning a default value if not
@ -295,7 +296,7 @@ public interface ConfigurationSection {
* not a boolean.
* @return Requested boolean.
*/
public boolean getBoolean(String path, boolean def);
public boolean getBoolean(final String path, final boolean def);
/**
* Checks if the specified path is a boolean.
@ -308,7 +309,7 @@ public interface ConfigurationSection {
* @param path Path of the boolean to check.
* @return Whether or not the specified path is a boolean.
*/
public boolean isBoolean(String path);
public boolean isBoolean(final String path);
/**
* Gets the requested double by path.
@ -320,7 +321,7 @@ public interface ConfigurationSection {
* @param path Path of the double to get.
* @return Requested double.
*/
public double getDouble(String path);
public double getDouble(final String path);
/**
* Gets the requested double by path, returning a default value if not
@ -335,7 +336,7 @@ public interface ConfigurationSection {
* not a double.
* @return Requested double.
*/
public double getDouble(String path, double def);
public double getDouble(final String path, final double def);
/**
* Checks if the specified path is a double.
@ -348,7 +349,7 @@ public interface ConfigurationSection {
* @param path Path of the double to check.
* @return Whether or not the specified path is a double.
*/
public boolean isDouble(String path);
public boolean isDouble(final String path);
/**
* Gets the requested long by path.
@ -360,7 +361,7 @@ public interface ConfigurationSection {
* @param path Path of the long to get.
* @return Requested long.
*/
public long getLong(String path);
public long getLong(final String path);
/**
* Gets the requested long by path, returning a default value if not
@ -375,7 +376,7 @@ public interface ConfigurationSection {
* not a long.
* @return Requested long.
*/
public long getLong(String path, long def);
public long getLong(final String path, final long def);
/**
* Checks if the specified path is a long.
@ -388,7 +389,7 @@ public interface ConfigurationSection {
* @param path Path of the long to check.
* @return Whether or not the specified path is a long.
*/
public boolean isLong(String path);
public boolean isLong(final String path);
// Java
/**
@ -401,7 +402,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List.
*/
public List<?> getList(String path);
public List<?> getList(final String path);
/**
* Gets the requested List by path, returning a default value if not
@ -416,7 +417,7 @@ public interface ConfigurationSection {
* not a List.
* @return Requested List.
*/
public List<?> getList(String path, List<?> def);
public List<?> getList(final String path, final List<?> def);
/**
* Checks if the specified path is a List.
@ -429,7 +430,7 @@ public interface ConfigurationSection {
* @param path Path of the List to check.
* @return Whether or not the specified path is a List.
*/
public boolean isList(String path);
public boolean isList(final String path);
/**
* Gets the requested List of String by path.
@ -444,7 +445,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of String.
*/
public List<String> getStringList(String path);
public List<String> getStringList(final String path);
/**
* Gets the requested List of Integer by path.
@ -459,7 +460,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Integer.
*/
public List<Integer> getIntegerList(String path);
public List<Integer> getIntegerList(final String path);
/**
* Gets the requested List of Boolean by path.
@ -474,7 +475,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Boolean.
*/
public List<Boolean> getBooleanList(String path);
public List<Boolean> getBooleanList(final String path);
/**
* Gets the requested List of Double by path.
@ -489,7 +490,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Double.
*/
public List<Double> getDoubleList(String path);
public List<Double> getDoubleList(final String path);
/**
* Gets the requested List of Float by path.
@ -504,7 +505,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Float.
*/
public List<Float> getFloatList(String path);
public List<Float> getFloatList(final String path);
/**
* Gets the requested List of Long by path.
@ -519,7 +520,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Long.
*/
public List<Long> getLongList(String path);
public List<Long> getLongList(final String path);
/**
* Gets the requested List of Byte by path.
@ -534,7 +535,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Byte.
*/
public List<Byte> getByteList(String path);
public List<Byte> getByteList(final String path);
/**
* Gets the requested List of Character by path.
@ -549,7 +550,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Character.
*/
public List<Character> getCharacterList(String path);
public List<Character> getCharacterList(final String path);
/**
* Gets the requested List of Short by path.
@ -564,7 +565,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Short.
*/
public List<Short> getShortList(String path);
public List<Short> getShortList(final String path);
/**
* Gets the requested List of Maps by path.
@ -579,7 +580,7 @@ public interface ConfigurationSection {
* @param path Path of the List to get.
* @return Requested List of Maps.
*/
public List<Map<?, ?>> getMapList(String path);
public List<Map<?, ?>> getMapList(final String path);
/**
* Gets the requested ConfigurationSection by path.
@ -592,7 +593,7 @@ public interface ConfigurationSection {
* @param path Path of the ConfigurationSection to get.
* @return Requested ConfigurationSection.
*/
public ConfigurationSection getConfigurationSection(String path);
public ConfigurationSection getConfigurationSection(final String path);
/**
* Checks if the specified path is a ConfigurationSection.
@ -606,7 +607,7 @@ public interface ConfigurationSection {
* @param path Path of the ConfigurationSection to check.
* @return Whether or not the specified path is a ConfigurationSection.
*/
public boolean isConfigurationSection(String path);
public boolean isConfigurationSection(final String path);
/**
* Gets the equivalent {@link ConfigurationSection} from the default
@ -638,5 +639,5 @@ public interface ConfigurationSection {
* @param value Value to set the default to.
* @throws IllegalArgumentException Thrown if path is null.
*/
public void addDefault(String path, Object value);
public void addDefault(final String path, final Object value);
}

View File

@ -4,13 +4,15 @@ 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
@ -18,7 +20,8 @@ public class InvalidConfigurationException extends Exception {
*
* @param msg The details of the exception.
*/
public InvalidConfigurationException(String msg) {
public InvalidConfigurationException(final String msg)
{
super(msg);
}
@ -28,7 +31,8 @@ public class InvalidConfigurationException extends Exception {
*
* @param cause The cause of the exception.
*/
public InvalidConfigurationException(Throwable cause) {
public InvalidConfigurationException(final Throwable cause)
{
super(cause);
}
@ -39,7 +43,8 @@ public class InvalidConfigurationException extends Exception {
* @param cause The cause of the exception.
* @param msg The details of the exception.
*/
public InvalidConfigurationException(String msg, Throwable cause) {
public InvalidConfigurationException(final String msg, final Throwable cause)
{
super(msg, cause);
}
}

View File

@ -7,14 +7,16 @@ 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
@ -23,51 +25,67 @@ public class MemoryConfiguration extends MemorySection implements Configuration
* @param defaults Default value provider
* @throws IllegalArgumentException Thrown if defaults is null
*/
public MemoryConfiguration(Configuration defaults) {
public MemoryConfiguration(final Configuration defaults)
{
this.defaults = defaults;
}
@Override
public void addDefault(String path, 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);
}
public void addDefaults(Map<String, Object> defaults) {
if (defaults == null) throw new NullPointerException("Defaults may not be null");
@Override
public void addDefaults(final Map<String, Object> defaults)
{
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
for (Map.Entry<String, Object> entry : defaults.entrySet()) {
for (final Map.Entry<String, Object> entry : defaults.entrySet())
{
addDefault(entry.getKey(), entry.getValue());
}
}
public void addDefaults(Configuration defaults) {
if (defaults == null) throw new NullPointerException("Defaults may not be null");
@Override
public void addDefaults(final Configuration defaults)
{
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
addDefaults(defaults.getValues(true));
}
public void setDefaults(Configuration defaults) {
if (defaults == null) throw new NullPointerException("Defaults may not be null");
@Override
public void setDefaults(final Configuration defaults)
{
if (defaults == null) { throw new NullPointerException("Defaults may not be null"); }
this.defaults = defaults;
}
public Configuration getDefaults() {
@Override
public Configuration getDefaults()
{
return defaults;
}
@Override
public ConfigurationSection getParent() {
public ConfigurationSection getParent()
{
return null;
}
public MemoryConfigurationOptions options() {
if (options == null) {
@Override
public MemoryConfigurationOptions options()
{
if (options == null)
{
options = new MemoryConfigurationOptions(this);
}

View File

@ -4,24 +4,29 @@ package com.intellectualcrafters.configuration;
* Various settings for controlling the input and output of a {@link
* MemoryConfiguration}
*/
public class MemoryConfigurationOptions extends ConfigurationOptions {
protected MemoryConfigurationOptions(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(boolean value) {
public MemoryConfigurationOptions copyDefaults(final boolean value)
{
super.copyDefaults(value);
return this;
}
@Override
public MemoryConfigurationOptions pathSeparator(char value) {
public MemoryConfigurationOptions pathSeparator(final char value)
{
super.pathSeparator(value);
return this;
}

View File

@ -24,7 +24,8 @@ 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
@ -49,7 +50,8 @@ 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();
@ -63,7 +65,8 @@ public abstract class FileConfiguration extends MemoryConfiguration {
/**
* Creates an empty {@link FileConfiguration} with no default values.
*/
public FileConfiguration() {
public FileConfiguration()
{
super();
}
@ -73,7 +76,8 @@ public abstract class FileConfiguration extends MemoryConfiguration {
*
* @param defaults Default value provider
*/
public FileConfiguration(Configuration defaults) {
public FileConfiguration(final Configuration defaults)
{
super(defaults);
}
@ -92,17 +96,21 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* any reason.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void save(File file) throws IOException {
if (file == null) throw new NullPointerException("File cannot be null");
public void save(final File file) throws IOException
{
if (file == null) { throw new NullPointerException("File cannot be null"); }
file.getParentFile().mkdirs();
String data = saveToString();
final String data = saveToString();
Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? StandardCharsets.UTF_8 : Charset.defaultCharset());
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? StandardCharsets.UTF_8 : Charset.defaultCharset());
try {
try
{
writer.write(data);
} finally {
}
finally
{
writer.close();
}
}
@ -122,8 +130,9 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* any reason.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void save(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));
}
@ -157,8 +166,9 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* a valid Configuration.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void load(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);
@ -184,8 +194,9 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @see #load(Reader)
*/
@Deprecated
public void load(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()));
}
@ -203,19 +214,24 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* represent a valid Configuration
* @throws IllegalArgumentException thrown when reader is null
*/
public void load(Reader reader) throws IOException, InvalidConfigurationException {
BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
public void load(final Reader reader) throws IOException, InvalidConfigurationException
{
final BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
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();
}
@ -240,8 +256,9 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* a valid Configuration.
* @throws IllegalArgumentException Thrown when file is null.
*/
public void load(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));
}
@ -261,7 +278,7 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* invalid.
* @throws IllegalArgumentException Thrown if contents is null.
*/
public abstract void loadFromString(String contents) throws InvalidConfigurationException;
public abstract void loadFromString(final String contents) throws InvalidConfigurationException;
/**
* Compiles the header for this {@link FileConfiguration} and returns the
@ -276,11 +293,13 @@ public abstract class FileConfiguration extends MemoryConfiguration {
protected abstract String buildHeader();
@Override
public FileConfigurationOptions options() {
if (options == null) {
public FileConfigurationOptions options()
{
if (options == null)
{
options = new FileConfigurationOptions(this);
}
return (FileConfigurationOptions) options;
}
}
}

View File

@ -7,27 +7,32 @@ 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(MemoryConfiguration configuration) {
protected FileConfigurationOptions(final MemoryConfiguration configuration)
{
super(configuration);
}
@Override
public FileConfiguration configuration() {
public FileConfiguration configuration()
{
return (FileConfiguration) super.configuration();
}
@Override
public FileConfigurationOptions copyDefaults(boolean value) {
public FileConfigurationOptions copyDefaults(final boolean value)
{
super.copyDefaults(value);
return this;
}
@Override
public FileConfigurationOptions pathSeparator(char value) {
public FileConfigurationOptions pathSeparator(final char value)
{
super.pathSeparator(value);
return this;
}
@ -46,7 +51,8 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions {
*
* @return Header
*/
public String header() {
public String header()
{
return header;
}
@ -65,8 +71,9 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions {
* @param value New header
* @return This object, for chaining
*/
public FileConfigurationOptions header(String value) {
this.header = value;
public FileConfigurationOptions header(final String value)
{
header = value;
return this;
}
@ -88,7 +95,8 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions {
*
* @return Whether or not to copy the header
*/
public boolean copyHeader() {
public boolean copyHeader()
{
return copyHeader;
}
@ -111,7 +119,8 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions {
* @param value Whether or not to copy the header
* @return This object, for chaining
*/
public FileConfigurationOptions copyHeader(boolean value) {
public FileConfigurationOptions copyHeader(final boolean value)
{
copyHeader = value;
return this;

View File

@ -22,7 +22,8 @@ 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();
@ -30,16 +31,18 @@ public class YamlConfiguration extends FileConfiguration {
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);
String header = buildHeader();
final String header = buildHeader();
String dump = yaml.dump(getValues(false));
if (dump.equals(BLANK_CONFIG)) {
if (dump.equals(BLANK_CONFIG))
{
dump = "";
}
@ -47,63 +50,85 @@ public class YamlConfiguration extends FileConfiguration {
}
@Override
public void loadFromString(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 (YAMLException e) {
}
catch (final YAMLException e)
{
throw new InvalidConfigurationException(e);
} catch (ClassCastException e) {
}
catch (final ClassCastException e)
{
throw new InvalidConfigurationException("Top level is not a Map.");
}
String header = parseHeader(contents);
if (header.length() > 0) {
final String header = parseHeader(contents);
if (header.length() > 0)
{
options().header(header);
}
if (input != null) {
if (input != null)
{
convertMapsToSections(input, this);
}
}
protected void convertMapsToSections(Map<?, ?> input, ConfigurationSection section) {
for (Map.Entry<?, ?> entry : input.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
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(String input) {
String[] lines = input.split("\r?\n", -1);
StringBuilder result = new StringBuilder();
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++) {
String line = lines[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;
}
}
@ -112,34 +137,35 @@ public class YamlConfiguration extends FileConfiguration {
}
@Override
protected String buildHeader() {
String header = options().header();
protected String buildHeader()
{
final String header = options().header();
if (options().copyHeader()) {
Configuration def = getDefaults();
if (options().copyHeader())
{
final Configuration def = getDefaults();
if ((def != null) && (def instanceof FileConfiguration)) {
FileConfiguration filedefaults = (FileConfiguration) def;
String defaultsHeader = filedefaults.buildHeader();
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 ""; }
StringBuilder builder = new StringBuilder();
String[] lines = header.split("\r?\n", -1);
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;
@ -150,8 +176,10 @@ public class YamlConfiguration extends FileConfiguration {
}
@Override
public YamlConfigurationOptions options() {
if (options == null) {
public YamlConfigurationOptions options()
{
if (options == null)
{
options = new YamlConfigurationOptions(this);
}
@ -171,28 +199,36 @@ public class YamlConfiguration extends FileConfiguration {
* @return Resulting configuration
* @throws IllegalArgumentException Thrown if file is null
*/
public static YamlConfiguration loadConfiguration(File file) {
if (file == null) throw new NullPointerException("File cannot be null");
public static YamlConfiguration loadConfiguration(final File file)
{
if (file == null) { throw new NullPointerException("File cannot be null"); }
YamlConfiguration config = new YamlConfiguration();
final YamlConfiguration config = new YamlConfiguration();
try {
try
{
config.load(file);
} catch (Exception ex) {
try {
String path = file.getAbsolutePath() + "_broken";
}
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);
Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
PS.debug("&dCould not read: &7" + file);
PS.debug("&drenamed to: &7" + dest.getName());
PS.debug("&c============ Full stacktrace ============");
ex.printStackTrace();
PS.debug("&c=========================================");
} catch (IOException e) {
}
catch (final IOException e)
{
e.printStackTrace();
}
}
@ -215,17 +251,23 @@ public class YamlConfiguration extends FileConfiguration {
* @see #loadConfiguration(Reader)
*/
@Deprecated
public static YamlConfiguration loadConfiguration(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"); }
YamlConfiguration config = new YamlConfiguration();
final YamlConfiguration config = new YamlConfiguration();
try {
try
{
config.load(stream);
} catch (IOException ex) {
}
catch (final IOException ex)
{
PS.debug("Cannot load configuration from stream");
ex.printStackTrace();
} catch (InvalidConfigurationException ex) {
}
catch (final InvalidConfigurationException ex)
{
ex.printStackTrace();
PS.debug("Cannot load configuration from stream");
}
@ -233,7 +275,6 @@ public class YamlConfiguration extends FileConfiguration {
return config;
}
/**
* Creates a new {@link YamlConfiguration}, loading from the given reader.
* <p>
@ -245,17 +286,23 @@ public class YamlConfiguration extends FileConfiguration {
* @return resulting configuration
* @throws IllegalArgumentException Thrown if stream is null
*/
public static YamlConfiguration loadConfiguration(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"); }
YamlConfiguration config = new YamlConfiguration();
final YamlConfiguration config = new YamlConfiguration();
try {
try
{
config.load(reader);
} catch (IOException ex) {
}
catch (final IOException ex)
{
PS.debug("Cannot load configuration from stream");
ex.printStackTrace();
} catch (InvalidConfigurationException ex) {
}
catch (final InvalidConfigurationException ex)
{
PS.debug("Cannot load configuration from stream");
ex.printStackTrace();
}

View File

@ -4,38 +4,45 @@ 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(YamlConfiguration configuration) {
protected YamlConfigurationOptions(final YamlConfiguration configuration)
{
super(configuration);
}
@Override
public YamlConfiguration configuration() {
public YamlConfiguration configuration()
{
return (YamlConfiguration) super.configuration();
}
@Override
public YamlConfigurationOptions copyDefaults(boolean value) {
public YamlConfigurationOptions copyDefaults(final boolean value)
{
super.copyDefaults(value);
return this;
}
@Override
public YamlConfigurationOptions pathSeparator(char value) {
public YamlConfigurationOptions pathSeparator(final char value)
{
super.pathSeparator(value);
return this;
}
@Override
public YamlConfigurationOptions header(String value) {
public YamlConfigurationOptions header(final String value)
{
super.header(value);
return this;
}
@Override
public YamlConfigurationOptions copyHeader(boolean value) {
public YamlConfigurationOptions copyHeader(final boolean value)
{
super.copyHeader(value);
return this;
}
@ -47,7 +54,8 @@ public class YamlConfigurationOptions extends FileConfigurationOptions {
*
* @return How much to indent by
*/
public int indent() {
public int indent()
{
return indent;
}
@ -59,11 +67,12 @@ public class YamlConfigurationOptions extends FileConfigurationOptions {
* @param value New indent
* @return This object, for chaining
*/
public YamlConfigurationOptions indent(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"); }
this.indent = value;
indent = value;
return this;
}
}

View File

@ -10,30 +10,37 @@ import org.yaml.snakeyaml.nodes.Tag;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization;
public class YamlConstructor extends SafeConstructor {
public class YamlConstructor extends SafeConstructor
{
public YamlConstructor() {
this.yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
public YamlConstructor()
{
yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
}
private class ConstructCustomObject extends ConstructYamlMap {
private class ConstructCustomObject extends ConstructYamlMap
{
@Override
public Object construct(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); }
Map<?, ?> raw = (Map<?, ?>) super.construct(node);
final Map<?, ?> raw = (Map<?, ?>) super.construct(node);
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
for (Map.Entry<?, ?> entry : raw.entrySet()) {
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())
{
typed.put(entry.getKey().toString(), entry.getValue());
}
try {
try
{
return ConfigurationSerialization.deserializeObject(typed);
} catch (IllegalArgumentException ex) {
}
catch (final IllegalArgumentException ex)
{
throw new YAMLException("Could not deserialize object", ex);
}
}
@ -42,7 +49,8 @@ public class YamlConstructor extends SafeConstructor {
}
@Override
public void construct2ndStep(Node node, Object object) {
public void construct2ndStep(final Node node, final Object object)
{
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
}

View File

@ -10,25 +10,31 @@ import com.intellectualcrafters.configuration.ConfigurationSection;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerializable;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization;
public class YamlRepresenter extends Representer {
public class YamlRepresenter extends Representer
{
public YamlRepresenter() {
this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection());
this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable());
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(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(Object data) {
ConfigurationSerializable serializable = (ConfigurationSerializable) data;
Map<String, Object> values = new LinkedHashMap<String, Object>();
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());

View File

@ -21,7 +21,8 @@ import java.util.Map;
* @see DelegateDeserialization
* @see SerializableAs
*/
public interface ConfigurationSerializable {
public interface ConfigurationSerializable
{
/**
* Creates a Map representation of this class.

View File

@ -14,103 +14,130 @@ 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(Class<? extends ConfigurationSerializable> clazz) {
protected ConfigurationSerialization(final Class<? extends ConfigurationSerializable> clazz)
{
this.clazz = clazz;
}
protected Method getMethod(String name, boolean isStatic) {
try {
Method method = clazz.getDeclaredMethod(name, Map.class);
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 (NoSuchMethodException ex) {
}
catch (final NoSuchMethodException ex)
{
return null;
} catch (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 (NoSuchMethodException ex) {
}
catch (final NoSuchMethodException ex)
{
return null;
} catch (SecurityException ex) {
}
catch (final SecurityException ex)
{
return null;
}
}
protected ConfigurationSerializable deserializeViaMethod(Method method, Map<String, ?> args) {
try {
ConfigurationSerializable result = (ConfigurationSerializable) method.invoke(null, args);
protected ConfigurationSerializable deserializeViaMethod(final Method method, final Map<String, ?> args)
{
try
{
final ConfigurationSerializable result = (ConfigurationSerializable) method.invoke(null, args);
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 {
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
{
return result;
}
} catch (Throwable ex) {
}
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);
Level.SEVERE,
"Could not call method '" + method.toString() + "' of " + clazz + " for deserialization",
ex instanceof InvocationTargetException ? ex.getCause() : ex);
}
return null;
}
protected ConfigurationSerializable deserializeViaCtor(Constructor<? extends ConfigurationSerializable> ctor, Map<String, ?> args) {
try {
protected ConfigurationSerializable deserializeViaCtor(final Constructor<? extends ConfigurationSerializable> ctor, final Map<String, ?> args)
{
try
{
return ctor.newInstance(args);
} catch (Throwable ex) {
}
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);
Level.SEVERE,
"Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization",
ex instanceof InvocationTargetException ? ex.getCause() : ex);
}
return null;
}
public ConfigurationSerializable deserialize(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) {
Constructor<? extends ConfigurationSerializable> constructor = getConstructor();
if (result == null)
{
final Constructor<? extends ConfigurationSerializable> constructor = getConstructor();
if (constructor != null) {
if (constructor != null)
{
result = deserializeViaCtor(constructor, args);
}
}
@ -133,7 +160,8 @@ public class ConfigurationSerialization {
* @param clazz Class to deserialize into
* @return New instance of the specified class
*/
public static ConfigurationSerializable deserializeObject(Map<String, ?> args, Class<? extends ConfigurationSerializable> clazz) {
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args, final Class<? extends ConfigurationSerializable> clazz)
{
return new ConfigurationSerialization(clazz).deserialize(args);
}
@ -151,25 +179,28 @@ public class ConfigurationSerialization {
* @param args Arguments for deserialization
* @return New instance of the specified class
*/
public static ConfigurationSerializable deserializeObject(Map<String, ?> args) {
public static ConfigurationSerializable deserializeObject(final Map<String, ?> args)
{
Class<? extends ConfigurationSerializable> clazz = null;
if (args.containsKey(SERIALIZED_TYPE_KEY)) {
try {
String alias = (String) args.get(SERIALIZED_TYPE_KEY);
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 (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 + "')");
}
@ -182,10 +213,12 @@ public class ConfigurationSerialization {
*
* @param clazz Class to register
*/
public static void registerClass(Class<? extends ConfigurationSerializable> clazz) {
DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);
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());
}
@ -199,7 +232,8 @@ public class ConfigurationSerialization {
* @param alias Alias to register as
* @see SerializableAs
*/
public static void registerClass(Class<? extends ConfigurationSerializable> clazz, String alias) {
public static void registerClass(final Class<? extends ConfigurationSerializable> clazz, final String alias)
{
aliases.put(alias, clazz);
}
@ -208,7 +242,8 @@ public class ConfigurationSerialization {
*
* @param alias Alias to unregister
*/
public static void unregisterClass(String alias) {
public static void unregisterClass(final String alias)
{
aliases.remove(alias);
}
@ -218,9 +253,10 @@ public class ConfigurationSerialization {
*
* @param clazz Class to unregister
*/
public static void unregisterClass(Class<? extends ConfigurationSerializable> clazz) {
while (aliases.values().remove(clazz)) {
}
public static void unregisterClass(final Class<? extends ConfigurationSerializable> clazz)
{
while (aliases.values().remove(clazz))
{}
}
/**
@ -230,7 +266,8 @@ public class ConfigurationSerialization {
* @param alias Alias of the serializable
* @return Registered class, or null if not found
*/
public static Class<? extends ConfigurationSerializable> getClassByAlias(String alias) {
public static Class<? extends ConfigurationSerializable> getClassByAlias(final String alias)
{
return aliases.get(alias);
}
@ -241,23 +278,27 @@ public class ConfigurationSerialization {
* @param clazz Class to get alias for
* @return Alias to use for the class
*/
public static String getAlias(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) {
SerializableAs alias = clazz.getAnnotation(SerializableAs.class);
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();

View File

@ -11,7 +11,8 @@ 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

View File

@ -21,7 +21,8 @@ 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>