renamed modules

dev
Exlll 8 years ago
parent e7ce5ad632
commit fbca8cdcb0

@ -17,7 +17,7 @@ public abstract class Configuration {
/**
* Creates a new {@code Configuration} instance.
*
* <p>
* You can use {@link File#toPath()} to obtain a {@link Path} object from a {@link File}.
*
* @param configPath location of the configuration file
@ -85,9 +85,6 @@ public abstract class Configuration {
}
private void createParentDirectories() throws IOException {
Path parentDirectory = configPath.getParent();
if (Files.notExists(parentDirectory)) {
Files.createDirectories(parentDirectory);
}
Files.createDirectories(configPath.getParent());
}
}

@ -4,7 +4,7 @@ import java.io.IOException;
import java.nio.file.Path;
import java.util.Scanner;
class ConfigurationReader {
final class ConfigurationReader {
private final Path path;
ConfigurationReader(Path path) {

@ -2,10 +2,10 @@ package de.exlll.configlib;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toMap;
final class FieldMapper {
private final FilteredFieldStreamSupplier streamSupplier;
@ -16,15 +16,11 @@ final class FieldMapper {
}
Map<String, Object> mapFieldNamesToValues(Object instance) {
Map<String, Object> valuesByFieldNames = new LinkedHashMap<>();
List<Field> fields = streamSupplier.get().collect(Collectors.toList());
for (Field field : fields) {
Object value = getValue(field, instance);
valuesByFieldNames.put(field.getName(), value);
}
return valuesByFieldNames;
return streamSupplier.get().collect(
toMap(Field::getName,
field -> getValue(field, instance),
(f1, f2) -> f1,
LinkedHashMap::new));
}
private Object getValue(Field field, Object instance) {
@ -38,9 +34,7 @@ final class FieldMapper {
}
void mapValuesToFields(Map<String, Object> valuesByFieldNames, Object instance) {
List<Field> fields = streamSupplier.get().collect(Collectors.toList());
for (Field field : fields) {
for (Field field : streamSupplier.toList()) {
String fieldName = field.getName();
if (valuesByFieldNames.containsKey(fieldName)) {
Object value = valuesByFieldNames.get(fieldName);

@ -2,21 +2,21 @@ package de.exlll.configlib;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
final class FilteredFieldStreamSupplier implements Supplier<Stream<Field>> {
private final Class<?> cls;
private final Predicate<Field> fieldFilter;
private final Supplier<Stream<Field>> streamSupplier;
FilteredFieldStreamSupplier(Class<?> cls, Predicate<Field> fieldFilter) {
Objects.requireNonNull(cls);
Objects.requireNonNull(fieldFilter);
this.cls = cls;
this.fieldFilter = fieldFilter;
Field[] fields = cls.getDeclaredFields();
streamSupplier = () -> Arrays.stream(fields).filter(fieldFilter);
}
@ -26,6 +26,10 @@ final class FilteredFieldStreamSupplier implements Supplier<Stream<Field>> {
return streamSupplier.get();
}
public List<Field> toList() {
return streamSupplier.get().collect(Collectors.toList());
}
public Class<?> getSupplyingClass() {
return cls;
}

@ -6,12 +6,13 @@ import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
public class FilteredFieldStreamSupplierTest {
@Rule
@ -49,6 +50,19 @@ public class FilteredFieldStreamSupplierTest {
assertThat(fieldStream.count(), is(3L));
}
@Test
public void toListReturnsFieldsAsList() throws Exception {
FilteredFieldStreamSupplier supplier = new FilteredFieldStreamSupplier(
TestClass.class, field -> true);
List<Field> fields = supplier.toList();
assertThat(fields.get(0), is(TestClass.class.getDeclaredField("i")));
assertThat(fields.get(1), is(TestClass.class.getDeclaredField("j")));
assertThat(fields.get(2), is(TestClass.class.getDeclaredField("k")));
assertThat(fields.get(3), is(TestClass.class.getDeclaredField("l")));
}
private static final class TestClass {
public int i;
protected int j;

@ -1,5 +1,5 @@
# ConfigLib
This library facilitates the creation, saving and loading of YAML configuration files. It does so
This library facilitates creating, saving and loading YAML configuration files. It does so
by using Reflection on configuration classes and automatically saving and loading their fields,
creating the configuration file and its parent directories if necessary.
@ -17,16 +17,16 @@ added to this class and which are not `final`, `static` or `transient` can autom
##### 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 overridden; otherwise, it is created.
- `load` reads the configuration file and updates the instance's field values.
- `loadAndSave` loads the configuration file and then calls `save` to update the file.
If the file doesn't exist, it is saved.
- `load` reads the configuration file and updates the field values of your instance.
- `save` dumps all field names and values to a configuration file. If the file exists, it is
overridden; otherwise, it is created.
- `loadAndSave` first calls `load` and then `save`, which is useful when you have added or
removed fields from the class or you simply don't know if the configuration file exists.
##### Adding and removing fields
In order to add or to remove fields, you just need to add them to or remove them from your
configuration class. The next time `save` or `loadAndSave` is called, changes are saved to the
configuration file.
configuration class. The changes are saved to the configuration file the next time `save` or
`loadAndSave` is called.
##### Comments
By using the `@Comment` annotation, you can add comments to your configuration file. The

@ -13,44 +13,44 @@ subprojects {
}
}
project(':config-lib-core') {
project(':configlib-core') {
dependencies {
compile group: 'org.yaml', name: 'snakeyaml', version: '1.17'
}
}
project(':config-lib-bukkit') {
project(':configlib-bukkit') {
repositories {
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
}
dependencies {
compile project(':config-lib-core')
compile project(':configlib-core')
compile group: 'org.bukkit', name: 'bukkit', version: '1.11.2-R0.1-SNAPSHOT'
}
jar {
baseName = 'ConfigLib.bukkit'
from {
project(':config-lib-core').sourceSets.main.output
project(':configlib-core').sourceSets.main.output
}
}
}
project(':config-lib-bungee') {
project(':configlib-bungee') {
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
compile project(':config-lib-core')
compile project(':configlib-core')
compile group: 'net.md-5', name: 'bungeecord-api', version: '1.10-SNAPSHOT'
}
jar {
baseName = 'ConfigLib.bungee'
from {
project(':config-lib-core').sourceSets.main.output
project(':configlib-core').sourceSets.main.output
}
}
}

@ -1,9 +1,8 @@
rootProject.name = 'config-lib'
rootProject.name = 'configlib'
include 'ConfigLib-Core'
findProject(':ConfigLib-Core')?.name = 'config-lib-core'
findProject(':ConfigLib-Core')?.name = 'configlib-core'
include 'ConfigLib-Bukkit'
findProject(':ConfigLib-Bukkit')?.name = 'config-lib-bukkit'
findProject(':ConfigLib-Bukkit')?.name = 'configlib-bukkit'
include 'ConfigLib-Bungee'
findProject(':ConfigLib-Bungee')?.name = 'config-lib-bungee'
rootProject.name = 'ConfigLib'
findProject(':ConfigLib-Bungee')?.name = 'configlib-bungee'

Loading…
Cancel
Save