1   /*
2    * Copyright 2018 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.common;
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.google.common.base.MoreObjects;
27  
28  /**
29   * The result of a {@code push} operation.
30   */
31  public class PushResult {
32  
33      private final Revision revision;
34      private final long when;
35      @Nullable
36      private String whenAsText;
37  
38      /**
39       * Creates a new instance.
40       *
41       * @param revision the {@link Revision} of the pushed commit
42       * @param when the time and date of the pushed commit, represented as the number of milliseconds
43       *             since the epoch (midnight, January 1, 1970 UTC)
44       */
45      public PushResult(Revision revision, long when) {
46          this.revision = requireNonNull(revision, "revision");
47          this.when = when / 1000L * 1000L; // Drop the milliseconds
48      }
49  
50      /**
51       * Returns the {@link Revision} of the pushed commit.
52       */
53      public Revision revision() {
54          return revision;
55      }
56  
57      /**
58       * Returns the time and date of the pushed commit.
59       *
60       * @return the number of milliseconds since the epoch (midnight, January 1, 1970 UTC)
61       */
62      public long when() {
63          return when;
64      }
65  
66      /**
67       * Returns the time and date of the pushed commit in
68       * <a href="https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations">ISO 8601
69       * combined date and time representation</a>.
70       */
71      public String whenAsText() {
72          if (whenAsText == null) {
73              whenAsText = DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(when()));
74          }
75          return whenAsText;
76      }
77  
78      @Override
79      public int hashCode() {
80          return revision.hashCode() * 31 + (int) (when / 1000);
81      }
82  
83      @Override
84      public boolean equals(Object o) {
85          if (this == o) {
86              return true;
87          }
88          if (!(o instanceof PushResult)) {
89              return false;
90          }
91  
92          final PushResult that = (PushResult) o;
93          return revision.equals(that.revision) && when == that.when;
94      }
95  
96      @Override
97      public String toString() {
98          return MoreObjects.toStringHelper(this)
99                            .addValue(revision.text())
100                           .add("when", whenAsText())
101                           .toString();
102     }
103 }