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.server.command;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Objects;
22  
23  import javax.annotation.Nullable;
24  
25  import com.google.common.base.MoreObjects;
26  
27  import com.linecorp.centraldogma.common.Author;
28  
29  abstract class AbstractCommand<T> implements Command<T> {
30  
31      private final CommandType type;
32      private final long timestamp;
33      private final Author author;
34  
35      protected AbstractCommand(CommandType type, @Nullable Long timestamp, @Nullable Author author) {
36          this.type = requireNonNull(type, "type");
37          this.timestamp = timestamp != null ? timestamp : System.currentTimeMillis();
38          this.author = author != null ? author : Author.SYSTEM;
39      }
40  
41      @Override
42      public final CommandType type() {
43          return type;
44      }
45  
46      @Override
47      public final long timestamp() {
48          return timestamp;
49      }
50  
51      @Override
52      public final Author author() {
53          return author;
54      }
55  
56      @Override
57      public boolean equals(Object obj) {
58          if (this == obj) {
59              return true;
60          }
61          if (!(obj instanceof AbstractCommand)) {
62              return false;
63          }
64  
65          final AbstractCommand<?> that = (AbstractCommand<?>) obj;
66          return type == that.type &&
67                 timestamp == that.timestamp &&
68                 author.equals(that.author);
69      }
70  
71      @Override
72      public int hashCode() {
73          return Objects.hash(type, timestamp, author);
74      }
75  
76      @Override
77      public final String toString() {
78          return toStringHelper().toString();
79      }
80  
81      MoreObjects.ToStringHelper toStringHelper() {
82          return MoreObjects.toStringHelper(this)
83                            .add("type", type)
84                            .add("timestamp", timestamp)
85                            .add("author", author);
86      }
87  }