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  package com.linecorp.centraldogma.client.updater;
17  
18  import static com.linecorp.centraldogma.internal.Util.validateFilePath;
19  
20  import java.util.Objects;
21  import java.util.Optional;
22  
23  import javax.annotation.Nullable;
24  
25  import com.google.common.base.MoreObjects;
26  import com.google.common.base.Strings;
27  
28  /**
29   * Provides the necessary information to {@link CentralDogmaBeanFactory} so that the bean properties are
30   * mirrored from a file in Central Dogma.
31   *
32   * @see CentralDogmaBean
33   */
34  public final class CentralDogmaBeanConfig {
35  
36      static final CentralDogmaBeanConfig EMPTY = new CentralDogmaBeanConfigBuilder().build();
37  
38      private final String project;
39      private final String repository;
40      private final String path;
41      private final String jsonPath;
42  
43      /**
44       * Creates a new instance.
45       *
46       * @param project the Central Dogma project name
47       * @param repository the Central Dogma repository name
48       * @param path the path of the file in Central Dogma
49       * @param jsonPath the JSON path expression that will be evaluated when retrieving the file
50       */
51      public CentralDogmaBeanConfig(@Nullable String project, @Nullable String repository,
52                                    @Nullable String path, @Nullable String jsonPath) {
53  
54          this.project = Strings.emptyToNull(project);
55          this.repository = Strings.emptyToNull(repository);
56          this.path = Strings.emptyToNull(path);
57          this.jsonPath = Strings.emptyToNull(jsonPath);
58  
59          if (this.path != null) {
60              validateFilePath(this.path, "path");
61          }
62      }
63  
64      /**
65       * Returns the Central Dogma project name.
66       *
67       * @return {@link Optional#empty()} if the project name is unspecified
68       */
69      public Optional<String> project() {
70          return Optional.ofNullable(project);
71      }
72  
73      /**
74       * Returns the Central Dogma repository name.
75       *
76       * @return {@link Optional#empty()} if the repository name is unspecified
77       */
78      public Optional<String> repository() {
79          return Optional.ofNullable(repository);
80      }
81  
82      /**
83       * Returns the path of the file in Central Dogma.
84       *
85       * @return {@link Optional#empty()} if the path is unspecified
86       */
87      public Optional<String> path() {
88          return Optional.ofNullable(path);
89      }
90  
91      /**
92       * Returns the JSON path expression that will be evaluated when retrieving the file.
93       *
94       * @return {@link Optional#empty()} if the JSON path expression is unspecified
95       */
96      public Optional<String> jsonPath() {
97          return Optional.ofNullable(jsonPath);
98      }
99  
100     @Override
101     public boolean equals(Object o) {
102         if (o == this) {
103             return true;
104         }
105         if (!(o instanceof CentralDogmaBeanConfig)) {
106             return false;
107         }
108         final CentralDogmaBeanConfig other = (CentralDogmaBeanConfig) o;
109         return Objects.equals(project, other.project) &&
110                Objects.equals(repository, other.repository) &&
111                Objects.equals(path, other.path) &&
112                Objects.equals(jsonPath, other.jsonPath);
113     }
114 
115     @Override
116     public int hashCode() {
117         return Objects.hash(project, repository, path, jsonPath);
118     }
119 
120     @Override
121     public String toString() {
122         return MoreObjects.toStringHelper(this).omitNullValues()
123                           .add("project", project)
124                           .add("repository", repository)
125                           .add("path", path)
126                           .add("jsonPath", jsonPath)
127                           .toString();
128     }
129 }