2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.serviceprovider.model;
19 import java.util.HashSet;
20 import java.util.Iterator;
22 import java.util.Vector;
24 import oic.simulator.serviceprovider.utils.Constants;
25 import oic.simulator.serviceprovider.utils.Utility;
27 import org.oic.simulator.AttributeProperty;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.AttributeValue.ValueType;
30 import org.oic.simulator.BooleanProperty;
31 import org.oic.simulator.DoubleProperty;
32 import org.oic.simulator.IntegerProperty;
33 import org.oic.simulator.SimulatorResourceAttribute;
34 import org.oic.simulator.StringProperty;
36 public class AttributeHelper {
37 private String attributeName;
38 private String attributeType;
41 private Set<String> allowedValues;
42 private String attributeDflValue;
44 public enum ValidValuesType {
45 RANGE, VALUESET, UNKNOWN
48 private ValidValuesType validValuesType;
50 public AttributeHelper() {
51 allowedValues = new HashSet<String>();
52 setValidValuesType(ValidValuesType.UNKNOWN);
56 public AttributeHelper clone() {
57 AttributeHelper att = new AttributeHelper();
58 att.setAttributeName(attributeName);
59 att.setAttributeType(attributeType);
62 if (null != allowedValues) {
63 Set<String> values = new HashSet<String>();
64 values.addAll(allowedValues);
65 att.setAllowedValues(values);
67 att.setAttributeDflValue(attributeDflValue);
68 att.setValidValuesType(validValuesType);
72 public String toString() {
73 return attributeName + "," + attributeType + "," + attributeDflValue
74 + "," + validValuesType + ",(" + min + max + "),("
75 + allowedValues + ")";
78 public String getAttributeName() {
82 public void setAttributeName(String attributeName) {
83 this.attributeName = attributeName;
86 public String getAttributeType() {
90 public void setAttributeType(String attributeType) {
91 this.attributeType = attributeType;
94 public String getMin() {
98 public void setMin(String min) {
102 public String getMax() {
106 public void setMax(String max) {
110 public Set<String> getAllowedValues() {
111 return allowedValues;
114 public void setAllowedValues(Set<String> allowedValues) {
115 this.allowedValues = allowedValues;
118 public void addValueToAllowedValues(String value) {
119 if (null != value && value.trim().length() > 0) {
120 if (null == allowedValues) {
121 allowedValues = new HashSet<String>();
123 allowedValues.add(value);
127 public void removeValueFromAllowedValues(String value) {
128 if (null != allowedValues && null != value && value.trim().length() > 0) {
129 allowedValues.remove(value);
133 public void removeAllCustomValues() {
134 allowedValues.clear();
137 public String getAttributeDflValue() {
138 return attributeDflValue;
141 public void setAttributeDflValue(String attributeDflValue) {
142 this.attributeDflValue = attributeDflValue;
145 public ValidValuesType getValidValuesType() {
146 return validValuesType;
149 public void setValidValuesType(ValidValuesType validValuesType) {
150 this.validValuesType = validValuesType;
153 public boolean isValueValid(String value, String type) {
154 if (null == value || value.trim().length() < 1 || null == type
155 || type.trim().length() < 1)
157 if (type.equalsIgnoreCase(Constants.INT)) {
158 return isIntValue(value);
159 } else if (type.equalsIgnoreCase(Constants.DOUBLE)) {
160 return isDoubleValue(value);
161 } else if (type.equalsIgnoreCase(Constants.BOOL)) {
162 return isBoolValue(value);
167 public boolean isBoolValue(String value) {
168 boolean isBool = false;
170 && (value.equalsIgnoreCase("true") || value
171 .equalsIgnoreCase("false"))) {
177 public boolean isIntValue(String value) {
178 boolean isInt = true;
180 Integer.parseInt(value);
181 } catch (Exception e) {
187 public boolean isDoubleValue(String value) {
188 boolean isDouble = true;
190 Double.parseDouble(value);
191 } catch (Exception e) {
197 public boolean isRangeValid(String minStr, String maxStr, String type) {
198 if (null == type || type.trim().isEmpty()) {
201 boolean result = true;
202 if (type.equals(Constants.INT)) {
205 min = Integer.parseInt(minStr);
206 max = Integer.parseInt(maxStr);
210 } catch (Exception e) {
213 } else if (type.equals(Constants.DOUBLE)) {
216 min = Double.parseDouble(minStr);
217 max = Double.parseDouble(maxStr);
221 } catch (Exception e) {
228 public boolean isDefaultValueValid(String value) {
229 if (null == attributeType || attributeType.trim().isEmpty()) {
232 boolean result = true;
233 if (attributeType.equals(Constants.STRING)) {
234 if (validValuesType == ValidValuesType.VALUESET
235 && null != allowedValues) {
236 result = allowedValues.contains(value);
240 } else if (attributeType.equals(Constants.BOOL)) {
241 String val = value.toLowerCase();
242 if (!(val.equals("true") || val.equals("false"))) {
246 if (validValuesType == ValidValuesType.RANGE) {
247 if (attributeType.equals(Constants.INT)) {
248 int min, max, dflValue;
250 min = Integer.parseInt(this.min);
251 max = Integer.parseInt(this.max);
252 dflValue = Integer.parseInt(value);
253 if (dflValue < min || dflValue > max) {
256 } catch (Exception e) {
259 } else if (attributeType.equals(Constants.DOUBLE)) {
260 double min, max, dflValue;
262 min = Double.parseDouble(this.min);
263 max = Double.parseDouble(this.max);
264 dflValue = Double.parseDouble(value);
265 boolean found = false;
266 for (double val = min; val <= max; val += 1.0) {
267 if (dflValue == val) {
275 } catch (Exception e) {
279 } else if (validValuesType == ValidValuesType.VALUESET
280 && null != allowedValues && !allowedValues.isEmpty()) {
281 boolean found = false;
282 if (attributeType.equals(Constants.INT)) {
283 Iterator<String> itr = allowedValues.iterator();
284 while (itr.hasNext()) {
285 String val = itr.next();
286 int v1 = Integer.parseInt(val);
287 int v2 = Integer.parseInt(value);
294 Iterator<String> itr = allowedValues.iterator();
295 while (itr.hasNext()) {
296 String val = itr.next();
297 double v1 = Double.parseDouble(val);
298 double v2 = Double.parseDouble(value);
313 public void setAllowedValuesByArray(String[] cusItems) {
314 if (null == cusItems) {
317 for (String item : cusItems) {
318 addValueToAllowedValues(item);
322 public boolean isAllowedValueExist(String[] items, String value) {
323 if (null == items || items.length < 1 || null == value
326 for (String item : items) {
327 if (value.equalsIgnoreCase(item)) {
334 public SimulatorResourceAttribute convertToSimulatorResourceAttribute() {
335 AttributeValue attValue = null;
336 AttributeProperty attProperty = null;
337 SimulatorResourceAttribute simResAtt;
339 ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
342 IntegerProperty.Builder intPropertyBuilder = new IntegerProperty.Builder();
343 attValue = new AttributeValue(
344 Integer.parseInt(attributeDflValue));
346 // Set default value.
347 intPropertyBuilder.setDefaultValue(Integer
348 .parseInt(attributeDflValue));
350 if (validValuesType == ValidValuesType.VALUESET) {
351 // Set allowed values.
352 intPropertyBuilder.setValues(Utility
353 .convertSetToArrayInt(Utility
354 .convertSetStringToSetObject(allowedValues,
356 } else if (validValuesType == ValidValuesType.RANGE) {
358 intPropertyBuilder.setRange(Integer.parseInt(min),
359 Integer.parseInt(max));
361 attProperty = intPropertyBuilder.build();
364 DoubleProperty.Builder dblPropertyBuilder = new DoubleProperty.Builder();
365 attValue = new AttributeValue(
366 Double.parseDouble(attributeDflValue));
368 // Set default value.
369 dblPropertyBuilder.setDefaultValue(Double
370 .parseDouble(attributeDflValue));
372 if (validValuesType == ValidValuesType.VALUESET) {
373 // Set allowed values.
374 dblPropertyBuilder.setValues(Utility
375 .convertSetToArrayDouble(Utility
376 .convertSetStringToSetObject(allowedValues,
378 } else if (validValuesType == ValidValuesType.RANGE) {
380 dblPropertyBuilder.setRange(Double.parseDouble(min),
381 Double.parseDouble(max));
383 attProperty = dblPropertyBuilder.build();
386 attValue = new AttributeValue(
387 Boolean.parseBoolean(attributeDflValue));
388 BooleanProperty.Builder boolPropertyBuilder = new BooleanProperty.Builder();
389 // Set Default Value.
390 boolPropertyBuilder.setDefaultValue(Boolean
391 .parseBoolean(attributeDflValue));
393 attProperty = boolPropertyBuilder.build();
396 attValue = new AttributeValue(attributeDflValue);
397 StringProperty.Builder stringPropertyBuilder = new StringProperty.Builder();
399 // Set Default Value.
400 stringPropertyBuilder.setDefaultValue(attributeDflValue);
402 if (validValuesType == ValidValuesType.VALUESET) {
403 // Set Allowed Values.
404 stringPropertyBuilder.setValues(Utility
405 .convertSetToArrayString(Utility
406 .convertSetStringToSetObject(allowedValues,
409 attProperty = stringPropertyBuilder.build();
414 simResAtt = new SimulatorResourceAttribute(attributeName, attValue,