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 package gov.nist.secauto.metaschema.core.datatype.adapter;
28
29 import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
30
31 import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
32 import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem;
33 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34
35 import java.time.ZonedDateTime;
36 import java.time.format.DateTimeParseException;
37 import java.util.List;
38
39 import edu.umd.cs.findbugs.annotations.NonNull;
40
41 public class DateTimeWithTZAdapter
42 extends AbstractDataTypeAdapter<ZonedDateTime, IDateTimeItem> {
43 @NonNull
44 private static final List<String> NAMES = ObjectUtils.notNull(
45 List.of(
46 "date-time-with-timezone",
47
48 "dateTime-with-timezone"));
49
50 DateTimeWithTZAdapter() {
51 super(ZonedDateTime.class);
52 }
53
54 @Override
55 public List<String> getNames() {
56 return NAMES;
57 }
58
59 @Override
60 public JsonFormatTypes getJsonRawType() {
61 return JsonFormatTypes.STRING;
62 }
63
64 @SuppressWarnings("null")
65 @Override
66 public ZonedDateTime parse(String value) {
67 try {
68 return ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value));
69 } catch (DateTimeParseException ex) {
70 throw new IllegalArgumentException(ex.getLocalizedMessage(), ex);
71 }
72 }
73
74 @SuppressWarnings("null")
75 @Override
76 public String asString(Object value) {
77 return DateFormats.DATE_TIME_WITH_TZ.format((ZonedDateTime) value);
78 }
79
80 @SuppressWarnings("null")
81 @Override
82 public ZonedDateTime copy(Object obj) {
83 return ZonedDateTime.from((ZonedDateTime) obj);
84 }
85
86 @Override
87 public Class<IDateTimeItem> getItemClass() {
88 return IDateTimeItem.class;
89 }
90
91 @Override
92 public IDateTimeItem newItem(Object value) {
93 ZonedDateTime item = toValue(value);
94 return IDateTimeItem.valueOf(item);
95 }
96 }