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.fasterxml.jackson.annotation.JsonProperty;
26  import com.google.common.base.MoreObjects.ToStringHelper;
27  
28  import com.linecorp.centraldogma.common.Author;
29  
30  /**
31   * A super class of the {@link Command}s which perform operations on a repository.
32   *
33   * @param <T> the result type of a {@link Command}
34   */
35  public abstract class RepositoryCommand<T> extends AbstractCommand<T> {
36  
37      private final String projectName;
38      private final String repositoryName;
39  
40      RepositoryCommand(CommandType commandType, @Nullable Long timestamp, @Nullable Author author,
41                        String projectName, String repositoryName) {
42          super(commandType, timestamp, author);
43          this.projectName = requireNonNull(projectName, "projectName");
44          this.repositoryName = requireNonNull(repositoryName, "repositoryName");
45      }
46  
47      /**
48       * Returns the project name.
49       */
50      @JsonProperty
51      public final String projectName() {
52          return projectName;
53      }
54  
55      /**
56       * Returns the repository name.
57       */
58      @JsonProperty
59      public final String repositoryName() {
60          return repositoryName;
61      }
62  
63      @Override
64      public final String executionPath() {
65          return String.format("/%s/%s", projectName, repositoryName);
66      }
67  
68      @Override
69      public boolean equals(Object obj) {
70          if (this == obj) {
71              return true;
72          }
73  
74          if (!(obj instanceof RepositoryCommand)) {
75              return false;
76          }
77  
78          final RepositoryCommand<?> that = (RepositoryCommand<?>) obj;
79          return super.equals(that) &&
80                 projectName.equals(that.projectName) &&
81                 repositoryName.equals(that.repositoryName);
82      }
83  
84      @Override
85      public int hashCode() {
86          return Objects.hash(projectName, repositoryName) * 31 + super.hashCode();
87      }
88  
89      @Override
90      ToStringHelper toStringHelper() {
91          return super.toStringHelper()
92                      .add("projectName", projectName)
93                      .add("repositoryName", repositoryName);
94      }
95  }