View Javadoc

1   /*
2    * Copyright 2007-2011 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.exceptions.id;
17  
18  import java.util.UUID;
19  
20  import de.smartics.exceptions.core.ExceptionId;
21  
22  /**
23   * An implementation where the exception identifier is modelled as an UUID.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision$
27   */
28  public class UuidExceptionId implements ExceptionId<UUID>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     */
37    private static final long serialVersionUID = 1L;
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The implementing identifier.
43     *
44     * @serial
45     */
46    private final UUID uuid;
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Constructor to create an instance with a random UUID.
54     */
55    public UuidExceptionId()
56    {
57      this(UUID.randomUUID());
58    }
59  
60    /**
61     * Default constructor that creates an instance with the given UUID.
62     *
63     * @param uuid the unique identifier to use for an exception.
64     */
65    public UuidExceptionId(final UUID uuid)
66    {
67      this.uuid = uuid;
68    }
69  
70    // ****************************** Inner Classes *****************************
71  
72    // ********************************* Methods ********************************
73  
74    // --- init -----------------------------------------------------------------
75  
76    // --- get&set --------------------------------------------------------------
77  
78    /**
79     * {@inheritDoc}
80     *
81     * @see de.smartics.exceptions.core.ExceptionId#getId()
82     */
83    public UUID getId()
84    {
85      return uuid;
86    }
87  
88    // --- business -------------------------------------------------------------
89  
90    // --- object basics --------------------------------------------------------
91  
92    /**
93     * Returns <code>true</code> if the given object is semantically equal to the
94     * given object, <code>false</code> otherwise.
95     * <p>
96     * Two instances o this class are equal if they have the same identifier.
97     *
98     * @param object the instance to compare to.
99     * @return <code>true</code> if the given object is semantically equal to the
100    *         given object, <code>false</code> otherwise.
101    */
102   public boolean equals(final Object object)
103   {
104     if (this == object)
105     {
106       return true;
107     }
108     else if (object == null || getClass() != object.getClass())
109     {
110       return false;
111     }
112 
113     final UuidExceptionId other = (UuidExceptionId) object;
114 
115     return uuid.equals(other.uuid);
116   }
117 
118   /**
119    * Returns the hashcode of the object.
120    * <p>
121    * It is the hashcode of the UUID instance.
122    *
123    * @return the hash code.
124    */
125   public int hashCode()
126   {
127     return uuid.hashCode();
128   }
129 
130   /**
131    * Returns the UUID as a string.
132    *
133    * @return the UUID as a string.
134    * @see java.lang.Object#toString()
135    */
136   @Override
137   public String toString()
138   {
139     return uuid.toString();
140   }
141 }