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

Commit c16aa3d

Browse files
author
Jacob van Mourik
committed
Improved ExtendedProperties class.
1 parent 6f89dda commit c16aa3d

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/main/java/com/jvms/i18neditor/Editor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private void restoreEditorState() {
417417
settings.getIntegerProperty("window_width", 1024),
418418
settings.getIntegerProperty("window_height", 768)));
419419
pack();
420-
if (settings.containsKey("window_pos_x") && settings.containsKey("window_pos_y")) {
420+
if (settings.containsKeys("window_pos_x", "window_pos_y")) {
421421
setLocation(settings.getIntegerProperty("window_pos_x"),
422422
settings.getIntegerProperty("window_pos_y"));
423423
} else {

src/main/java/com/jvms/i18neditor/util/ExtendedProperties.java

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,53 @@
1515
/**
1616
* This class extends {@link Properties}.
1717
*
18-
* <p>This implementation adds the ability to load and store properties from a given file path.<br>
19-
* It also adds support to retrieve and store {@code Integer} and {@code List} values.</p>
18+
* <p>This implementation adds the ability to load and store properties from a given file path
19+
* and adds support to retrieve and store {@code Integer} and {@code List} values.</p>
20+
*
21+
* <p>This implementation also adds extended functionality like {@link #containsKeys(String...)}.</p>
2022
*
2123
* @author Jacob
2224
*/
2325
public class ExtendedProperties extends Properties {
2426
private static final long serialVersionUID = 6042931434040718478L;
2527

26-
private static final String LIST_SEPARATOR = ",";
28+
protected String separator;
29+
30+
/**
31+
* Creates an empty property list with no default values and "," as list separator.
32+
*/
33+
public ExtendedProperties() {
34+
this(null, ",");
35+
}
36+
37+
/**
38+
* Creates an empty property list with no default values and the specified list separator.
39+
*
40+
* @param separator the separator used for storing list values.
41+
*/
42+
public ExtendedProperties(String separator) {
43+
this(null, separator);
44+
}
45+
46+
/**
47+
* Creates an empty property list with the specified defaults and "," as list separator.
48+
*
49+
* @param defaults the defaults.
50+
*/
51+
public ExtendedProperties(Properties defaults) {
52+
this(defaults, ",");
53+
}
54+
55+
/**
56+
* Creates an empty property list with the specified defaults and list separator.
57+
*
58+
* @param defaults the defaults.
59+
* @param separator the separator used for storing list values.
60+
*/
61+
public ExtendedProperties(Properties defaults, String separator) {
62+
super(defaults);
63+
this.separator = separator;
64+
}
2765

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

5694
/**
5795
* Sets a value in the property list. The list of values will be converted
58-
* to a single string separated by {@value #LIST_SEPARATOR}.
96+
* to a single string separated by {@value #separator}.
5997
*
6098
* @param key the key to be placed in this property list.
6199
* @param values the value corresponding to {@code key}.
62100
*/
63101
public void setProperty(String key, List<String> values) {
64-
setProperty(key, values.stream().collect(Collectors.joining(LIST_SEPARATOR)));
102+
setProperty(key, values.stream().collect(Collectors.joining(separator)));
65103
}
66104

67105
/**
@@ -84,7 +122,7 @@ public void setProperty(String key, Integer value) {
84122
*/
85123
public List<String> getListProperty(String key) {
86124
String value = getProperty(key);
87-
return value == null ? Lists.newLinkedList() : Lists.newLinkedList(Arrays.asList(value.split(LIST_SEPARATOR)));
125+
return value == null ? Lists.newLinkedList() : Lists.newLinkedList(Arrays.asList(value.split(separator)));
88126
}
89127

90128
/**
@@ -102,7 +140,7 @@ public Integer getIntegerProperty(String key) {
102140
/**
103141
* See {@link #getIntegerProperty(String)}. This method returns {@code defaultValue} when
104142
* there is no value in the property list with the specified {@code key}.
105-
*
143+
*
106144
* @param key the property key.
107145
* @param defaultValue the default value to return when there is no value for the specified key
108146
* @return the value in this property list with the specified key value or the defaultValue
@@ -112,4 +150,15 @@ public Integer getIntegerProperty(String key, Integer defaultValue) {
112150
Integer value = getIntegerProperty(key);
113151
return value != null ? value : defaultValue;
114152
}
153+
154+
/**
155+
* This function does the same as {@link #containsKey(Object)}, only for multiple keys.
156+
*
157+
* @param keys possible keys.
158+
* @return {@code true} if and only if the specified objects are keys in this hashtable,
159+
* as determined by the equals method; false otherwise.
160+
*/
161+
public boolean containsKeys(String... keys) {
162+
return Arrays.asList(keys).stream().allMatch(k -> containsKey(k));
163+
}
115164
}

0 commit comments

Comments
 (0)