Skip to content

Commit

Permalink
core: Abstract wrappers to deduplicate code + allow to convert commen…
Browse files Browse the repository at this point in the history
…ted configurations
  • Loading branch information
TheElectronWill committed Apr 17, 2017
1 parent 5aab7a2 commit 0a662bc
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 309 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.electronwill.nightconfig.core.conversion;

import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.utils.ConfigWrapper;
import com.electronwill.nightconfig.core.utils.TransformingMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* @author TheElectronWill
*/
abstract class AbstractConvertedConfig<C extends Config> extends ConfigWrapper<C>
implements Config {
final Function<Object, Object> readConversion, writeConversion;
final Predicate<Class<?>> supportPredicate;

AbstractConvertedConfig(C config, Function<Object, Object> readConversion,
Function<Object, Object> writeConversion,
Predicate<Class<?>> supportPredicate) {
super(config);
this.readConversion = readConversion;
this.writeConversion = writeConversion;
this.supportPredicate = supportPredicate;
}

@Override
public <T> T setValue(List<String> path, Object value) {
return (T)readConversion.apply(config.setValue(path, writeConversion.apply(value)));
}

@Override
public Map<String, Object> valueMap() {
return new TransformingMap<>(config.valueMap(), readConversion, writeConversion,
writeConversion);
}

@Override
public <T> T getValue(List<String> path) {
return (T)readConversion.apply(config.getValue(path));
}

@Override
public boolean supportsType(Class<?> type) {
return supportPredicate.test(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.electronwill.nightconfig.core.conversion;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.utils.TransformingSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* A Config's wrapper that converts the values that are read from and put into the config.
*
* @author TheElectronWill
*/
public final class CommentedConvertedConfig extends AbstractConvertedConfig<CommentedConfig>
implements CommentedConfig {
/**
* Creates a new ConvertedConfig that uses two conversion tables.
*
* @param config the config to wrap
* @param readTable the ConversionTable used for parse operations (like getValue)
* @param writeTable the ConversionTable used for write operations (like setValue)
* @param supportPredicate a Predicate that checks if a given class is supported by the
* ConvertedConfig
*/
public CommentedConvertedConfig(CommentedConfig config, ConversionTable readTable,
ConversionTable writeTable,
Predicate<Class<?>> supportPredicate) {
this(config, readTable::convert, writeTable::convert, supportPredicate);
}

/**
* Creates a new ConvertedConfig that uses two custom conversion functions.
*
* @param config the config to wrap
* @param readConversion the Function used for parse operations (like getValue)
* @param writeConversion the Function used for write operations (like setValue)
* @param supportPredicate a Predicate that checks if a given class is supported by the
* ConvertedConfig
*/
public CommentedConvertedConfig(CommentedConfig config, Function<Object, Object> readConversion,
Function<Object, Object> writeConversion,
Predicate<Class<?>> supportPredicate) {
super(config, readConversion, writeConversion, supportPredicate);
}

@Override
public String getComment(List<String> path) {
return config.getComment(path);
}

@Override
public boolean containsComment(List<String> path) {
return config.containsComment(path);
}

@Override
public String setComment(List<String> path, String comment) {
return config.setComment(path, comment);
}

@Override
public void removeComment(List<String> path) {
config.removeComment(path);
}

@Override
public Set<? extends CommentedConfig.Entry> entrySet() {
Function<CommentedConfig.Entry, CommentedConfig.Entry> readTransfo = entry -> new CommentedConfig.Entry() {
@Override
public String getComment() {
return entry.getComment();
}

@Override
public String setComment(String comment) {
return entry.setComment(comment);
}

@Override
public void removeComment() {
entry.removeComment();
}

@Override
public String getKey() {
return entry.getKey();
}

@Override
public <T> T getValue() {
return (T)readConversion.apply(entry.getValue());
}

@Override
public <T> T setValue(Object value) {
return (T)readConversion.apply(entry.setValue(writeConversion.apply(value)));
}
};
return new TransformingSet<>(config.entrySet(), readTransfo, o -> null, e -> e);
}
}
Loading

0 comments on commit 0a662bc

Please sign in to comment.