Skip to content

Commit

Permalink
core: Add SimpleCommentedConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Apr 26, 2017
1 parent 6e3ab81 commit 6bd3a05
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions core/src/main/java/SimpleCommentedConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import com.electronwill.nightconfig.core.AbstractCommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.SimpleConfig;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import java.util.function.Predicate;

/**
* A basic commented configuration.
*
* @author TheElectronWill
*/
public final class SimpleCommentedConfig extends AbstractCommentedConfig {

private final Predicate<Class<?>> supportPredicate;

/**
* Creates a SimpleCommentedConfig that supports the following types:
* <ul>
* <li>Integer, Long, Float and Double
* <li>Boolean
* <li>String
* <li>List and all its implementations
* <li>Config and all its implementations
* </ul>
*/
public SimpleCommentedConfig() {
this.supportPredicate = SimpleConfig.BASIC_SUPPORT_PREDICATE;
}

/**
* Creates a SimpleCommentedConfig that uses the specified Predicate to determines what types
* it supports.
*
* @param supportPredicate the Predicate that returns true when the class it's given is
* supported by the config
*/
public SimpleCommentedConfig(Predicate<Class<?>> supportPredicate) {
this.supportPredicate = supportPredicate;
}

/**
* Creates a SimpleCommentedConfig by copying a config. The supportPredicate will be
* {@link SimpleConfig#BASIC_SUPPORT_PREDICATE}.
*
* @param toCopy the config to copy
*/
public SimpleCommentedConfig(UnmodifiableConfig toCopy) {
this(toCopy, SimpleConfig.BASIC_SUPPORT_PREDICATE);
}

/**
* Creates a SimpleConfig by copying a config.
*
* @param toCopy the config to copy
* @param supportPredicate the Predicate that returns true when the class it's given is
* supported by the config
*/
public SimpleCommentedConfig(UnmodifiableConfig toCopy, Predicate<Class<?>> supportPredicate) {
super(toCopy);
this.supportPredicate = supportPredicate;
}

/**
* Creates a SimpleCommentedConfig by copying a config. The SimpleConfig will supports the same
* types as the specified config.
*
* @param toCopy the config to copy
*/
public SimpleCommentedConfig(Config toCopy) {
this(toCopy, toCopy::supportsType);
}

@Override
public boolean supportsType(Class<?> type) {
return supportPredicate.test(type);
}

@Override
public AbstractCommentedConfig clone() {
return new SimpleCommentedConfig(this);
}

@Override
protected AbstractCommentedConfig createSubConfig() {
return new SimpleCommentedConfig();
}
}

0 comments on commit 6bd3a05

Please sign in to comment.