Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / remoteresource / RemoteResourceAttribute.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.clientcontroller.remoteresource;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.oic.simulator.ResourceAttribute;
25 import org.oic.simulator.ResourceAttribute.Type;
26
27 /**
28  * This class represents an attribute in the remote resource.
29  */
30 public class RemoteResourceAttribute {
31
32     // Native object reference
33     private ResourceAttribute resourceAttribute;
34
35     private String            attributeName;
36     private Object            attributeValue;
37     private Type              attValType;
38     private Type              attValBaseType;
39     private List<Object>      allowedValues;
40
41     private Object            minValue;
42     private Object            maxValue;
43
44     public ResourceAttribute getResourceAttribute() {
45         return resourceAttribute;
46     }
47
48     public void setResourceAttribute(ResourceAttribute resourceAttribute) {
49         this.resourceAttribute = resourceAttribute;
50     }
51
52     public String getAttributeName() {
53         return attributeName;
54     }
55
56     public void setAttributeName(String attributeName) {
57         this.attributeName = attributeName;
58     }
59
60     public Object getAttributeValue() {
61         return attributeValue;
62     }
63
64     public void setAttributeValue(Object attributeValue) {
65         this.attributeValue = attributeValue;
66     }
67
68     public List<Object> getAllowedValues() {
69         return allowedValues;
70     }
71
72     public void setAllowedValues(List<Object> allowedValues) {
73         this.allowedValues = allowedValues;
74     }
75
76     public void setAllowedValues(String[] allowedValues) {
77         List<Object> allowedValueList = null;
78         if (null != allowedValues && allowedValues.length > 0) {
79             allowedValueList = new ArrayList<Object>();
80             for (String value : allowedValues) {
81                 allowedValueList.add(value);
82             }
83         }
84         this.allowedValues = allowedValueList;
85     }
86
87     public Object getMinValue() {
88         return minValue;
89     }
90
91     public void setMinValue(Object minValue) {
92         this.minValue = minValue;
93     }
94
95     public Object getMaxValue() {
96         return maxValue;
97     }
98
99     public void setMaxValue(Object maxValue) {
100         this.maxValue = maxValue;
101     }
102
103     public static RemoteResourceAttribute clone(
104             RemoteResourceAttribute attribute) {
105         RemoteResourceAttribute clone = null;
106         if (null != attribute) {
107             clone = new RemoteResourceAttribute();
108             clone.setAttributeName(attribute.getAttributeName());
109             clone.setAttributeValue(attribute.getAttributeValue());
110             clone.setAllowedValues(attribute.getAllowedValues());
111             clone.setAttValBaseType(attribute.getAttValBaseType());
112             clone.setAttValType(attribute.getAttValType());
113             clone.setMinValue(attribute.getMinValue());
114             clone.setMaxValue(attribute.getMaxValue());
115             clone.setResourceAttribute(null);
116         }
117         return clone;
118     }
119
120     // This method gives all known possible values of the attribute in string
121     // format.
122     // It takes allowed values or range of values whichever is available
123     public List<String> getAllValues() {
124         List<String> valueList = new ArrayList<String>();
125         if (null != allowedValues) {
126             Iterator<Object> values = allowedValues.iterator();
127             Object value;
128             while (values.hasNext()) {
129                 value = values.next();
130                 if (null != value) {
131                     valueList.add(String.valueOf(value));
132                 }
133             }
134         } else if (null != minValue && null != maxValue) {
135             if (attributeValue.getClass() == Integer.class) {
136                 int min = (Integer) minValue;
137                 int max = (Integer) maxValue;
138                 for (int value = min; value <= max; value++) {
139                     valueList.add(String.valueOf(value));
140                 }
141             } else if (attributeValue.getClass() == Double.class) {
142                 double min = (Double) minValue;
143                 double max = (Double) maxValue;
144                 for (double value = min; value <= max; value++) {
145                     valueList.add(String.valueOf(value));
146                 }
147             }
148         }
149         if (valueList.size() < 1 && null != attributeValue) {
150             valueList.add(String.valueOf(attributeValue));
151         }
152         return valueList;
153     }
154
155     public static void printAttributes(
156             Map<String, RemoteResourceAttribute> attributeMap) {
157         Iterator<String> itr = attributeMap.keySet().iterator();
158         String attName;
159         RemoteResourceAttribute att;
160         while (itr.hasNext()) {
161             attName = itr.next();
162             att = attributeMap.get(attName);
163             System.out.println("AttributeName:" + attName);
164             System.out.println("AttributeValue:"
165                     + att.getAttributeValue().toString());
166             System.out.println("Allowed Values:" + att.getAllValues());
167         }
168     }
169
170     public Type getAttValType() {
171         return attValType;
172     }
173
174     public void setAttValType(Type attValType) {
175         this.attValType = attValType;
176     }
177
178     public Type getAttValBaseType() {
179         return attValBaseType;
180     }
181
182     public void setAttValBaseType(Type attValBaseType) {
183         this.attValBaseType = attValBaseType;
184     }
185 }