01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.core.impl.portlet.state;
23:
24: import org.jboss.portal.common.value.BooleanValue;
25: import org.jboss.portal.common.value.FloatValue;
26: import org.jboss.portal.common.value.IntegerValue;
27: import org.jboss.portal.common.value.StringValue;
28: import org.jboss.portal.common.value.Value;
29:
30: /**
31: * A manager handling type conversion. For now we handle only basic types : java.lang.String, integer and boolean.
32: *
33: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
34: * @version $Revision: 8786 $
35: */
36: public class ValueManager {
37:
38: /** Represents a string type. */
39: public static final int TYPE_STRING = 1;
40:
41: /** Represents a integer type. */
42: public static final int TYPE_INTEGER = 2;
43:
44: /** Represents a boolean type. */
45: public static final int TYPE_BOOLEAN = 3;
46:
47: /** Represents a boolean type. */
48: public static final int TYPE_FLOAT = 4;
49:
50: public static Value toValue(TypedStringArray tsa) {
51: switch (tsa.getType()) {
52: case TYPE_INTEGER:
53: return new IntegerValue(tsa.getStrings());
54: case TYPE_BOOLEAN:
55: return new BooleanValue(tsa.getStrings());
56: case TYPE_FLOAT:
57: return new FloatValue(tsa.getStrings());
58: default:
59: return new StringValue(tsa.getStrings());
60: }
61: }
62:
63: public static TypedStringArray toTypedStringArray(Value value) {
64: if (value.isInstanceOf(Integer.class)) {
65: return new TypedStringArray(TYPE_INTEGER, value
66: .asStringArray());
67: } else if (value.isInstanceOf(Boolean.class)) {
68: return new TypedStringArray(TYPE_BOOLEAN, value
69: .asStringArray());
70: } else if (value.isInstanceOf(Float.class)) {
71: return new TypedStringArray(TYPE_FLOAT, value
72: .asStringArray());
73: } else {
74: return new TypedStringArray(TYPE_STRING, value
75: .asStringArray());
76: }
77: }
78: }
|