View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.impl.config.domain.key.envapp;
17  
18  import de.smartics.properties.api.config.domain.key.ApplicationId;
19  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
20  import de.smartics.properties.api.config.domain.key.EnvironmentId;
21  import de.smartics.util.lang.BlankArgumentException;
22  
23  /**
24   * Helper to create instances of {@link ConfigurationKey}.
25   */
26  public final class EnvAppConfigurationKeyBuilder
27  { // NOPMD
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    // --- members --------------------------------------------------------------
33  
34    /**
35     * The builder for the environment identifier.
36     */
37    private final EnvironmentIdBuilder environmentIdBuilder =
38        new EnvironmentIdBuilder();
39  
40    /**
41     * The builder for the application identifier.
42     */
43    private final ApplicationIdBuilder applicationIdBuilder =
44        new ApplicationIdBuilder();
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    // ****************************** Inner Classes *****************************
51  
52    /**
53     * Builder of {@link EnvironmentId}s.
54     */
55    private static final class EnvironmentIdBuilder
56    {
57      // ******************************** Fields ********************************
58  
59      // --- constants ----------------------------------------------------------
60  
61      // --- members ------------------------------------------------------------
62  
63      /**
64       * The name of the environment.
65       */
66      private String name;
67  
68      /**
69       * The optional identifier of the node within the given environment.
70       */
71      private String node;
72  
73      // ***************************** Initializer ******************************
74  
75      // ***************************** Constructors *****************************
76  
77      // ***************************** Inner Classes ****************************
78  
79      // ******************************** Methods *******************************
80  
81      // --- init ---------------------------------------------------------------
82  
83      // --- get&set ------------------------------------------------------------
84  
85      /**
86       * Sets the name of the environment.
87       *
88       * @param name the name of the environment.
89       * @return a reference to the builder.
90       */
91      public EnvironmentIdBuilder withName(final String name)
92      {
93        this.name = name;
94        return this;
95      }
96  
97      /**
98       * Sets the optional identifier of the node within the given environment.
99       *
100      * @param node the optional identifier of the node within the given
101      *          environment.
102      * @return a reference to the builder.
103      */
104     public EnvironmentIdBuilder withNode(final String node)
105     {
106       this.node = node;
107       return this;
108     }
109 
110     // --- business -----------------------------------------------------------
111 
112     /**
113      * Creates an {@link EnvironmentId} instance.
114      *
115      * @return an {@link EnvironmentId} instance.
116      * @throws BlankArgumentException if the {@code name} of the environment is
117      *           blank.
118      */
119     public EnvironmentId build() throws BlankArgumentException
120     {
121       final EnvironmentId id = new EnvironmentId(name, node);
122       return id;
123     }
124 
125     // --- object basics ------------------------------------------------------
126   }
127 
128   /**
129    * Builder of {@link ApplicationId}s.
130    */
131   private static final class ApplicationIdBuilder
132   {
133     // ******************************** Fields ********************************
134 
135     // --- constants ----------------------------------------------------------
136 
137     // --- members ------------------------------------------------------------
138 
139     /**
140      * The group the application belongs to.
141      */
142     private String groupId;
143 
144     /**
145      * The name of the application.
146      */
147     private String artifactId;
148 
149     /**
150      * The version of the application.
151      */
152     private String version;
153 
154     // ***************************** Initializer ******************************
155 
156     // ***************************** Constructors *****************************
157 
158     // ***************************** Inner Classes ****************************
159 
160     // ******************************** Methods *******************************
161 
162     // --- init ---------------------------------------------------------------
163 
164     // --- get&set ------------------------------------------------------------
165 
166     /**
167      * Sets the group the application belongs to.
168      *
169      * @param groupId the group the application belongs to.
170      * @return a reference to this builder.
171      */
172     public ApplicationIdBuilder withGroupId(final String groupId)
173     {
174       this.groupId = groupId;
175       return this;
176     }
177 
178     /**
179      * Sets the name of the application.
180      *
181      * @param artifactId the name of the application.
182      * @return a reference to this builder.
183      */
184     public ApplicationIdBuilder withArtifactId(final String artifactId)
185     {
186       this.artifactId = artifactId;
187       return this;
188     }
189 
190     /**
191      * Sets the version of the application.
192      *
193      * @param version the version of the application.
194      * @return a reference to this builder.
195      */
196     public ApplicationIdBuilder withVersion(final String version)
197     {
198       this.version = version;
199       return this;
200     }
201 
202     // --- business -----------------------------------------------------------
203 
204     /**
205      * Creates an {@link ApplicationId} instance.
206      *
207      * @return an {@link ApplicationId} instance.
208      * @throws BlankArgumentException if the {@code groupId} or
209      *           {@code artifactId} of the application is blank.
210      */
211     public ApplicationId build() throws BlankArgumentException
212     {
213       final ApplicationId id = new ApplicationId(groupId, artifactId, version);
214       return id;
215     }
216 
217     // --- object basics ------------------------------------------------------
218   }
219 
220   // ********************************* Methods ********************************
221 
222   // --- init -----------------------------------------------------------------
223 
224   // --- get&set --------------------------------------------------------------
225 
226   /**
227    * Sets the name of the environment.
228    *
229    * @param name the name of the environment.
230    * @return a reference to the builder.
231    */
232   public EnvAppConfigurationKeyBuilder withEnvironmentName(final String name)
233   {
234     environmentIdBuilder.withName(name);
235     return this;
236   }
237 
238   /**
239    * Sets the optional identifier of the node within the given environment.
240    *
241    * @param node the optional identifier of the node within the given
242    *          environment.
243    * @return a reference to the builder.
244    */
245   public EnvAppConfigurationKeyBuilder withNode(final String node)
246   {
247     environmentIdBuilder.withNode(node);
248     return this;
249   }
250 
251   /**
252    * Sets the group the application belongs to.
253    *
254    * @param groupId the group the application belongs to.
255    * @return a reference to this builder.
256    */
257   public EnvAppConfigurationKeyBuilder withGroupId(final String groupId)
258   {
259     applicationIdBuilder.withGroupId(groupId);
260     return this;
261   }
262 
263   /**
264    * Sets the name of the application.
265    *
266    * @param artifactId the name of the application.
267    * @return a reference to this builder.
268    */
269   public EnvAppConfigurationKeyBuilder withArtifactId(final String artifactId)
270   {
271     applicationIdBuilder.withArtifactId(artifactId);
272     return this;
273   }
274 
275   /**
276    * Sets the version of the application.
277    *
278    * @param version the version of the application.
279    * @return a reference to this builder.
280    */
281   public EnvAppConfigurationKeyBuilder withVersion(final String version)
282   {
283     applicationIdBuilder.withVersion(version);
284     return this;
285   }
286 
287   // --- business -------------------------------------------------------------
288 
289   /**
290    * Creates an {@link ConfigurationKey} instance.
291    *
292    * @return an {@link ConfigurationKey} instance.
293    * @throws BlankArgumentException if the {@code name} of the environment, the
294    *           {@code groupId} or {@code artifactId} of the application is
295    *           blank.
296    */
297   public ConfigurationKey<?> build() throws BlankArgumentException
298   {
299     final EnvironmentId environmentId = environmentIdBuilder.build();
300     final ApplicationId applicationId = applicationIdBuilder.build();
301     final ConfigurationKey<?> key =
302         new EnvAppConfigurationKey(environmentId, applicationId);
303     return key;
304   }
305 
306   // --- object basics --------------------------------------------------------
307 
308 }