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