added loadAndSave method

dev
Exlll 8 years ago
parent 174fd25fe5
commit 720ca8f47f

@ -1,44 +1,84 @@
package de.exlll.configlib; package de.exlll.configlib;
import org.yaml.snakeyaml.parser.ParserException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
public abstract class Configuration { public abstract class Configuration {
private final Path configPath; private final Path configPath;
private final Comments comments;
private final FieldMapper fieldMapper; private final FieldMapper fieldMapper;
private final ConfigurationWriter writer;
public Configuration(Path configPath) { public Configuration(Path configPath) {
Objects.requireNonNull(configPath); Objects.requireNonNull(configPath);
this.configPath = configPath;
FilteredFieldStreamSupplier ffss = new FilteredFieldStreamSupplier( FilteredFieldStreamSupplier ffss = new FilteredFieldStreamSupplier(
getClass(), ConfigurationFieldFilter.INSTANCE); getClass(), ConfigurationFieldFilter.INSTANCE);
this.comments = Comments.from(ffss); Comments comments = Comments.from(ffss);
this.configPath = configPath;
this.fieldMapper = new FieldMapper(ffss); this.fieldMapper = new FieldMapper(ffss);
this.writer = new ConfigurationWriter(configPath, comments);
} }
/**
* Loads the configuration file from the specified {@code Path} and sets {@code this} fields.
*
* @throws ClassCastException if parsed Object is not a {@code Map}
* @throws IOException if an I/O error occurs when loading the configuration file.
* @throws ParserException if invalid YAML
*/
public final void load() throws IOException {
String dump = new ConfigurationReader(configPath).read();
Map<String, Object> valuesByFieldNames = YamlSerializer.deserialize(dump);
fieldMapper.mapValuesToFields(valuesByFieldNames, this);
}
/**
* Saves {@code this} fields and {@code @Comment} annotations to a configuration file at the specified {@code Path}.
* <p>
* Fields which are {@code final}, {@code static} or {@code transient} are ignored.
* <p>
* If the file exists, it is overriden; otherwise, it is created.
*
* @throws IOException if an I/O error occurs when saving the configuration file.
* @throws ParserException if invalid YAML
*/
public final void save() throws IOException { public final void save() throws IOException {
createParentDirectories(); createParentDirectories();
Map<String, Object> valuesByFieldNames = fieldMapper Map<String, Object> valuesByFieldNames = fieldMapper.mapFieldNamesToValues(this);
.mapFieldNamesToValues(this);
String dump = YamlSerializer.serialize(valuesByFieldNames); String dump = YamlSerializer.serialize(valuesByFieldNames);
ConfigurationWriter writer = new ConfigurationWriter(configPath, comments);
writer.write(dump); writer.write(dump);
} }
private void createParentDirectories() throws IOException { /**
Path parentDirectory = configPath.getParent(); * Loads and saves the configuration file.
Files.createDirectories(parentDirectory); *
* @throws ClassCastException if parsed Object is not a {@code Map}
* @throws IOException if an I/O error occurs when loading or saving the configuration file.
* @throws ParserException if invalid YAML
* @see #load()
* @see #save()
*/
public final void loadAndSave() throws IOException {
try {
load();
save();
} catch (NoSuchFileException e) {
save();
}
} }
public final void load() throws IOException { private void createParentDirectories() throws IOException {
// TODO save if not existent Path parentDirectory = configPath.getParent();
String dump = new ConfigurationReader(configPath).read(); if (Files.notExists(parentDirectory)) {
Map<String, Object> valuesByFieldNames = YamlSerializer.deserialize(dump); Files.createDirectories(parentDirectory);
fieldMapper.mapValuesToFields(valuesByFieldNames, this); }
} }
} }

@ -67,13 +67,20 @@ public class ConfigurationTest {
public void load() throws Exception { public void load() throws Exception {
new OriginalTestClass(filePath).save(); new OriginalTestClass(filePath).save();
ChangedTestClass cls = new ChangedTestClass(filePath); ChangedTestClass cls = new ChangedTestClass(filePath);
cls.load(); cls.loadAndSave();
assertThat(cls.a, is(0)); assertThat(cls.a, is(0));
assertThat(cls.b, is(1)); assertThat(cls.b, is(1));
assertThat(cls.c, is(4)); assertThat(cls.c, is(4));
} }
@Test
public void loadAndSaveSavesFile() throws Exception {
new OriginalTestClass(filePath).loadAndSave();
assertThat(Files.exists(filePath), is(true));
}
private static final class OriginalTestClass extends Configuration { private static final class OriginalTestClass extends Configuration {
private int a = 0; private int a = 0;
private int b = 1; private int b = 1;

@ -11,11 +11,17 @@ This library facilitates the creation, saving and loading of YAML configuration
## How-to ## How-to
##### Creating a configuration ##### Creating a configuration
To create a new configuration, you need a class which extends `Configuration`. Instances of this To create a new configuration, create a class which extends `Configuration`. Fields which are
class have a `load` and a `save` method. Calling `save` dumps all fields which are not added to this class and which are not `final`, `static` or `transient` can automatically be saved
`final`, `static` or `transient` to a configuration file. If the file is present, it is overriden; to the corresponding configuration file.
otherwise, it is created. The `load` method tries to load the configuration file. If it fails
because the file doesn't exist, it calls `save` and then tries again. ##### Saving and loading a configuration
Instances of your configuration class have a `load`, `save` and `loadAndSave` method:
- `save` dumps all fields which are not `final`, `static` or `transient` to a configuration file.
If the file exists, it is overriden; otherwise, it is created.
- `load` reads the configuration file and updates the instance's values.
- `loadAndSave` loads the configuration file and then calls `save` to update the file's values.
If the file doesn't exist, it is saved.
##### Adding and removing fields ##### Adding and removing fields
In order to add or to remove fields, you just need to add them to or remove them from your In order to add or to remove fields, you just need to add them to or remove them from your
@ -66,7 +72,7 @@ public class Plugin {
DatabaseConfig config = new DatabaseConfig(path); DatabaseConfig config = new DatabaseConfig(path);
try { try {
config.load(); config.loadAndSave();
} catch (IOException e) { } catch (IOException e) {
/* do something with exception */ /* do something with exception */
} }

Loading…
Cancel
Save