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 oic.simulator.serviceprovider.utils.Constants;
24 import oic.simulator.serviceprovider.utils.Utility;
26 import org.oic.simulator.AttributeProperty;
27 import org.oic.simulator.AttributeProperty.Type;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.AttributeValue.ValueType;
30 import org.oic.simulator.SimulatorResourceAttribute;
32 public class AttributeHelper {
33 private String attributeName;
34 private String attributeType;
37 private Set<String> allowedValues;
38 private String attributeDflValue;
40 private Type validValuesType;
42 public AttributeHelper() {
43 allowedValues = new HashSet<String>();
44 setValidValuesType(Type.UNKNOWN);
48 public AttributeHelper clone() {
49 AttributeHelper att = new AttributeHelper();
50 att.setAttributeName(attributeName);
51 att.setAttributeType(attributeType);
54 if (null != allowedValues) {
55 Set<String> values = new HashSet<String>();
56 values.addAll(allowedValues);
57 att.setAllowedValues(values);
59 att.setAttributeDflValue(attributeDflValue);
60 att.setValidValuesType(validValuesType);
64 public String toString() {
65 return attributeName + "," + attributeType + "," + attributeDflValue
66 + "," + validValuesType + ",(" + min + max + "),("
67 + allowedValues + ")";
70 public String getAttributeName() {
74 public void setAttributeName(String attributeName) {
75 this.attributeName = attributeName;
78 public String getAttributeType() {
82 public void setAttributeType(String attributeType) {
83 this.attributeType = attributeType;
86 public String getMin() {
90 public void setMin(String min) {
94 public String getMax() {
98 public void setMax(String max) {
102 public Set<String> getAllowedValues() {
103 return allowedValues;
106 public void setAllowedValues(Set<String> allowedValues) {
107 this.allowedValues = allowedValues;
110 public void addValueToAllowedValues(String value) {
111 if (null != value && value.trim().length() > 0) {
112 if (null == allowedValues) {
113 allowedValues = new HashSet<String>();
115 allowedValues.add(value);
119 public void removeValueFromAllowedValues(String value) {
120 if (null != allowedValues && null != value && value.trim().length() > 0) {
121 allowedValues.remove(value);
125 public void removeAllCustomValues() {
126 allowedValues.clear();
129 public String getAttributeDflValue() {
130 return attributeDflValue;
133 public void setAttributeDflValue(String attributeDflValue) {
134 this.attributeDflValue = attributeDflValue;
137 public Type getValidValuesType() {
138 return validValuesType;
141 public void setValidValuesType(Type validValuesType) {
142 this.validValuesType = validValuesType;
145 public boolean isValueValid(String value, String type) {
146 if (null == value || value.trim().length() < 1 || null == type
147 || type.trim().length() < 1)
149 if (type.equalsIgnoreCase(Constants.INT)) {
150 return isIntValue(value);
151 } else if (type.equalsIgnoreCase(Constants.DOUBLE)) {
152 return isDoubleValue(value);
153 } else if (type.equalsIgnoreCase(Constants.BOOL)) {
154 return isBoolValue(value);
159 public boolean isBoolValue(String value) {
160 boolean isBool = false;
162 && (value.equalsIgnoreCase("true") || value
163 .equalsIgnoreCase("false"))) {
169 public boolean isIntValue(String value) {
170 boolean isInt = true;
172 Integer.parseInt(value);
173 } catch (Exception e) {
179 public boolean isDoubleValue(String value) {
180 boolean isDouble = true;
182 Double.parseDouble(value);
183 } catch (Exception e) {
189 public boolean isRangeValid(String minStr, String maxStr, String type) {
190 if (null == type || type.trim().isEmpty()) {
193 boolean result = true;
194 if (type.equals(Constants.INT)) {
197 min = Integer.parseInt(minStr);
198 max = Integer.parseInt(maxStr);
202 } catch (Exception e) {
205 } else if (type.equals(Constants.DOUBLE)) {
208 min = Double.parseDouble(minStr);
209 max = Double.parseDouble(maxStr);
213 } catch (Exception e) {
220 public boolean isDefaultValueValid(String value) {
221 if (null == attributeType || attributeType.trim().isEmpty()) {
224 boolean result = true;
225 if (attributeType.equals(Constants.STRING)) {
226 if (validValuesType == Type.VALUESET && null != allowedValues) {
227 result = allowedValues.contains(value);
231 } else if (attributeType.equals(Constants.BOOL)) {
232 String val = value.toLowerCase();
233 if (!(val.equals("true") || val.equals("false"))) {
237 if (validValuesType == Type.RANGE) {
238 if (attributeType.equals(Constants.INT)) {
239 int min, max, dflValue;
241 min = Integer.parseInt(this.min);
242 max = Integer.parseInt(this.max);
243 dflValue = Integer.parseInt(value);
244 if (dflValue < min || dflValue > max) {
247 } catch (Exception e) {
250 } else if (attributeType.equals(Constants.DOUBLE)) {
251 double min, max, dflValue;
253 min = Double.parseDouble(this.min);
254 max = Double.parseDouble(this.max);
255 dflValue = Double.parseDouble(value);
256 boolean found = false;
257 for (double val = min; val <= max; val += 1.0) {
258 if (dflValue == val) {
266 } catch (Exception e) {
270 } else if (validValuesType == Type.VALUESET
271 && null != allowedValues && !allowedValues.isEmpty()) {
272 boolean found = false;
273 if (attributeType.equals(Constants.INT)) {
274 Iterator<String> itr = allowedValues.iterator();
275 while (itr.hasNext()) {
276 String val = itr.next();
277 int v1 = Integer.parseInt(val);
278 int v2 = Integer.parseInt(value);
285 Iterator<String> itr = allowedValues.iterator();
286 while (itr.hasNext()) {
287 String val = itr.next();
288 double v1 = Double.parseDouble(val);
289 double v2 = Double.parseDouble(value);
304 public void setAllowedValuesByArray(String[] cusItems) {
305 if (null == cusItems) {
308 for (String item : cusItems) {
309 addValueToAllowedValues(item);
313 public boolean isAllowedValueExist(String[] items, String value) {
314 if (null == items || items.length < 1 || null == value
317 for (String item : items) {
318 if (value.equalsIgnoreCase(item)) {
325 public SimulatorResourceAttribute convertToSimulatorResourceAttribute() {
326 AttributeValue attValue = null;
327 AttributeProperty attProperty = null;
328 SimulatorResourceAttribute simResAtt;
330 ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
333 attValue = new AttributeValue(
334 Integer.parseInt(attributeDflValue));
335 if (validValuesType == Type.VALUESET) {
336 attProperty = new AttributeProperty(
337 Utility.convertSetToArrayInt(Utility
338 .convertSetStringToSetObject(allowedValues,
340 } else if (validValuesType == Type.RANGE) {
341 attProperty = new AttributeProperty(
342 Double.parseDouble(min), Double.parseDouble(max));
348 attValue = new AttributeValue(
349 Double.parseDouble(attributeDflValue));
350 if (validValuesType == Type.VALUESET) {
351 attProperty = new AttributeProperty(
352 Utility.convertSetToArrayDouble(Utility
353 .convertSetStringToSetObject(allowedValues,
355 } else if (validValuesType == Type.RANGE) {
356 attProperty = new AttributeProperty(
357 Double.parseDouble(min), Double.parseDouble(max));
363 attValue = new AttributeValue(
364 Boolean.parseBoolean(attributeDflValue));
365 boolean[] arr = { true, false };
366 attProperty = new AttributeProperty(arr);
369 attValue = new AttributeValue(attributeDflValue);
370 if (validValuesType == Type.VALUESET) {
371 attProperty = new AttributeProperty(
372 Utility.convertSetToArrayString(Utility
373 .convertSetStringToSetObject(allowedValues,
382 simResAtt = new SimulatorResourceAttribute(attributeName, attValue,