Skip to content

Commit

Permalink
core: Add getComments and setComments, resolve #20
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Apr 26, 2017
1 parent 82cef7a commit 0024db9
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,49 @@ default String removeComment(String path) {
*/
void clearComments();

/**
* Sets the comments of the config to the content of the specified Map. The Map isn't
* directly used, its content is copied.
*
* @param comments the comments to set
*/
default void setComments(Map<String, CommentNode> comments) {
for (Map.Entry<String, CommentNode> entry : comments.entrySet()) {
String key = entry.getKey();
CommentNode node = entry.getValue();
String comment = node.getComment();
if (comment != null) {
setComment(Collections.singletonList(key), comment);
}
Map<String, CommentNode> children = node.getChildren();
if (children != null) {
CommentedConfig config = getValue(Collections.singletonList(key));
config.setComments(children);
}
}
}

/**
* Copies the comments of a config to this config.
*
* @param commentedConfig the config to copy its comments
*/
default void setComments(UnmodifiableCommentedConfig commentedConfig) {
for (UnmodifiableCommentedConfig.Entry entry : commentedConfig.entrySet()) {
String key = entry.getKey();
String comment = entry.getComment();
if (comment != null) {
setComment(Collections.singletonList(key), comment);
}
Object value = entry.getValue();
if (value instanceof UnmodifiableCommentedConfig) {
CommentedConfig config = getValue(Collections.singletonList(key));
config.setComments((UnmodifiableCommentedConfig)value);
}

}
}

@Override
default UnmodifiableCommentedConfig unmodifiable() {
return new UnmodifiableCommentedConfig() {
Expand Down Expand Up @@ -94,6 +137,11 @@ public Map<String, String> commentMap() {
return Collections.unmodifiableMap(CommentedConfig.this.commentMap());
}

@Override
public Map<String, CommentNode> getComments() {
return CommentedConfig.this.getComments();
}

@Override
public Set<? extends Entry> entrySet() {
return CommentedConfig.this.entrySet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.electronwill.nightconfig.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -82,6 +83,58 @@ default boolean containsComment(String path) {
*/
Map<String, String> commentMap();

/**
* Returns a Map containing a deep copy of all the comments in the config.
*
* @return a Map containing the comments in the config.
*/
default Map<String, CommentNode> getComments() {
Map<String, CommentNode> map = new HashMap<>();
for (Entry entry : entrySet()) {
String key = entry.getKey();
String comment = entry.getComment();
Object value = entry.getValue();
if (comment != null || value instanceof UnmodifiableCommentedConfig) {
Map<String, CommentNode> children = (value instanceof UnmodifiableCommentedConfig)
? ((UnmodifiableCommentedConfig)value).getComments()
: null;
CommentNode node = new CommentNode(comment, children);
map.put(key, node);
}
}
return map;
}

final class CommentNode {
private final String comment;
private final Map<String, CommentNode> children;

/**
* Creates a new CommentNode.
*
* @param comment the comment
* @param children the children Map, may be null
*/
public CommentNode(String comment, Map<String, CommentNode> children) {
this.comment = comment;
this.children = children;
}

/**
* @return the node's comment
*/
public String getComment() {
return comment;
}

/**
* @return the Map of the node's children
*/
public Map<String, CommentNode> getChildren() {
return children;
}
}

@Override
Set<? extends Entry> entrySet();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.electronwill.nightconfig.core.conversion;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.UnmodifiableCommentedConfig;
import com.electronwill.nightconfig.core.utils.TransformingSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -70,6 +71,21 @@ public void clearComments() {
config.clearComments();
}

@Override
public Map<String, CommentNode> getComments() {
return config.getComments();
}

@Override
public void setComments(Map<String, CommentNode> comments) {
config.setComments(comments);
}

@Override
public void setComments(UnmodifiableCommentedConfig commentedConfig) {
config.setComments(commentedConfig);
}

@Override
public Map<String, String> commentMap() {
return config.commentMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.electronwill.nightconfig.core.utils;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.UnmodifiableCommentedConfig;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -49,4 +50,19 @@ public Set<? extends CommentedConfig.Entry> entrySet() {
public void clearComments() {
config.clearComments();
}

@Override
public void setComments(Map<String, CommentNode> comments) {
config.setComments(comments);
}

@Override
public void setComments(UnmodifiableCommentedConfig commentedConfig) {
config.setComments(commentedConfig);
}

@Override
public Map<String, CommentNode> getComments() {
return config.getComments();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.UnmodifiableCommentedConfig;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -61,6 +62,17 @@ public String removeComment(List<String> path) {
@Override
public void clearComments() {}

@Override
public Map<String, CommentNode> getComments() {
return Collections.emptyMap();
}

@Override
public void setComments(Map<String, CommentNode> comments) {}

@Override
public void setComments(UnmodifiableCommentedConfig commentedConfig) {}

@Override
public Map<String, String> commentMap() {
return Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public boolean containsComment(List<String> path) {
return false;
}

@Override
public Map<String, CommentNode> getComments() {
return Collections.emptyMap();
}

@Override
public Map<String, String> commentMap() {
return Collections.emptyMap();
Expand Down

0 comments on commit 0024db9

Please sign in to comment.