Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Commit

Permalink
Improved ExtendedProperties class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob van Mourik committed Jul 22, 2015
1 parent 6f89dda commit c16aa3d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jvms/i18neditor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private void restoreEditorState() {
settings.getIntegerProperty("window_width", 1024),
settings.getIntegerProperty("window_height", 768)));
pack();
if (settings.containsKey("window_pos_x") && settings.containsKey("window_pos_y")) {
if (settings.containsKeys("window_pos_x", "window_pos_y")) {
setLocation(settings.getIntegerProperty("window_pos_x"),
settings.getIntegerProperty("window_pos_y"));
} else {
Expand Down
63 changes: 56 additions & 7 deletions src/main/java/com/jvms/i18neditor/util/ExtendedProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,53 @@
/**
* This class extends {@link Properties}.
*
* <p>This implementation adds the ability to load and store properties from a given file path.<br>
* It also adds support to retrieve and store {@code Integer} and {@code List} values.</p>
* <p>This implementation adds the ability to load and store properties from a given file path
* and adds support to retrieve and store {@code Integer} and {@code List} values.</p>
*
* <p>This implementation also adds extended functionality like {@link #containsKeys(String...)}.</p>
*
* @author Jacob
*/
public class ExtendedProperties extends Properties {
private static final long serialVersionUID = 6042931434040718478L;

private static final String LIST_SEPARATOR = ",";
protected String separator;

/**
* Creates an empty property list with no default values and "," as list separator.
*/
public ExtendedProperties() {
this(null, ",");
}

/**
* Creates an empty property list with no default values and the specified list separator.
*
* @param separator the separator used for storing list values.
*/
public ExtendedProperties(String separator) {
this(null, separator);
}

/**
* Creates an empty property list with the specified defaults and "," as list separator.
*
* @param defaults the defaults.
*/
public ExtendedProperties(Properties defaults) {
this(defaults, ",");
}

/**
* Creates an empty property list with the specified defaults and list separator.
*
* @param defaults the defaults.
* @param separator the separator used for storing list values.
*/
public ExtendedProperties(Properties defaults, String separator) {
super(defaults);
this.separator = separator;
}

/**
* Reads a property list from the given file path.
Expand Down Expand Up @@ -55,13 +93,13 @@ public void store(Path path) {

/**
* Sets a value in the property list. The list of values will be converted
* to a single string separated by {@value #LIST_SEPARATOR}.
* to a single string separated by {@value #separator}.
*
* @param key the key to be placed in this property list.
* @param values the value corresponding to {@code key}.
*/
public void setProperty(String key, List<String> values) {
setProperty(key, values.stream().collect(Collectors.joining(LIST_SEPARATOR)));
setProperty(key, values.stream().collect(Collectors.joining(separator)));
}

/**
Expand All @@ -84,7 +122,7 @@ public void setProperty(String key, Integer value) {
*/
public List<String> getListProperty(String key) {
String value = getProperty(key);
return value == null ? Lists.newLinkedList() : Lists.newLinkedList(Arrays.asList(value.split(LIST_SEPARATOR)));
return value == null ? Lists.newLinkedList() : Lists.newLinkedList(Arrays.asList(value.split(separator)));
}

/**
Expand All @@ -102,7 +140,7 @@ public Integer getIntegerProperty(String key) {
/**
* See {@link #getIntegerProperty(String)}. This method returns {@code defaultValue} when
* there is no value in the property list with the specified {@code key}.
*
*
* @param key the property key.
* @param defaultValue the default value to return when there is no value for the specified key
* @return the value in this property list with the specified key value or the defaultValue
Expand All @@ -112,4 +150,15 @@ public Integer getIntegerProperty(String key, Integer defaultValue) {
Integer value = getIntegerProperty(key);
return value != null ? value : defaultValue;
}

/**
* This function does the same as {@link #containsKey(Object)}, only for multiple keys.
*
* @param keys possible keys.
* @return {@code true} if and only if the specified objects are keys in this hashtable,
* as determined by the equals method; false otherwise.
*/
public boolean containsKeys(String... keys) {
return Arrays.asList(keys).stream().allMatch(k -> containsKey(k));
}
}

0 comments on commit c16aa3d

Please sign in to comment.