1   /*
2    * Copyright 2021 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  package com.linecorp.centraldogma.server.command;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.List;
21  import java.util.Objects;
22  
23  import javax.annotation.Nullable;
24  
25  import com.fasterxml.jackson.annotation.JsonProperty;
26  import com.google.common.base.MoreObjects.ToStringHelper;
27  import com.google.common.collect.ImmutableList;
28  
29  import com.linecorp.centraldogma.common.Author;
30  import com.linecorp.centraldogma.common.Change;
31  import com.linecorp.centraldogma.common.Markup;
32  import com.linecorp.centraldogma.common.Revision;
33  
34  /**
35   * A {@link Command} which is used for pushing changes to the repository.
36   */
37  public abstract class AbstractPushCommand<T> extends RepositoryCommand<T> {
38  
39      private final Revision baseRevision;
40      private final String summary;
41      private final String detail;
42      private final Markup markup;
43      private final List<Change<?>> changes;
44  
45      AbstractPushCommand(CommandType type, @Nullable Long timestamp, @Nullable Author author,
46                          String projectName, String repositoryName, Revision baseRevision,
47                          String summary, String detail, Markup markup, Iterable<Change<?>> changes) {
48          super(type, timestamp, author, projectName, repositoryName);
49  
50          this.baseRevision = requireNonNull(baseRevision, "baseRevision");
51          this.summary = requireNonNull(summary, "summary");
52          this.detail = requireNonNull(detail, "detail");
53          this.markup = requireNonNull(markup, "markup");
54  
55          requireNonNull(changes, "changes");
56          this.changes = ImmutableList.copyOf(changes);
57      }
58  
59      /**
60       * Returns the base {@link Revision}.
61       */
62      @JsonProperty
63      public Revision baseRevision() {
64          return baseRevision;
65      }
66  
67      /**
68       * Returns the human-readable summary of the commit.
69       */
70      @JsonProperty
71      public String summary() {
72          return summary;
73      }
74  
75      /**
76       * Returns the human-readable detail of the commit.
77       */
78      @JsonProperty
79      public String detail() {
80          return detail;
81      }
82  
83      /**
84       * Returns the {@link Markup} of the {@link #detail()}.
85       */
86      @JsonProperty
87      public Markup markup() {
88          return markup;
89      }
90  
91      /**
92       * Returns the {@link Change}s of the commit.
93       */
94      @JsonProperty
95      public List<Change<?>> changes() {
96          return changes;
97      }
98  
99      @Override
100     public boolean equals(Object obj) {
101         if (this == obj) {
102             return true;
103         }
104 
105         if (!(obj instanceof AbstractPushCommand)) {
106             return false;
107         }
108 
109         final AbstractPushCommand that = (AbstractPushCommand) obj;
110         return super.equals(that) &&
111                baseRevision.equals(that.baseRevision) &&
112                summary.equals(that.summary) &&
113                detail.equals(that.detail) &&
114                markup == that.markup &&
115                changes.equals(that.changes);
116     }
117 
118     @Override
119     public int hashCode() {
120         return Objects.hash(baseRevision, summary, detail, markup, changes) * 31 + super.hashCode();
121     }
122 
123     @Override
124     ToStringHelper toStringHelper() {
125         return super.toStringHelper()
126                     .add("baseRevision", baseRevision)
127                     .add("summary", summary)
128                     .add("detail", detail)
129                     .add("markup", markup)
130                     .add("changes", changes);
131     }
132 }