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.rtaware;
17  
18  import java.io.Serializable;
19  
20  import javax.annotation.CheckForNull;
21  
22  import org.apache.commons.lang.ObjectUtils;
23  
24  import de.smartics.util.lang.Arg;
25  
26  /**
27   * Identifies a tenant.
28   */
29  public final class TenantId implements Serializable, Comparable<TenantId>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     * <p>
38     * The value of this constant is {@value}.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42  
43    /**
44     * Defines the ID for any tenant.
45     */
46    public static final TenantId ANY_TENANT = new TenantId(null);
47  
48    // --- members --------------------------------------------------------------
49  
50    /**
51     * The name of the tenant.
52     *
53     * @serial
54     */
55    private final String name;
56  
57    // ****************************** Initializer *******************************
58  
59    // ****************************** Constructors ******************************
60  
61    /**
62     * Default constructor.
63     *
64     * @param name the name of the tenant. Must not be blank, but may be
65     *          <code>null</code>.
66     * @throws IllegalArgumentException if {@code name} is blank.
67     */
68    public TenantId(final String name) throws IllegalArgumentException
69    {
70      this.name = Arg.checkNotBlankExceptNull("name", name);
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    /**
82     * Returns the name of the tenant.
83     *
84     * @return the name of the tenant.
85     */
86    @CheckForNull
87    public String getName()
88    {
89      return name;
90    }
91  
92    // --- business -------------------------------------------------------------
93  
94    // --- object basics --------------------------------------------------------
95  
96    @Override
97    public int compareTo(final TenantId o)
98    {
99      final int compare = ObjectUtils.compare(name, o.name);
100     return compare;
101   }
102 
103   /**
104    * Returns the hash code of the object.
105    *
106    * @return the hash code.
107    */
108   @Override
109   public int hashCode()
110   {
111     return ObjectUtils.hashCode(name);
112   }
113 
114   /**
115    * Returns <code>true</code> if the given object is semantically equal to the
116    * given object, <code>false</code> otherwise.
117    *
118    * @param object the instance to compare to.
119    * @return <code>true</code> if the given object is semantically equal to the
120    *         given object, <code>false</code> otherwise.
121    */
122   @Override
123   public boolean equals(final Object object)
124   {
125     if (this == object)
126     {
127       return true;
128     }
129     else if (object == null || getClass() != object.getClass())
130     {
131       return false;
132     }
133 
134     final TenantId other = (TenantId) object;
135 
136     return ObjectUtils.equals(name, other.name);
137   }
138 
139   /**
140    * Returns the string representation of the object.
141    *
142    * @return the string representation of the object.
143    */
144   @Override
145   public String toString()
146   {
147     return ObjectUtils.toString(name, "");
148   }
149 
150   /**
151    * Returns an {@link TenantId} for the given tenantString.
152    *
153    * @param tenantString a string describing an tenant.
154    * @return a tenantId for the given string.
155    */
156   public static TenantId valueOf(final String tenantString)
157   {
158     return new TenantId(tenantString);
159   }
160 }