source: PerfectFit/Dialog/src/main/java/tudelft/mentalhealth/perfectfit/updatefuncs/DateString.java

Last change on this file was 7, checked in by Wouter Pasman, 9 months ago

#124 release PerfectFit sources

File size: 2.3 KB
Line 
1package tudelft.mentalhealth.perfectfit.updatefuncs;
2
3import java.io.IOException;
4import java.time.LocalDateTime;
5import java.time.OffsetDateTime;
6import java.time.format.DateTimeFormatter;
7import java.util.Arrays;
8import java.util.List;
9import java.util.Objects;
10
11import com.fasterxml.jackson.annotation.JsonCreator;
12import com.fasterxml.jackson.annotation.JsonProperty;
13
14import tudelft.dialogmanager.parameters.Parameters;
15import tudelft.dialogmanager.parameters.StringValue;
16import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.