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 org.apache.commons.lang.ObjectUtils;
21  
22  import de.smartics.util.lang.Arguments;
23  
24  /**
25   * Defines an environment an application is deployed to.
26   */
27  public final class EnvironmentId implements Serializable,
28      Comparable<EnvironmentId>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     */
37    private static final long serialVersionUID = 1L;
38  
39    /**
40     * Defines the application for any environment.
41     */
42    public static final EnvironmentId ANY_ENV = new EnvironmentId(null, null);
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The name of the environment.
48     *
49     * @serial
50     */
51    private final String name;
52  
53    /**
54     * The optional identifier of the node within the given environment.
55     *
56     * @serial
57     */
58    private final String node;
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Convenience constructor without a node.
66     *
67     * @param name the name of the environment.
68     * @throws IllegalArgumentException if {@code name} is the empty string or
69     *           contains only white spaces.
70     */
71    public EnvironmentId(final String name) throws IllegalArgumentException
72    {
73      this(name, null);
74    }
75  
76    /**
77     * Default constructor.
78     *
79     * @param name the name of the environment.
80     * @param node the optional identifier of the node within the given
81     *          environment.
82     * @throws IllegalArgumentException if {@code name} or {@code node} is the
83     *           empty string or contains only white spaces.
84     */
85    public EnvironmentId(final String name, final String node)
86      throws IllegalArgumentException
87    {
88      Arguments.checkNotBlankExceptNull("name", name);
89      Arguments.checkNotBlankExceptNull("node", node);
90  
91      this.name = name;
92      this.node = node;
93    }
94  
95    // ****************************** Inner Classes *****************************
96  
97    // ********************************* Methods ********************************
98  
99    // --- init -----------------------------------------------------------------
100 
101   // --- get&set --------------------------------------------------------------
102 
103   /**
104    * Returns the name of the environment.
105    *
106    * @return the name of the environment.
107    */
108   public String getName()
109   {
110     return name;
111   }
112 
113   /**
114    * Returns the optional identifier of the node within the given environment.
115    *
116    * @return the optional identifier of the node within the given environment.
117    */
118   public String getNode()
119   {
120     return node;
121   }
122 
123   // --- business -------------------------------------------------------------
124 
125   // --- object basics --------------------------------------------------------
126 
127   /**
128    * Returns the hash code of the object.
129    *
130    * @return the hash code.
131    */
132   @Override
133   public int hashCode()
134   {
135     int result = 17;
136     result = 37 * result + ObjectUtils.hashCode(name);
137     result = 37 * result + ObjectUtils.hashCode(node);
138     return result;
139   }
140 
141   /**
142    * Returns <code>true</code> if the given object is semantically equal to the
143    * given object, <code>false</code> otherwise.
144    *
145    * @param object the instance to compare to.
146    * @return <code>true</code> if the given object is semantically equal to the
147    *         given object, <code>false</code> otherwise.
148    */
149   @Override
150   public boolean equals(final Object object)
151   {
152     if (this == object)
153     {
154       return true;
155     }
156     else if (object == null || getClass() != object.getClass())
157     {
158       return false;
159     }
160 
161     final EnvironmentId other = (EnvironmentId) object;
162 
163     return (ObjectUtils.equals(name, other.name) && ObjectUtils.equals(node,
164         other.node));
165   }
166 
167   /**
168    * {@inheritDoc}
169    *
170    * @see java.lang.Comparable#compareTo(java.lang.Object)
171    */
172   @Override
173   public int compareTo(final EnvironmentId o)
174   {
175     int result = ObjectUtils.compare(name, o.name);
176     if (result == 0)
177     {
178       result = ObjectUtils.compare(node, o.node);
179     }
180 
181     return result;
182   }
183 
184   /**
185    * Returns the string representation of the object.
186    *
187    * @return the string representation of the object.
188    */
189   @Override
190   public String toString()
191   {
192     return ObjectUtils.toString(name, "") + (node != null ? "/" : "")
193            + ObjectUtils.toString(node, "");
194   }
195 }