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