1 | package tudelft.mentalhealth.perfectfit.updatefuncs;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.time.LocalDateTime;
|
---|
5 | import java.time.OffsetDateTime;
|
---|
6 | import java.time.format.DateTimeFormatter;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Objects;
|
---|
10 |
|
---|
11 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
13 |
|
---|
14 | import tudelft.dialogmanager.parameters.Parameters;
|
---|
15 | import tudelft.dialogmanager.parameters.StringValue;
|
---|
16 | import tudelft.dialogmanager.updatefunctions.UpdateFunction;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Function to convert given time (epoch = time since 1970 in seconds) to human
|
---|
20 | * readable string.
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class DateString implements UpdateFunction {
|
---|
24 | private final String timeepochdname;
|
---|
25 | private final String timestringname;
|
---|
26 |
|
---|
27 | public static DateTimeFormatter dateformat = DateTimeFormatter
|
---|
28 | .ofPattern("dd-MM-yyyy");
|
---|
29 |
|
---|
30 | /**
|
---|
31 | *
|
---|
32 | * @param timeepochdname the name of the param containing the epoch (seconds
|
---|
33 | * since 1970)
|
---|
34 | * @param timestringname the name of the param that will be filled with the
|
---|
35 | * string representation.
|
---|
36 | */
|
---|
37 | @JsonCreator
|
---|
38 | public DateString(@JsonProperty("timefieldname") String timeepochdname,
|
---|
39 | @JsonProperty("timeepochdname") String timestringname)
|
---|
40 | throws IOException {
|
---|
41 | this.timeepochdname = timeepochdname;
|
---|
42 | this.timestringname = timestringname;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Parameters call(Parameters parameters) {
|
---|
47 | Double t = parameters.getDouble(timeepochdname);
|
---|
48 | if (t == null)
|
---|
49 | throw new IllegalArgumentException("parameter " + timeepochdname
|
---|
50 | + " has not been set to a double");
|
---|
51 | LocalDateTime localdate = LocalDateTime.ofEpochSecond(Math.round(t), 0,
|
---|
52 | OffsetDateTime.now().getOffset());
|
---|
53 | return parameters.with(timestringname,
|
---|
54 | new StringValue(localdate.format(dateformat)));
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public List<String> getAssignedParameters() {
|
---|
59 | return Arrays.asList(timestringname);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public int hashCode() {
|
---|
64 | return Objects.hash(timeepochdname, timestringname);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public boolean equals(Object obj) {
|
---|
69 | if (this == obj)
|
---|
70 | return true;
|
---|
71 | if (obj == null)
|
---|
72 | return false;
|
---|
73 | if (getClass() != obj.getClass())
|
---|
74 | return false;
|
---|
75 | DateString other = (DateString) obj;
|
---|
76 | return Objects.equals(timeepochdname, other.timeepochdname)
|
---|
77 | && Objects.equals(timestringname, other.timestringname);
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|