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.client.updater;
18  
19  import static com.linecorp.centraldogma.internal.Util.validateFilePath;
20  import static java.util.Objects.requireNonNull;
21  
22  import javax.annotation.Nullable;
23  
24  import com.google.common.base.Strings;
25  
26  /**
27   * Builds a {@link CentralDogmaBeanConfig}.
28   */
29  public final class CentralDogmaBeanConfigBuilder {
30  
31      @Nullable
32      private String project;
33      @Nullable
34      private String repository;
35      @Nullable
36      private String path;
37      @Nullable
38      private String jsonPath;
39  
40      /**
41       * Creates a new builder whose all properties are unspecified.
42       */
43      public CentralDogmaBeanConfigBuilder() {}
44  
45      /**
46       * Creates a new builder from an existing {@link CentralDogmaBeanConfig}. This method is a shortcut of
47       * {@code new CentralDogmaBeanConfigBuilder().merge(config)}
48       */
49      public CentralDogmaBeanConfigBuilder(CentralDogmaBeanConfig config) {
50          merge(config);
51      }
52  
53      /**
54       * Creates a new builder from the properties of a {@link CentralDogmaBean} annotation.
55       */
56      public CentralDogmaBeanConfigBuilder(CentralDogmaBean config) {
57          project = Strings.emptyToNull(config.project());
58          repository = Strings.emptyToNull(config.repository());
59          path = Strings.emptyToNull(config.path());
60          jsonPath = Strings.emptyToNull(config.jsonPath());
61      }
62  
63      /**
64       * Merges the properties of the specified {@link CentralDogmaBeanConfig} into this builder.
65       */
66      public CentralDogmaBeanConfigBuilder merge(CentralDogmaBeanConfig config) {
67          config.project().ifPresent(this::project);
68          config.repository().ifPresent(this::repository);
69          config.path().ifPresent(this::path);
70          config.jsonPath().ifPresent(this::jsonPath);
71          return this;
72      }
73  
74      /**
75       * Sets the Central Dogma project name.
76       */
77      public CentralDogmaBeanConfigBuilder project(String project) {
78          this.project = requireNonNull(project, "project");
79          return this;
80      }
81  
82      /**
83       * Sets the Central Dogma repository name.
84       */
85      public CentralDogmaBeanConfigBuilder repository(String repository) {
86          this.repository = requireNonNull(repository, "repository");
87          return this;
88      }
89  
90      /**
91       * Sets the path of the file in Central Dogma.
92       */
93      public CentralDogmaBeanConfigBuilder path(String path) {
94          this.path = validateFilePath(path, "path");
95          return this;
96      }
97  
98      /**
99       * Sets the JSON path expression that will be evaluated when retrieving the file.
100      */
101     public CentralDogmaBeanConfigBuilder jsonPath(String jsonPath) {
102         this.jsonPath = requireNonNull(jsonPath, "jsonPath");
103         return this;
104     }
105 
106     /**
107      * Returns a newly-created {@link CentralDogmaBeanConfig}.
108      */
109     public CentralDogmaBeanConfig build() {
110         return new CentralDogmaBeanConfig(project, repository, path, jsonPath);
111     }
112 }