1 /*
2 Copyright (C) 2007 Grid Systems, S.A.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 package com.gridsystems.nextgrid.api.pom.ref;
19
20 /**
21 * Scalar References are references to workflow local values (e.g. simple
22 * parameters).
23 * <p>
24 * The value is wrapped in an XML document with the following layout:
25 *
26 * <pre>
27 * <value>
28 * [value string representation]
29 * </value>
30 * </pre>
31 *
32 * @param <E> The scalar type
33 * @author Rodrigo Ruiz
34 */
35 public class ScalarReference<E> extends ReferenceSupport<E> {
36
37 /**
38 * <code>serialVersionUID</code> attribute.
39 */
40 private static final long serialVersionUID = -913366860389629686L;
41
42 /**
43 * Value type.
44 */
45 private final Class<E> type;
46
47 /**
48 * The wrapped value.
49 */
50 private E value;
51
52 /**
53 * Creates a new instance.
54 *
55 * @param type A class instance for returning value types
56 */
57 public ScalarReference(Class<E> type) {
58 this.type = type;
59
60 }
61
62 /**
63 * Creates a new instance.
64 *
65 * @param type A class instance for returning value types
66 * @param value An object instance
67 */
68 public ScalarReference(Class<E> type, E value) {
69 this(type);
70 setValue(value);
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @SuppressWarnings("unchecked")
77 public final void setValue(Object value) {
78 LOG.debug("Setting value [" + this + " ::= " + value + "]");
79 this.value = (E)castTo(getValueType(), value);
80 setAvailable(value != null);
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 public final E getValue() {
87 return this.value;
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 public final Class<E> getValueType() {
94 return this.type;
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 public boolean canCastTo(Class<?> type) {
101 return type != null && (type.isAssignableFrom(String.class)
102 || type.isAssignableFrom(this.type));
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public boolean canCastFrom(Class<?> type) {
109 return type != null && (this.type.isAssignableFrom(String.class)
110 || this.type.isAssignableFrom(type));
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public Object castTo(Class<?> type) {
117 return castTo(type, this.value);
118 }
119
120 /**
121 * Performs a cast of the given value to the specified type.
122 *
123 * @param type The target type
124 * @param value The source value
125 * @return The target value
126 */
127 private Object castTo(Class<?> type, Object value) {
128 if (type.isAssignableFrom(String.class)) {
129 return (value == null) ? null : value.toString();
130 } else if (type.isAssignableFrom(this.type)) {
131 return value;
132 } else {
133 throw new ClassCastException("Cannot cast to " + type);
134 }
135 }
136
137 }