View Javadoc

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