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.report.message;
17  
18  import java.io.Serializable;
19  
20  import de.smartics.exceptions.i18n.message.MessageType;
21  
22  /**
23   * Identifies a place holder description by its index an message type.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public final class PlaceHolderDescId implements Serializable,
29      Comparable<PlaceHolderDescId>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     */
38    private static final long serialVersionUID = 1L;
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The index of the place holder within a message of the given message type.
44     *
45     * @serial
46     */
47    private final int placeHolderIndex;
48  
49    /**
50     * The type of the indexed message. If <code>null</code>, the place holder is
51     * valid for any message that has no specific description (aka default
52     * description).
53     *
54     * @serial
55     */
56    private final MessageType messageType;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Convenience constructor for a place holder for any message type.
64     *
65     * @param placeHolderIndex the index of the place holder within any message.
66     * @throws NullPointerException if <code>placeHolderIndex</code>is
67     *           <code>null</code>.
68     */
69    public PlaceHolderDescId(final int placeHolderIndex)
70      throws NullPointerException
71    {
72      this(placeHolderIndex, null);
73    }
74  
75    /**
76     * Default constructor.
77     *
78     * @param placeHolderIndex the index of the place holder within a message of
79     *          the given message type.
80     * @param messageType the type of the indexed message.
81     */
82    public PlaceHolderDescId(final int placeHolderIndex,
83        final MessageType messageType)
84    {
85      this.placeHolderIndex = placeHolderIndex;
86      this.messageType = messageType;
87    }
88  
89    // ****************************** Inner Classes *****************************
90  
91    // ********************************* Methods ********************************
92  
93    // --- init -----------------------------------------------------------------
94  
95    // --- get&set --------------------------------------------------------------
96  
97    /**
98     * Returns the index of the place holder within a message of the given message
99     * type.
100    *
101    * @return the index of the place holder within a message of the given message
102    *         type.
103    */
104   public int getPlaceHolderIndex()
105   {
106     return placeHolderIndex;
107   }
108 
109   /**
110    * Returns the type of the indexed message.
111    *
112    * @return the type of the indexed message.
113    */
114   public MessageType getMessageType()
115   {
116     return messageType;
117   }
118 
119   // --- business -------------------------------------------------------------
120 
121   // --- object basics --------------------------------------------------------
122 
123   /**
124    * Returns the hash code of the object.
125    *
126    * @return the hash code.
127    */
128   @Override
129   public int hashCode()
130   {
131     return placeHolderIndex;
132   }
133 
134   /**
135    * Returns <code>true</code> if the given object is semantically equal to the
136    * given object, <code>false</code> otherwise.
137    *
138    * @param object the instance to compare to.
139    * @return <code>true</code> if the given object is semantically equal to the
140    *         given object, <code>false</code> otherwise.
141    */
142   @Override
143   public boolean equals(final Object object)
144   {
145     if (this == object)
146     {
147       return true;
148     }
149     else if (object == null || getClass() != object.getClass())
150     {
151       return false;
152     }
153 
154     final PlaceHolderDescId other = (PlaceHolderDescId) object;
155 
156     return (placeHolderIndex == other.placeHolderIndex && messageType
157         .equals(other.messageType));
158   }
159 
160   /**
161    * Returns the string representation of the object.
162    *
163    * @return the string representation of the object.
164    */
165   @Override
166   public String toString()
167   {
168     return String.valueOf(placeHolderIndex) + '/' + messageType;
169   }
170 
171   /**
172    * {@inheritDoc}
173    *
174    * @see java.lang.Comparable#compareTo(java.lang.Object)
175    */
176   public int compareTo(final PlaceHolderDescId o)
177   {
178     return o.placeHolderIndex - placeHolderIndex;
179   }
180 }