-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Abstract wrappers to deduplicate code + allow to convert commen…
…ted configurations
- Loading branch information
1 parent
5aab7a2
commit 0a662bc
Showing
8 changed files
with
333 additions
and
309 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/electronwill/nightconfig/core/conversion/AbstractConvertedConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
.../src/main/java/com/electronwill/nightconfig/core/conversion/CommentedConvertedConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.