added loadAndSave method

dev
Exlll 8 years ago
parent 174fd25fe5
commit 720ca8f47f

@ -1,44 +1,84 @@
package de.exlll.configlib;
import org.yaml.snakeyaml.parser.ParserException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
public abstract class Configuration {
private final Path configPath;
private final Comments comments;
private final FieldMapper fieldMapper;
private final ConfigurationWriter writer;
public Configuration(Path configPath) {
Objects.requireNonNull(configPath);
this.configPath = configPath;
FilteredFieldStreamSupplier ffss = new FilteredFieldStreamSupplier(
getClass(), ConfigurationFieldFilter.INSTANCE);
this.comments = Comments.from(ffss);
Comments comments = Comments.from(ffss);
this.configPath = configPath;
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 {
createParentDirectories();
Map<String, Object> valuesByFieldNames = fieldMapper
.mapFieldNamesToValues(this);
Map<String, Object> valuesByFieldNames = fieldMapper.mapFieldNamesToValues(this);
String dump = YamlSerializer.serialize(valuesByFieldNames);
ConfigurationWriter writer = new ConfigurationWriter(configPath, comments);
writer.write(dump);
}
private void createParentDirectories() throws IOException {
Path parentDirectory = configPath.getParent();
Files.createDirectories(parentDirectory);
/**
* Loads and saves the configuration file.
*
* @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 {
// TODO save if not existent
String dump = new ConfigurationReader(configPath).read();
Map<String, Object> valuesByFieldNames = YamlSerializer.deserialize(dump);
fieldMapper.mapValuesToFields(valuesByFieldNames, this);
private void createParentDirectories() throws IOException {
Path parentDirectory = configPath.getParent();
if (Files.notExists(parentDirectory)) {
Files.createDirectories(parentDirectory);
}
}
}

@ -67,13 +67,20 @@ public class ConfigurationTest {
public void load() throws Exception {
new OriginalTestClass(filePath).save();
ChangedTestClass cls = new ChangedTestClass(filePath);
cls.load();
cls.loadAndSave();
assertThat(cls.a, is(0));
assertThat(cls.b, is(1));
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 int a = 0;
private int b = 1;

@ -11,11 +11,17 @@ This library facilitates the creation, saving and loading of YAML configuration
## How-to
##### Creating a configuration
To create a new configuration, you need a class which extends `Configuration`. Instances of this
class have a `load` and a `save` method. Calling `save` dumps all fields which are not
`final`, `static` or `transient` to a configuration file. If the file is present, it is overriden;
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.
To create a new configuration, create a class which extends `Configuration`. Fields which are
added to this class and which are not `final`, `static` or `transient` can automatically be saved
to the corresponding configuration file.
##### 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
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);
try {
config.load();
config.loadAndSave();
} catch (IOException e) {
/* do something with exception */
}

Loading…
Cancel
Save