-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoublePreference.java
More file actions
58 lines (50 loc) · 1.42 KB
/
DoublePreference.java
File metadata and controls
58 lines (50 loc) · 1.42 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.DoubleSupplier;
/**
* A class which represents a double-valued preference
*
* @author James Hagborg
*/
public class DoublePreference extends Preference implements DoubleSupplier {
private final double m_default;
private double m_lastValue;
/**
* Create a {@link DoublePreference} 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 DoublePreference(String name, double value) {
super(name);
m_lastValue = value;
m_default = value;
}
/** {@inheritDoc} */
@Override
protected synchronized boolean hasChanged() {
double newValue = get();
boolean changed = newValue != m_lastValue;
m_lastValue = newValue;
return changed;
}
/** {@inheritDoc} */
@Override
protected void putDefaultValue() {
Preferences.setDouble(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#getDouble(String, double)
*/
public double get() {
return Preferences.getDouble(getName(), m_default);
}
@Override
public double getAsDouble() {
return this.get();
}
}