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.code;
17  
18  /**
19   * This {@link de.smartics.exceptions.core.Code} implementation models the
20   * exception code as a number. It supports a component identifier as an
21   * arbitrary string and provides a facility to specify the number as a major an
22   * minor number that will be added.
23   *
24   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
25   * @version $Revision:591 $
26   */
27  public class NumberCodeInfo implements NumberCode
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The class version identifier.
35     * <p>
36     * The value of this constant is {@value}.
37     */
38    private static final long serialVersionUID = 1L;
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The identifier of the component this code belongs to.
44     *
45     * @serial
46     */
47    protected final String componentId;
48  
49    /**
50     * The major number of the code defines a group of codes. This group is
51     * associated with a certain type of problem within the component.
52     * <p>
53     * For example HTTP knows the major numbers 100, 200, 300, 400, etc.
54     *
55     * @serial
56     */
57    protected final Integer majorNumber;
58  
59    /**
60     * The minor number gives additional information about the problem. It
61     * specifies exactly the type of problem. If this value is <code>null</code>,
62     * there is no minor number specified.
63     * <p>
64     * For example HTTP knows the minor number 4 for page not found. This will add
65     * with the major number to 404.
66     */
67    protected final Integer minorNumber;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    /**
74     * Convenience constructor.
75     *
76     * @param majorNumber the major number of the code defines a group of codes.
77     */
78    public NumberCodeInfo(final Integer majorNumber)
79    {
80      this(null, majorNumber);
81    }
82  
83    /**
84     * Convenience constructor.
85     *
86     * @param componentId the identifier of the component this code belongs to.
87     * @param majorNumber the major number of the code defines a group of codes.
88     */
89    public NumberCodeInfo(final String componentId, final Integer majorNumber)
90    {
91      this(componentId, majorNumber, null);
92    }
93  
94    /**
95     * Default constructor.
96     *
97     * @param componentId the identifier of the component this code belongs to.
98     * @param majorNumber the major number of the code defines a group of codes.
99     * @param minorNumber the minor number gives additional information about the
100    *          problem.
101    * @throws NullPointerException if the major number is <code>null</code>.
102    */
103   public NumberCodeInfo(final String componentId, final Integer majorNumber,
104       final Integer minorNumber) throws NullPointerException
105   {
106     if (majorNumber == null)
107     {
108       throw new NullPointerException("Major number must not be 'null'.");
109     }
110     this.componentId = componentId;
111     this.majorNumber = majorNumber;
112     this.minorNumber = minorNumber;
113   }
114 
115   // ****************************** Inner Classes *****************************
116 
117   // ********************************* Methods ********************************
118 
119   // --- init -----------------------------------------------------------------
120 
121   // --- get&set --------------------------------------------------------------
122 
123   /**
124    * Returns the identifier of the component this code belongs to.
125    *
126    * @return the identifier of the component this code belongs to.
127    */
128   public String getComponentId()
129   {
130     return componentId;
131   }
132 
133   /**
134    * {@inheritDoc}
135    *
136    * @see de.smartics.exceptions.code.NumberCode#getMajorNumber()
137    */
138   public Integer getMajorNumber()
139   {
140     return majorNumber;
141   }
142 
143   /**
144    * {@inheritDoc}
145    *
146    * @see de.smartics.exceptions.code.NumberCode#getMinorNumber()
147    */
148   public Integer getMinorNumber()
149   {
150     return minorNumber;
151   }
152 
153   /**
154    * {@inheritDoc}
155    *
156    * @see de.smartics.exceptions.core.Code#getDisplayId()
157    */
158   public String getDisplayId()
159   {
160     final String code = getCode();
161 
162     final StringBuilder buffer = new StringBuilder();
163 
164     if (componentId != null)
165     {
166       buffer.append(componentId).append('-');
167     }
168     buffer.append(code);
169 
170     return buffer.toString();
171   }
172 
173   // --- business -------------------------------------------------------------
174 
175   /**
176    * {@inheritDoc}
177    */
178   // @Override
179   public String getCode()
180   {
181     int number = majorNumber.intValue();
182     if (minorNumber != null)
183     {
184       number += minorNumber.intValue();
185     }
186 
187     return String.valueOf(number);
188   }
189 
190   // --- object basics --------------------------------------------------------
191 
192   /**
193    * Returns the string representation of the object.
194    *
195    * @return the string representation of the object.
196    */
197   @Override
198   public String toString()
199   {
200     return getDisplayId();
201   }
202 
203 }