Displaying and editing the complex value types for attributes.
[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.AttributeProperty.Type;
25 import org.oic.simulator.AttributeValue.ValueType;
26 import org.oic.simulator.SimulatorResourceAttribute;
27
28 /**
29  * This class represents an attribute in the remote resource.
30  */
31 public class RemoteResourceAttribute {
32
33     // Native object reference
34     private SimulatorResourceAttribute resourceAttributeRef;
35
36     private String                     attributeName;
37     private Object                     attributeValue;
38
39     private ValueType                  attValType;
40     private ValueType                  attValBaseType;
41     private int                        depth;
42
43     private Type                       valuesType;
44     private List<Object>               allowedValues;
45     private Object                     minValue;
46     private Object                     maxValue;
47
48     public SimulatorResourceAttribute getResourceAttributeRef() {
49         return resourceAttributeRef;
50     }
51
52     public void setResourceAttributeRef(
53             SimulatorResourceAttribute resourceAttribute) {
54         this.resourceAttributeRef = resourceAttribute;
55     }
56
57     public String getAttributeName() {
58         return attributeName;
59     }
60
61     public void setAttributeName(String attributeName) {
62         this.attributeName = attributeName;
63     }
64
65     public Object getAttributeValue() {
66         return attributeValue;
67     }
68
69     public void setAttributeValue(Object attributeValue) {
70         this.attributeValue = attributeValue;
71     }
72
73     public List<Object> getAllowedValues() {
74         return allowedValues;
75     }
76
77     public void setAllowedValues(List<Object> allowedValues) {
78         this.allowedValues = allowedValues;
79     }
80
81     public void setAllowedValues(String[] allowedValues) {
82         List<Object> allowedValueList = null;
83         if (null != allowedValues && allowedValues.length > 0) {
84             allowedValueList = new ArrayList<Object>();
85             for (String value : allowedValues) {
86                 allowedValueList.add(value);
87             }
88         }
89         this.allowedValues = allowedValueList;
90     }
91
92     public Object getMinValue() {
93         return minValue;
94     }
95
96     public void setMinValue(Object minValue) {
97         this.minValue = minValue;
98     }
99
100     public Object getMaxValue() {
101         return maxValue;
102     }
103
104     public void setMaxValue(Object maxValue) {
105         this.maxValue = maxValue;
106     }
107
108     public static RemoteResourceAttribute clone(
109             RemoteResourceAttribute attribute) {
110         RemoteResourceAttribute clone = null;
111         if (null != attribute) {
112             clone = new RemoteResourceAttribute();
113             clone.setAttributeName(attribute.getAttributeName());
114             clone.setAttributeValue(attribute.getAttributeValue());
115             clone.setAllowedValues(attribute.getAllowedValues());
116             clone.setAttValBaseType(attribute.getAttValBaseType());
117             clone.setAttValType(attribute.getAttValType());
118             clone.setMinValue(attribute.getMinValue());
119             clone.setMaxValue(attribute.getMaxValue());
120             clone.setResourceAttributeRef(null);
121         }
122         return clone;
123     }
124
125     // This method gives all known possible values of the attribute in string
126     // format.
127     // It takes allowed values or range of values whichever is available
128     public List<String> getAllValues() {
129         List<String> valueList = new ArrayList<String>();
130         if (null != allowedValues) {
131             Iterator<Object> values = allowedValues.iterator();
132             Object value;
133             while (values.hasNext()) {
134                 value = values.next();
135                 if (null != value) {
136                     valueList.add(String.valueOf(value));
137                 }
138             }
139         } else if (null != minValue && null != maxValue) {
140             double min = (Double) minValue;
141             double max = (Double) maxValue;
142             for (double value = min; value <= max; value++) {
143                 valueList.add(String.valueOf(value));
144             }
145         }
146         if (valueList.size() < 1 && null != attributeValue) {
147             valueList.add(String.valueOf(attributeValue));
148         }
149         return valueList;
150     }
151
152     // Method added for debugging purpose
153     public static void printAttributes(
154             Map<String, RemoteResourceAttribute> attributeMap) {
155         Iterator<String> itr = attributeMap.keySet().iterator();
156         String attName;
157         RemoteResourceAttribute att;
158         while (itr.hasNext()) {
159             attName = itr.next();
160             att = attributeMap.get(attName);
161             System.out.println("AttributeName:" + attName);
162             System.out.println("AttributeValue:"
163                     + att.getAttributeValue().toString());
164             System.out.println("Allowed Values:" + att.getAllValues());
165         }
166     }
167
168     public ValueType getAttValType() {
169         return attValType;
170     }
171
172     public void setAttValType(ValueType attValType) {
173         this.attValType = attValType;
174     }
175
176     public ValueType getAttValBaseType() {
177         return attValBaseType;
178     }
179
180     public void setAttValBaseType(ValueType attValBaseType) {
181         this.attValBaseType = attValBaseType;
182     }
183
184     public Type getValuesType() {
185         return valuesType;
186     }
187
188     public void setValuesType(Type valuesType) {
189         this.valuesType = valuesType;
190     }
191
192     public int getDepth() {
193         return depth;
194     }
195
196     public void setDepth(int depth) {
197         this.depth = depth;
198     }
199 }