-
Notifications
You must be signed in to change notification settings - Fork 33
/
ObjectParameter.java
203 lines (183 loc) · 6.31 KB
/
ObjectParameter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Get more info at : www.jrebirth.org .
* Copyright JRebirth.org © 2011-2013
* Contact : sebastien.bordes@jrebirth.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jrebirth.af.core.resource.parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jrebirth.af.core.exception.CoreRuntimeException;
import org.jrebirth.af.core.resource.AbstractBaseParams;
import org.jrebirth.af.core.resource.ParameterEntry;
import org.jrebirth.af.core.resource.ResourceParams;
/**
* The interface <strong>ObjectParameter</strong>.
*
* @author Sébastien Bordes
*
* @param <O> the parameter type
*/
public class ObjectParameter<O extends Object> extends AbstractBaseParams implements ParameterParams {
/** The parameter name. */
private final String parameterName;
/** The parameter object. */
private final O object;
// /**
// * Default Constructor.
// *
// * @param object the parameter object
// */
// public ObjectParameter(final O object) {
// super();
// this.object = object;
//
// // Object must be not null
// if (object == null) {
// throw new CoreRuntimeException("ObjectParameter must have a non null object for unamed parameter");
// }
// }
/**
* Default Constructor.
*
* @param parameterName the name of the parameter
* @param object the parameter object
*/
public ObjectParameter(final String parameterName, final O object) {
super();
this.object = object;
this.parameterName = parameterName;
// Object must be not null
if (object == null) {
throw new CoreRuntimeException("ObjectParameter must have a non null object for parameter : " + this.parameterName);
}
}
/**
* Return the parameter name.
*
* @return Returns the parameter name.
*/
public String name() {
return this.parameterName;
}
/**
* Return the parameter object value.
*
* @return Returns the object.
*/
public O object() {
return this.object;
}
/**
* Parse the serialized object.
*
* @param parameterEntry the parameter entry to convert that wrap the serialized string
*
* @return the real object
*/
public Object parseObject(final ParameterEntry parameterEntry) {
Object res = null;
if (this.object instanceof ResourceParams) {
// Setup the default object
((ResourceParams) this.object).parse(parameterEntry.getSerializedString().split(PARAMETER_SEPARATOR));
// return the new value
res = this.object;
} else if (this.object instanceof Class<?>) {
res = parseClassParameter(parameterEntry.getSerializedString());
} else if (this.object instanceof List<?>) {
res = parseListParameter(parameterEntry.getSerializedString());
} else {
res = parsePrimitive(parameterEntry.getSerializedString());
}
// Store the parsed object directly into the entry instance
// for later access
parameterEntry.setObject(res);
return res;
}
/**
* Parse a class definition by calling to call Class.forName.
*
* @param serializedObject the full class name
*
* @return the class object
*/
private Object parseClassParameter(final String serializedObject) {
Object res = null;
try {
res = Class.forName(serializedObject);
} catch (final ClassNotFoundException e) {
throw new CoreRuntimeException("Impossible to load class " + serializedObject, e);
}
return res;
}
/**
* Parse a generic list.
*
* @param serializedObject the concatenated list
*
* @return the list object
*/
private Object parseListParameter(final String serializedObject) {
List<Object> res = new ArrayList<>();
for(String item : serializedObject.split(";")){
res.add(item);
}
return res;
}
/**
* Parse primitive serialized object.
*
* @param serializedObject the serialized string to parse
*
* @return a new fresh instance of the object
*/
private Object parsePrimitive(final String serializedObject) {
Object res = null;
if (this.object instanceof Boolean) {
res = Boolean.valueOf(serializedObject);
} else if (this.object instanceof String) {
res = serializedObject;
} else if (this.object instanceof Character) {
res = Character.valueOf(serializedObject.charAt(0));
} else if (this.object instanceof Byte) {
res = Byte.parseByte(serializedObject);
} else if (this.object instanceof Short) {
res = Short.parseShort(serializedObject);
} else if (this.object instanceof Integer) {
res = Integer.parseInt(serializedObject);
} else if (this.object instanceof Long) {
res = Long.parseLong(serializedObject);
} else if (this.object instanceof Float) {
res = Float.parseFloat(serializedObject);
} else if (this.object instanceof Double) {
res = Double.parseDouble(serializedObject);
}
return res;
}
/**
* {@inheritDoc}
*/
@Override
public void parse(final String[] string) {
// Nothing to do, method added to be compliant with API
}
/**
* {@inheritDoc}
*/
@Override
protected List<? extends Object> getFieldValues() {
return Arrays.asList(name());
}
}