1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Specifies when an object is accessed by whom.
34   */
35  public final class UserAndTimestamp {
36  
37      /**
38       * Creates a new instance using the specified {@link Author}'s e-mail address as a user name and
39       * the current time as a timestamp.
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       * Creates a new instance using the current time as a timestamp.
52       */
53      public UserAndTimestamp(String user) {
54          this(user, Instant.now());
55      }
56  
57      /**
58       * Creates a new instance.
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       * Returns a {@code login} name who took any action on this object.
75       */
76      @JsonProperty
77      public String user() {
78          return user;
79      }
80  
81      /**
82       * Returns a date and time string which is formatted as ISO-8601.
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       * Returns the epoch milliseconds of the {@link #timestamp()}.
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 }