1 | package tudelft.mentalhealth.perfectfit.updatefuncs;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
8 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
9 |
|
---|
10 | import tudelft.dialogmanager.parameters.DoubleValue;
|
---|
11 | import tudelft.dialogmanager.parameters.Parameters;
|
---|
12 | import tudelft.dialogmanager.updatefunctions.UpdateFunction;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Function to get difference between two timestamps (in seconds since 1970).
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class TimeDifference implements UpdateFunction {
|
---|
19 |
|
---|
20 | private final String previousTimeFieldname;
|
---|
21 | private final String differenceFieldname;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | *
|
---|
25 | * @param previousTimeFieldname the name of the parameter containing the
|
---|
26 | * previous time
|
---|
27 | * @param differenceFieldname the parameter name that will be filled with
|
---|
28 | * the difference (seconds) between now and
|
---|
29 | * previous time.
|
---|
30 | */
|
---|
31 | @JsonCreator
|
---|
32 | public TimeDifference(
|
---|
33 | @JsonProperty("previousTimeFieldname") String previousTimeFieldname,
|
---|
34 | @JsonProperty("differenceFieldname") String differenceFieldname)
|
---|
35 | throws IOException {
|
---|
36 | this.previousTimeFieldname = previousTimeFieldname;
|
---|
37 | this.differenceFieldname = differenceFieldname;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public Parameters call(Parameters parameters) {
|
---|
42 | return parameters.with(differenceFieldname,
|
---|
43 | new DoubleValue(System.currentTimeMillis() / 1000d
|
---|
44 | - parameters.getDouble(previousTimeFieldname)));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public List<String> getAssignedParameters() {
|
---|
49 | return Arrays.asList(differenceFieldname);
|
---|
50 | }
|
---|
51 |
|
---|
52 | }
|
---|