-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringPreference.java
More file actions
59 lines (50 loc) · 1.45 KB
/
StringPreference.java
File metadata and controls
59 lines (50 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.hyperonline.hyperlib.pref;
import edu.wpi.first.wpilibj.Preferences;
import java.util.function.Supplier;
/**
* A class which represents a string-valued preference
*
* @author James Hagborg
*/
public class StringPreference extends Preference implements Supplier<String> {
private final String m_default;
private String m_lastValue;
/**
* Create a {@link StringPreference} object tracking the preference with the given name and
* default value. Calling this function does not yet modify the preferences file.
*
* @param name The string id of the preference
* @param value The default value
*/
public StringPreference(String name, String value) {
super(name);
if (value == null) {
throw new NullPointerException("value == null");
}
m_lastValue = value;
m_default = value;
}
/** {@inheritDoc} */
@Override
public synchronized boolean hasChanged() {
String newValue = get();
boolean changed = !newValue.equals(m_lastValue);
m_lastValue = newValue;
return changed;
}
/** {@inheritDoc} */
@Override
public void putDefaultValue() {
Preferences.setString(getName(), m_default);
}
/**
* Get the current value of the preferences file entry, or the default if no entry exists.
*
* @return The value of the preference
* @see Preferences#getString(String, String)
*/
@Override
public String get() {
return Preferences.getString(getName(), m_default);
}
}