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

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

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

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

@ -6,12 +6,13 @@ import org.junit.rules.ExpectedException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.*;
public class FilteredFieldStreamSupplierTest { public class FilteredFieldStreamSupplierTest {
@Rule @Rule
@ -49,6 +50,19 @@ public class FilteredFieldStreamSupplierTest {
assertThat(fieldStream.count(), is(3L)); 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 { private static final class TestClass {
public int i; public int i;
protected int j; protected int j;

@ -1,5 +1,5 @@
# ConfigLib # 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, by using Reflection on configuration classes and automatically saving and loading their fields,
creating the configuration file and its parent directories if necessary. 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 ##### Saving and loading a configuration
Instances of your configuration class have a `load`, `save` and `loadAndSave` method: 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. - `load` reads the configuration file and updates the field values of your instance.
If the file exists, it is overridden; otherwise, it is created. - `save` dumps all field names and values to a configuration file. If the file exists, it is
- `load` reads the configuration file and updates the instance's field values. overridden; otherwise, it is created.
- `loadAndSave` loads the configuration file and then calls `save` to update the file. - `loadAndSave` first calls `load` and then `save`, which is useful when you have added or
If the file doesn't exist, it is saved. removed fields from the class or you simply don't know if the configuration file exists.
##### 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
configuration class. The next time `save` or `loadAndSave` is called, changes are saved to the configuration class. The changes are saved to the configuration file the next time `save` or
configuration file. `loadAndSave` is called.
##### Comments ##### Comments
By using the `@Comment` annotation, you can add comments to your configuration file. The 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 { dependencies {
compile group: 'org.yaml', name: 'snakeyaml', version: '1.17' compile group: 'org.yaml', name: 'snakeyaml', version: '1.17'
} }
} }
project(':config-lib-bukkit') { project(':configlib-bukkit') {
repositories { repositories {
maven { maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
} }
} }
dependencies { dependencies {
compile project(':config-lib-core') compile project(':configlib-core')
compile group: 'org.bukkit', name: 'bukkit', version: '1.11.2-R0.1-SNAPSHOT' compile group: 'org.bukkit', name: 'bukkit', version: '1.11.2-R0.1-SNAPSHOT'
} }
jar { jar {
baseName = 'ConfigLib.bukkit' baseName = 'ConfigLib.bukkit'
from { from {
project(':config-lib-core').sourceSets.main.output project(':configlib-core').sourceSets.main.output
} }
} }
} }
project(':config-lib-bungee') { project(':configlib-bungee') {
repositories { repositories {
maven { maven {
url 'https://oss.sonatype.org/content/repositories/snapshots' url 'https://oss.sonatype.org/content/repositories/snapshots'
} }
} }
dependencies { dependencies {
compile project(':config-lib-core') compile project(':configlib-core')
compile group: 'net.md-5', name: 'bungeecord-api', version: '1.10-SNAPSHOT' compile group: 'net.md-5', name: 'bungeecord-api', version: '1.10-SNAPSHOT'
} }
jar { jar {
baseName = 'ConfigLib.bungee' baseName = 'ConfigLib.bungee'
from { 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' include 'ConfigLib-Core'
findProject(':ConfigLib-Core')?.name = 'config-lib-core' findProject(':ConfigLib-Core')?.name = 'configlib-core'
include 'ConfigLib-Bukkit' include 'ConfigLib-Bukkit'
findProject(':ConfigLib-Bukkit')?.name = 'config-lib-bukkit' findProject(':ConfigLib-Bukkit')?.name = 'configlib-bukkit'
include 'ConfigLib-Bungee' include 'ConfigLib-Bungee'
findProject(':ConfigLib-Bungee')?.name = 'config-lib-bungee' findProject(':ConfigLib-Bungee')?.name = 'configlib-bungee'
rootProject.name = 'ConfigLib'

Loading…
Cancel
Save