• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2009 Mike Cumings
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 
17 package com.kenai.jbosh;
18 
19 /**
20  * Abstract base class for attribute implementations based on {@code Integer}
21  * types.  Additional support for parsing of integer values from their
22  * {@code String} representations as well as callback handling of value
23  * validity checks are also provided.
24  */
25 abstract class AbstractIntegerAttr extends AbstractAttr<Integer> {
26 
27     /**
28      * Creates a new attribute object.
29      *
30      * @param val attribute value
31      * @throws BOSHException on parse or validation failure
32      */
AbstractIntegerAttr(final int val)33     protected AbstractIntegerAttr(final int val) throws BOSHException {
34         super(Integer.valueOf(val));
35     }
36 
37     /**
38      * Creates a new attribute object.
39      *
40      * @param val attribute value in string form
41      * @throws BOSHException on parse or validation failure
42      */
AbstractIntegerAttr(final String val)43     protected AbstractIntegerAttr(final String val) throws BOSHException {
44         super(parseInt(val));
45     }
46 
47     /**
48      * Utility method intended to be called by concrete implementation
49      * classes from within the {@code check()} method when the concrete
50      * class needs to ensure that the integer value does not drop below
51      * the specified minimum value.
52      *
53      * @param minVal minimum value to allow
54      * @throws BOSHException if the integer value is below the specific
55      *  minimum
56      */
checkMinValue(int minVal)57     protected final void checkMinValue(int minVal) throws BOSHException {
58         int intVal = getValue();
59         if (intVal < minVal) {
60             throw(new BOSHException(
61                     "Illegal attribute value '" + intVal + "' provided.  "
62                     + "Must be >= " + minVal));
63         }
64     }
65 
66     /**
67      * Utility method to parse a {@code String} into an {@code Integer},
68      * converting any possible {@code NumberFormatException} thrown into
69      * a {@code BOSHException}.
70      *
71      * @param str string to parse
72      * @return integer value
73      * @throws BOSHException on {@code NumberFormatException}
74      */
parseInt(final String str)75     private static int parseInt(final String str) throws BOSHException {
76         try {
77             return Integer.parseInt(str);
78         } catch (NumberFormatException nfx) {
79             throw(new BOSHException(
80                     "Could not parse an integer from the value provided: "
81                     + str,
82                     nfx));
83         }
84     }
85 
86     /**
87      * Returns the native {@code int} value of the underlying {@code Integer}.
88      * Will throw {@code NullPointerException} if the underlying
89      * integer was {@code null}.
90      *
91      * @return native {@code int} value
92      */
intValue()93     public int intValue() {
94         return getValue().intValue();
95     }
96 
97 }
98