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