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.api.config.domain.key;
17  
18  import java.io.Serializable;
19  
20  import static org.apache.commons.lang.StringUtils.defaultIfBlank;
21  import org.apache.commons.lang.ObjectUtils;
22  
23  import de.smartics.util.lang.Arg;
24  
25  /**
26   * Defines an environment an application is deployed to.
27   */
28  public final class EnvironmentId implements Serializable,
29      Comparable<EnvironmentId>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     */
38    private static final long serialVersionUID = 1L;
39  
40    /**
41     * Defines the application for any environment.
42     */
43    public static final EnvironmentId ANY_ENV = new EnvironmentId(null, null);
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The name of the environment.
49     *
50     * @serial
51     */
52    private final String name;
53  
54    /**
55     * The optional identifier of the node within the given environment.
56     *
57     * @serial
58     */
59    private final String node;
60  
61    // ****************************** Initializer *******************************
62  
63    // ****************************** Constructors ******************************
64  
65    /**
66     * Convenience constructor without a node.
67     *
68     * @param name the name of the environment.
69     * @throws IllegalArgumentException if {@code name} is the empty string or
70     *           contains only white spaces.
71     */
72    public EnvironmentId(final String name) throws IllegalArgumentException
73    {
74      this(name, null);
75    }
76  
77    /**
78     * Default constructor.
79     *
80     * @param name the name of the environment.
81     * @param node the optional identifier of the node within the given
82     *          environment.
83     * @throws IllegalArgumentException if {@code name} or {@code node} is the
84     *           empty string or contains only white spaces.
85     */
86    public EnvironmentId(final String name, final String node)
87      throws IllegalArgumentException
88    {
89      this.name = Arg.checkNotBlankExceptNull("name", name);
90      this.node = Arg.checkNotBlankExceptNull("node", node);
91    }
92  
93    // ****************************** Inner Classes *****************************
94  
95    // ********************************* Methods ********************************
96  
97    // --- init -----------------------------------------------------------------
98  
99    // --- get&set --------------------------------------------------------------
100 
101   /**
102    * Returns the name of the environment.
103    *
104    * @return the name of the environment.
105    */
106   public String getName()
107   {
108     return name;
109   }
110 
111   /**
112    * Returns the optional identifier of the node within the given environment.
113    *
114    * @return the optional identifier of the node within the given environment.
115    */
116   public String getNode()
117   {
118     return node;
119   }
120 
121   // --- business -------------------------------------------------------------
122 
123   // --- object basics --------------------------------------------------------
124 
125   /**
126    * Returns the hash code of the object.
127    *
128    * @return the hash code.
129    */
130   @Override
131   public int hashCode()
132   {
133     int result = 17;
134     result = 37 * result + ObjectUtils.hashCode(name);
135     result = 37 * result + ObjectUtils.hashCode(node);
136     return result;
137   }
138 
139   /**
140    * Returns <code>true</code> if the given object is semantically equal to the
141    * given object, <code>false</code> otherwise.
142    *
143    * @param object the instance to compare to.
144    * @return <code>true</code> if the given object is semantically equal to the
145    *         given object, <code>false</code> otherwise.
146    */
147   @Override
148   public boolean equals(final Object object)
149   {
150     if (this == object)
151     {
152       return true;
153     }
154     else if (object == null || getClass() != object.getClass())
155     {
156       return false;
157     }
158 
159     final EnvironmentId other = (EnvironmentId) object;
160 
161     return (ObjectUtils.equals(name, other.name) && ObjectUtils.equals(node,
162         other.node));
163   }
164 
165   /**
166    * {@inheritDoc}
167    *
168    * @see java.lang.Comparable#compareTo(java.lang.Object)
169    */
170   @Override
171   public int compareTo(final EnvironmentId o)
172   {
173     int result = ObjectUtils.compare(name, o.name);
174     if (result == 0)
175     {
176       result = ObjectUtils.compare(node, o.node);
177     }
178 
179     return result;
180   }
181 
182   /**
183    * Returns the string representation of the object as a path. All elements are
184    * separated by a slash.
185    *
186    * @return the string representation of the object.
187    */
188   public String toPath()
189   {
190     return ObjectUtils.toString(name, "") + '/'
191            + ObjectUtils.toString(node, "");
192   }
193 
194   /**
195    * Returns the string representation of the object.
196    *
197    * @return the string representation of the object.
198    */
199   @Override
200   public String toString()
201   {
202     return ObjectUtils.toString(name, "") + ':'
203            + ObjectUtils.toString(node, "");
204   }
205 
206   /**
207    * Returns an {@link EnvironmentId} for the given String or throws an
208    * {@link IllegalArgumentException} if the String is not valid.
209    *
210    * @param environmentString a string describing an environment.
211    * @return an envrironmentId for the given String.
212    * @throws IllegalArgumentException if an invalid environmentString has been
213    *           passed.
214    */
215   public static EnvironmentId valueOf(final String environmentString)
216     throws IllegalArgumentException
217   {
218     if (environmentString == null)
219     {
220       return new EnvironmentId(null);
221     }
222     final String[] environmentParts = environmentString.split(":", -1);
223     final int length = environmentParts.length;
224     if (length == 1)
225     {
226       return new EnvironmentId(environmentParts[0]);
227     }
228     else if (length == 2)
229     {
230       return new EnvironmentId(defaultIfBlank(environmentParts[0], null),
231           defaultIfBlank(environmentParts[1], null));
232     }
233     else
234     {
235       throw new IllegalArgumentException(
236           String
237               .format(
238                   "Could not create an instance out of the key %s. It has too many delimiters!",
239                   environmentString));
240     }
241   }
242 }