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