-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBooleanPreference.java
More file actions
58 lines (50 loc) · 1.44 KB
/
BooleanPreference.java
File metadata and controls
58 lines (50 loc) · 1.44 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
package org.hyperonline.hyperlib.pref;
import edu.wpi.first.wpilibj.Preferences;
import java.util.function.BooleanSupplier;
/**
* A class which represents a boolean-valued preference
*
* @author James Hagborg
*/
public class BooleanPreference extends Preference implements BooleanSupplier {
private final boolean m_default;
private boolean m_lastValue;
/**
* Create a {@link BooleanPreference} 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 BooleanPreference(String name, boolean value) {
super(name);
m_lastValue = value;
m_default = value;
}
/** {@inheritDoc} */
@Override
protected synchronized boolean hasChanged() {
boolean newValue = get();
boolean changed = newValue != m_lastValue;
m_lastValue = newValue;
return changed;
}
/** {@inheritDoc} */
@Override
protected void putDefaultValue() {
Preferences.setBoolean(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#getBoolean(String, boolean)
*/
public boolean get() {
return Preferences.getBoolean(getName(), m_default);
}
@Override
public boolean getAsBoolean() {
return this.get();
}
}