-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedDouble.java
More file actions
37 lines (31 loc) · 941 Bytes
/
CachedDouble.java
File metadata and controls
37 lines (31 loc) · 941 Bytes
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
package frc.spectrumLib;
import edu.wpi.first.wpilibj2.command.Subsystem;
import java.util.function.DoubleSupplier;
/**
* CachedDouble allows for a value to only be checked once per periodic loop if it is called by
* multiple methods. Periodic is run first, so the value will be updated before it is used in any
* Triggers or Command
*/
public class CachedDouble implements DoubleSupplier, Subsystem {
private boolean isCached;
private double value;
private DoubleSupplier canCall;
public CachedDouble(DoubleSupplier canCall) {
this.canCall = canCall;
value = canCall.getAsDouble();
isCached = true;
this.register();
}
@Override
public void periodic() {
isCached = false;
}
@Override
public double getAsDouble() {
if (!isCached) {
value = canCall.getAsDouble();
isCached = true;
}
return value;
}
}