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;
23 import org.oic.simulator.AttributeProperty;
24 import org.oic.simulator.AttributeValue;
25 import org.oic.simulator.AttributeValue.ValueType;
26 import org.oic.simulator.BooleanProperty;
27 import org.oic.simulator.DoubleProperty;
28 import org.oic.simulator.IntegerProperty;
29 import org.oic.simulator.SimulatorResourceAttribute;
30 import org.oic.simulator.StringProperty;
32 import oic.simulator.serviceprovider.utils.Constants;
33 import oic.simulator.serviceprovider.utils.Utility;
35 public class AttributeHelper {
36 private String attributeName;
37 private String attributeType;
40 private Set<String> allowedValues;
41 private String attributeDflValue;
43 public enum ValidValuesType {
44 RANGE, VALUESET, UNKNOWN
47 private ValidValuesType validValuesType;
49 public AttributeHelper() {
50 allowedValues = new HashSet<String>();
51 setValidValuesType(ValidValuesType.UNKNOWN);
55 public AttributeHelper clone() {
56 AttributeHelper att = new AttributeHelper();
57 att.setAttributeName(attributeName);
58 att.setAttributeType(attributeType);
61 if (null != allowedValues) {
62 Set<String> values = new HashSet<String>();
63 values.addAll(allowedValues);
64 att.setAllowedValues(values);
66 att.setAttributeDflValue(attributeDflValue);
67 att.setValidValuesType(validValuesType);
71 public String toString() {
72 return attributeName + "," + attributeType + "," + attributeDflValue
73 + "," + validValuesType + ",(" + min + max + "),("
74 + allowedValues + ")";
77 public String getAttributeName() {
81 public void setAttributeName(String attributeName) {
82 this.attributeName = attributeName;
85 public String getAttributeType() {
89 public void setAttributeType(String attributeType) {
90 this.attributeType = attributeType;
93 public String getMin() {
97 public void setMin(String min) {
101 public String getMax() {
105 public void setMax(String max) {
109 public Set<String> getAllowedValues() {
110 return allowedValues;
113 public void setAllowedValues(Set<String> allowedValues) {
114 this.allowedValues = allowedValues;
117 public void addValueToAllowedValues(String value) {
118 if (null != value && value.trim().length() > 0) {
119 if (null == allowedValues) {
120 allowedValues = new HashSet<String>();
122 allowedValues.add(value);
126 public void removeValueFromAllowedValues(String value) {
127 if (null != allowedValues && null != value && value.trim().length() > 0) {
128 allowedValues.remove(value);
132 public void removeAllCustomValues() {
133 allowedValues.clear();
136 public String getAttributeDflValue() {
137 return attributeDflValue;
140 public void setAttributeDflValue(String attributeDflValue) {
141 this.attributeDflValue = attributeDflValue;
144 public ValidValuesType getValidValuesType() {
145 return validValuesType;
148 public void setValidValuesType(ValidValuesType validValuesType) {
149 this.validValuesType = validValuesType;
152 public boolean isValueValid(String value, String type) {
153 if (null == value || value.trim().length() < 1 || null == type
154 || type.trim().length() < 1)
156 if (type.equalsIgnoreCase(Constants.INT)) {
157 return isIntValue(value);
158 } else if (type.equalsIgnoreCase(Constants.DOUBLE)) {
159 return isDoubleValue(value);
160 } else if (type.equalsIgnoreCase(Constants.BOOL)) {
161 return isBoolValue(value);
166 public boolean isBoolValue(String value) {
167 boolean isBool = false;
169 && (value.equalsIgnoreCase("true") || value
170 .equalsIgnoreCase("false"))) {
176 public boolean isIntValue(String value) {
177 boolean isInt = true;
179 Integer.parseInt(value);
180 } catch (Exception e) {
186 public boolean isDoubleValue(String value) {
187 boolean isDouble = true;
189 Double.parseDouble(value);
190 } catch (Exception e) {
196 public boolean isRangeValid(String minStr, String maxStr, String type) {
197 if (null == type || type.trim().isEmpty()) {
200 boolean result = true;
201 if (type.equals(Constants.INT)) {
204 min = Integer.parseInt(minStr);
205 max = Integer.parseInt(maxStr);
209 } catch (Exception e) {
212 } else if (type.equals(Constants.DOUBLE)) {
215 min = Double.parseDouble(minStr);
216 max = Double.parseDouble(maxStr);
220 } catch (Exception e) {
227 public boolean isDefaultValueValid(String value) {
228 if (null == attributeType || attributeType.trim().isEmpty()) {
231 boolean result = true;
232 if (attributeType.equals(Constants.STRING)) {
233 if (validValuesType == ValidValuesType.VALUESET
234 && null != allowedValues) {
235 result = allowedValues.contains(value);
239 } else if (attributeType.equals(Constants.BOOL)) {
240 String val = value.toLowerCase();
241 if (!(val.equals("true") || val.equals("false"))) {
245 if (validValuesType == ValidValuesType.RANGE) {
246 if (attributeType.equals(Constants.INT)) {
247 int min, max, dflValue;
249 min = Integer.parseInt(this.min);
250 max = Integer.parseInt(this.max);
251 dflValue = Integer.parseInt(value);
252 if (dflValue < min || dflValue > max) {
255 } catch (Exception e) {
258 } else if (attributeType.equals(Constants.DOUBLE)) {
259 double min, max, dflValue;
261 min = Double.parseDouble(this.min);
262 max = Double.parseDouble(this.max);
263 dflValue = Double.parseDouble(value);
264 boolean found = false;
265 for (double val = min; val <= max; val += 1.0) {
266 if (Double.compare(dflValue, val) == 0) {
274 } catch (Exception e) {
278 } else if (validValuesType == ValidValuesType.VALUESET
279 && null != allowedValues && !allowedValues.isEmpty()) {
280 boolean found = false;
281 if (attributeType.equals(Constants.INT)) {
282 Iterator<String> itr = allowedValues.iterator();
283 while (itr.hasNext()) {
284 String val = itr.next();
285 int v1 = Integer.parseInt(val);
286 int v2 = Integer.parseInt(value);
293 Iterator<String> itr = allowedValues.iterator();
294 while (itr.hasNext()) {
295 String val = itr.next();
296 double v1 = Double.parseDouble(val);
297 double v2 = Double.parseDouble(value);
298 if (Double.compare(v1, v2) == 0) {
312 public void setAllowedValuesByArray(String[] cusItems) {
313 if (null == cusItems) {
316 for (String item : cusItems) {
317 addValueToAllowedValues(item);
321 public boolean isAllowedValueExist(String[] items, String value) {
322 if (null == items || items.length < 1 || null == value
325 for (String item : items) {
326 if (value.equalsIgnoreCase(item)) {
333 public SimulatorResourceAttribute convertToSimulatorResourceAttribute() {
334 AttributeValue attValue = null;
335 AttributeProperty attProperty = null;
336 SimulatorResourceAttribute simResAtt;
338 ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
341 IntegerProperty.Builder intPropertyBuilder = new IntegerProperty.Builder();
342 attValue = new AttributeValue(
343 Integer.parseInt(attributeDflValue));
345 // Set default value.
346 intPropertyBuilder.setDefaultValue(Integer
347 .parseInt(attributeDflValue));
349 if (validValuesType == ValidValuesType.VALUESET) {
350 // Set allowed values.
351 intPropertyBuilder.setValues(Utility
352 .convertSetToArrayInt(Utility
353 .convertSetStringToSetObject(allowedValues,
355 } else if (validValuesType == ValidValuesType.RANGE) {
357 intPropertyBuilder.setRange(Integer.parseInt(min),
358 Integer.parseInt(max));
360 attProperty = intPropertyBuilder.build();
363 DoubleProperty.Builder dblPropertyBuilder = new DoubleProperty.Builder();
364 attValue = new AttributeValue(
365 Double.parseDouble(attributeDflValue));
367 // Set default value.
368 dblPropertyBuilder.setDefaultValue(Double
369 .parseDouble(attributeDflValue));
371 if (validValuesType == ValidValuesType.VALUESET) {
372 // Set allowed values.
373 dblPropertyBuilder.setValues(Utility
374 .convertSetToArrayDouble(Utility
375 .convertSetStringToSetObject(allowedValues,
377 } else if (validValuesType == ValidValuesType.RANGE) {
379 dblPropertyBuilder.setRange(Double.parseDouble(min),
380 Double.parseDouble(max));
382 attProperty = dblPropertyBuilder.build();
385 attValue = new AttributeValue(
386 Boolean.parseBoolean(attributeDflValue));
387 BooleanProperty.Builder boolPropertyBuilder = new BooleanProperty.Builder();
388 // Set Default Value.
389 boolPropertyBuilder.setDefaultValue(Boolean
390 .parseBoolean(attributeDflValue));
392 attProperty = boolPropertyBuilder.build();
395 attValue = new AttributeValue(attributeDflValue);
396 StringProperty.Builder stringPropertyBuilder = new StringProperty.Builder();
398 // Set Default Value.
399 stringPropertyBuilder.setDefaultValue(attributeDflValue);
401 if (validValuesType == ValidValuesType.VALUESET) {
402 // Set Allowed Values.
403 stringPropertyBuilder.setValues(Utility
404 .convertSetToArrayString(Utility
405 .convertSetStringToSetObject(allowedValues,
408 attProperty = stringPropertyBuilder.build();
413 simResAtt = new SimulatorResourceAttribute(attributeName, attValue,