1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.linecorp.centraldogma.server.metadata;
18
19 import static java.util.Objects.requireNonNull;
20
21 import java.time.Instant;
22 import java.time.format.DateTimeFormatter;
23
24 import javax.annotation.Nullable;
25
26 import com.fasterxml.jackson.annotation.JsonCreator;
27 import com.fasterxml.jackson.annotation.JsonProperty;
28 import com.google.common.base.MoreObjects;
29
30 import com.linecorp.centraldogma.common.Author;
31
32
33
34
35 public final class UserAndTimestamp {
36
37
38
39
40
41 public static UserAndTimestamp of(Author author) {
42 return new UserAndTimestamp(requireNonNull(author, "author").email());
43 }
44
45 private final String user;
46 private final Instant timestamp;
47 @Nullable
48 private String timestampAsText;
49
50
51
52
53 public UserAndTimestamp(String user) {
54 this(user, Instant.now());
55 }
56
57
58
59
60 public UserAndTimestamp(String user, Instant timestamp) {
61 this.user = requireNonNull(user, "user");
62 this.timestamp = requireNonNull(timestamp, "timestamp");
63 }
64
65 @JsonCreator
66 UserAndTimestamp(@JsonProperty("user") String user,
67 @JsonProperty("timestamp") String timestampAsText) {
68 this.user = requireNonNull(user, "user");
69 this.timestampAsText = requireNonNull(timestampAsText, "timestampAsText");
70 timestamp = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(timestampAsText));
71 }
72
73
74
75
76 @JsonProperty
77 public String user() {
78 return user;
79 }
80
81
82
83
84 @JsonProperty
85 public String timestamp() {
86 if (timestampAsText == null) {
87 timestampAsText = DateTimeFormatter.ISO_INSTANT.format(timestamp);
88 }
89 return timestampAsText;
90 }
91
92
93
94
95 public long timestampMillis() {
96 return timestamp.toEpochMilli();
97 }
98
99 @Override
100 public boolean equals(Object o) {
101 if (this == o) {
102 return true;
103 }
104 if (o == null || getClass() != o.getClass()) {
105 return false;
106 }
107 final UserAndTimestamp that = (UserAndTimestamp) o;
108 return user.equals(that.user) && timestamp.equals(that.timestamp);
109 }
110
111 @Override
112 public int hashCode() {
113 return user.hashCode() * 31 + timestamp.hashCode();
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(this)
119 .add("user", user())
120 .add("timestamp", timestamp())
121 .toString();
122 }
123 }