1   /*
2    * Copyright 2017 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.linecorp.centraldogma.internal.Util;
27  
28  /**
29   * A set of {@link Change}s and its metadata.
30   */
31  public class Commit {
32  
33      private final Revision revision;
34      private final Author author;
35      private final long when;
36      private final String summary;
37      private final String detail;
38      private final Markup markup;
39      @Nullable
40      private String whenAsText;
41  
42      /**
43       * Creates a new instance. The time and date of the new {@link Commit} is set to the current time and date.
44       *
45       * @param revision the {@link Revision} of this {@link Commit}
46       * @param author the {@link Author} of this {@link Commit}
47       * @param summary the human-readable summary of this {@link Commit}
48       * @param detail the human-readable detailed description of this {@link Commit}
49       * @param markup the {@link Markup} language of {@code summary} and {@code detail}
50       */
51      public Commit(Revision revision, Author author, String summary, String detail, Markup markup) {
52          this(revision, author, System.currentTimeMillis(), summary, detail, markup);
53      }
54  
55      /**
56       * Creates a new instance.
57       *
58       * @param revision the {@link Revision} of this {@link Commit}
59       * @param author the {@link Author} of this {@link Commit}
60       * @param when the time and date of this {@link Commit},
61       *             represented as the number of milliseconds since the epoch (midnight, January 1, 1970 UTC)
62       * @param summary the human-readable summary of this {@link Commit}
63       * @param detail the human-readable detailed description of this {@link Commit}
64       * @param markup the {@link Markup} language of {@code summary} and {@code detail}
65       */
66      public Commit(Revision revision, Author author, long when, String summary, String detail, Markup markup) {
67          this.revision = requireNonNull(revision, "revision");
68          this.author = requireNonNull(author, "author");
69          this.summary = requireNonNull(summary, "summary");
70          this.detail = requireNonNull(detail, "detail");
71          this.markup = requireNonNull(markup, "markup");
72          this.when = when / 1000L * 1000L; // Drop the milliseconds
73      }
74  
75      /**
76       * Returns the {@link Revision} of this {@link Commit}.
77       */
78      public Revision revision() {
79          return revision;
80      }
81  
82      /**
83       * Returns the time and date of this {@link Commit}.
84       *
85       * @return the number of milliseconds since the epoch (midnight, January 1, 1970 UTC)
86       */
87      public long when() {
88          return when;
89      }
90  
91      /**
92       * Returns the time and date of this {@link Commit} in
93       * <a href="https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations">ISO 8601
94       * combined date and time representation</a>.
95       */
96      public String whenAsText() {
97          if (whenAsText == null) {
98              whenAsText = DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(when()));
99          }
100         return whenAsText;
101     }
102 
103     /**
104      * Returns the {@link Author} of this {@link Commit}.
105      */
106     public Author author() {
107         return author;
108     }
109 
110     /**
111      * Returns the human-readable summary of this {@link Commit}.
112      */
113     public String summary() {
114         return summary;
115     }
116 
117     /**
118      * Returns the human-readable detailed description of this {@link Commit}.
119      */
120     public String detail() {
121         return detail;
122     }
123 
124     /**
125      * Returns the {@link Markup} language of {@link #summary()} and {@link #detail()}.
126      */
127     public Markup markup() {
128         return markup;
129     }
130 
131     @Override
132     public int hashCode() {
133         return (revision.hashCode() * 31 + author.hashCode()) * 31 + (int) (when / 1000);
134     }
135 
136     @Override
137     public boolean equals(Object o) {
138         if (this == o) {
139             return true;
140         }
141         if (!(o instanceof Commit)) {
142             return false;
143         }
144 
145         final Commit that = (Commit) o;
146         return revision.equals(that.revision) && author.equals(that.author) && when == that.when &&
147                summary.equals(that.summary) && detail.equals(that.detail) && markup == that.markup;
148     }
149 
150     @Override
151     public String toString() {
152         final StringBuilder buf = new StringBuilder(128);
153 
154         buf.append(Util.simpleTypeName(this));
155         buf.append('[');
156         buf.append(revision.major());
157         buf.append(": author=");
158         buf.append(author.email());
159         buf.append(", when=");
160         buf.append(whenAsText());
161         buf.append(", summary=");
162         buf.append(summary);
163         buf.append(']');
164 
165         return buf.toString();
166     }
167 }