KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > value > IntegerValues


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.value;
10
11
12 /**
13  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
14  * @version $Revision: 1.1.1.1 $
15  */

16 public class IntegerValues extends AbstractValues
17 {
18
19    /** The Integer array. */
20    private Integer JavaDoc[] values;
21
22    /** The cached int array. */
23    private int[] _values;
24
25    public IntegerValues()
26    {
27       values = new Integer JavaDoc[1];
28    }
29
30    public IntegerValues(String JavaDoc[] values) throws IllegalArgumentException JavaDoc
31    {
32       if (values == null)
33       {
34          throw new IllegalArgumentException JavaDoc();
35       }
36       try
37       {
38          this.values = new Integer JavaDoc[values.length];
39          for (int i = 0;i < values.length;i++)
40          {
41             String JavaDoc value = values[i];
42             if (value != null)
43             {
44                this.values[i] = new Integer JavaDoc(value);
45             }
46          }
47       }
48       catch (NumberFormatException JavaDoc e)
49       {
50          throw new IllegalArgumentException JavaDoc();
51       }
52    }
53
54    public IntegerValues(Integer JavaDoc integer)
55    {
56       if (integer == null)
57       {
58          throw new IllegalArgumentException JavaDoc();
59       }
60       else
61       {
62          values = new Integer JavaDoc[]{integer};
63       }
64    }
65
66    public IntegerValues(Integer JavaDoc[] values)
67    {
68       if (values == null)
69       {
70          throw new IllegalArgumentException JavaDoc();
71       }
72       this.values = values;
73    }
74
75    public IntegerValues(int _value)
76    {
77       this(new Integer JavaDoc(_value));
78    }
79
80    public IntegerValues(int[] _values)
81    {
82       if (_values == null)
83       {
84          throw new IllegalArgumentException JavaDoc();
85       }
86       values = new Integer JavaDoc[_values.length];
87       for (int i = 0;i < _values.length;i++)
88       {
89          values[i] = new Integer JavaDoc(_values[i]);
90       }
91    }
92
93    public String JavaDoc[] asStringArray()
94    {
95       try
96       {
97          return Helper.toStringArray(values, Helper.INTEGER_CONVERTER);
98       }
99       catch (ConversionException impossible)
100       {
101          throw new Error JavaDoc(impossible);
102       }
103    }
104
105    public int[] asIntArray() throws NullConversionException, FormatConversionException
106    {
107       if (_values == null)
108       {
109          _values = new int[values.length];
110          for (int i = 0;i < this.values.length;i++)
111          {
112             Integer JavaDoc integer = this.values[i];
113             if (integer == null)
114             {
115                throw new NullConversionException();
116             }
117             _values[i] = this.values[i].intValue();
118          }
119       }
120       return _values;
121    }
122
123    public String JavaDoc asString()
124    {
125       if (isNull())
126       {
127          return null;
128       }
129       return values[0].toString();
130    }
131
132    public int asInt() throws NullConversionException, FormatConversionException
133    {
134       if (isNull())
135       {
136          throw new NullConversionException();
137       }
138       return values[0].intValue();
139    }
140
141    protected Object JavaDoc[] getObjects()
142    {
143       return values;
144    }
145 }
146
Popular Tags