Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Jun 15, 2015
1 parent 818d3bd commit 20d39f7
Showing 1 changed file with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,49 +193,49 @@ private void extractPropertiesFromServices(Properties properties,
}
}

@SuppressWarnings("unchecked")
private void flatten(Properties properties, Map<String, Object> input, String path) {
for (Entry<String, Object> entry : input.entrySet()) {
String key = entry.getKey();
if (StringUtils.hasText(path)) {
if (key.startsWith("[")) {
key = path + key;
}
else {
key = path + "." + key;
}
}
String key = getFullKey(path, entry.getKey());
Object value = entry.getValue();
if (value instanceof String) {
properties.put(key, value);
}
else if (value instanceof Number) {
properties.put(key, value.toString());
}
else if (value instanceof Boolean) {
properties.put(key, value.toString());
}
else if (value instanceof Map) {
if (value instanceof Map) {
// Need a compound key
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
flatten(properties, map, key);
flatten(properties, (Map<String, Object>) value, key);
}
else if (value instanceof Collection) {
// Need a compound key
@SuppressWarnings("unchecked")
Collection<Object> collection = (Collection<Object>) value;
properties.put(key,
StringUtils.collectionToCommaDelimitedString(collection));
int count = 0;
for (Object object : collection) {
flatten(properties,
Collections.singletonMap("[" + (count++) + "]", object), key);
for (Object item : collection) {
String itemKey = "[" + (count++) + "]";
flatten(properties, Collections.singletonMap(itemKey, item), key);
}
}
else if (value instanceof String) {
properties.put(key, value);
}
else if (value instanceof Number) {
properties.put(key, value.toString());
}
else if (value instanceof Boolean) {
properties.put(key, value.toString());
}
else {
properties.put(key, value == null ? "" : value);
}
}
}

private String getFullKey(String path, String key) {
if (!StringUtils.hasText(path)) {
return key;
}
if (key.startsWith("[")) {
return path + key;
}
return path + "." + key;
}

}

0 comments on commit 20d39f7

Please sign in to comment.