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.Arrays;
22  
23  import javax.annotation.Nullable;
24  
25  import com.fasterxml.jackson.annotation.JsonCreator;
26  import com.fasterxml.jackson.annotation.JsonInclude;
27  import com.fasterxml.jackson.annotation.JsonInclude.Include;
28  import com.fasterxml.jackson.annotation.JsonProperty;
29  import com.google.common.base.MoreObjects.ToStringHelper;
30  
31  import com.linecorp.centraldogma.common.Author;
32  
33  /**
34   * A {@link Command} which is used for creating a new repository.
35   */
36  @JsonInclude(Include.NON_NULL)
37  public final class CreateRepositoryCommand extends ProjectCommand<Void> {
38  
39      private final String repositoryName;
40      @Nullable
41      private final byte[] wdek;
42  
43      @JsonCreator
44      CreateRepositoryCommand(@JsonProperty("timestamp") @Nullable Long timestamp,
45                              @JsonProperty("author") @Nullable Author author,
46                              @JsonProperty("projectName") String projectName,
47                              @JsonProperty("repositoryName") String repositoryName,
48                              @JsonProperty("wdek") @Nullable byte[] wdek) {
49          super(CommandType.CREATE_REPOSITORY, timestamp, author, projectName);
50          this.repositoryName = requireNonNull(repositoryName, "repositoryName");
51          this.wdek = wdek != null ? wdek.clone() : null;
52      }
53  
54      /**
55       * Returns the repository name.
56       */
57      @JsonProperty
58      public String repositoryName() {
59          return repositoryName;
60      }
61  
62      /**
63       * Returns the WDEK of the repository.
64       */
65      @JsonProperty
66      @Nullable
67      public byte[] wdek() {
68          return wdek != null ? wdek.clone() : null;
69      }
70  
71      @Override
72      public boolean equals(Object obj) {
73          if (this == obj) {
74              return true;
75          }
76  
77          if (!(obj instanceof CreateRepositoryCommand)) {
78              return false;
79          }
80  
81          final CreateRepositoryCommand that = (CreateRepositoryCommand) obj;
82          return super.equals(obj) &&
83                 repositoryName.equals(that.repositoryName) &&
84                 Arrays.equals(wdek, that.wdek);
85      }
86  
87      @Override
88      public int hashCode() {
89          return (repositoryName.hashCode() * 31 + Arrays.hashCode(wdek)) * 31 + super.hashCode();
90      }
91  
92      @Override
93      ToStringHelper toStringHelper() {
94          final ToStringHelper toStringHelper = super.toStringHelper()
95                                                     .add("repositoryName", repositoryName);
96          if (wdek != null) {
97              toStringHelper.add("wdek", "[***]");
98          }
99          return toStringHelper;
100     }
101 }