KickJava   Java API By Example, From Geeks To Geeks.

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


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