Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / SimulatorResourceAttribute.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 /**
18  * This file contains a class which has a set of native methods for
19  * getting the information associated with a particular attribute.
20  */
21 package org.oic.simulator;
22
23 /**
24  * This class represents an attribute of a resource. It has a set of native
25  * methods for getting the attribute's information.
26  */
27 public class SimulatorResourceAttribute {
28
29     /**
30      * Handle to the native object of the simulator resource attribute.
31      */
32     private long nativeHandle;
33
34     public SimulatorResourceAttribute() {
35     }
36
37     /**
38      * Constructor for SimulatorResourceAttribute.
39      *
40      * @param nativeHandle
41      *            Handle to the native SimulatorResourceAttribute object.
42      */
43     private SimulatorResourceAttribute(long handle) {
44         nativeHandle = handle;
45     }
46
47     /**
48      * This constructor is used to create a new attribute.
49      *
50      * @param attrName
51      *            Name of the attribute
52      */
53     public SimulatorResourceAttribute(String attrName) {
54         create(attrName);
55     }
56
57     @Override
58     protected void finalize() throws Throwable {
59         dispose();
60     }
61
62     /**
63      * This generic API is used to get the value of an attribute whose type is
64      * given by the caller of the method.
65      *
66      * @param <T>
67      *            This specifies the type in which the value has to be returned.
68      *
69      * @return The attribute's value in a specified type.
70      */
71     public <T> T getAttributeValue() {
72         Object obj = getValue();
73         @SuppressWarnings("unchecked")
74         T t = (T) obj;
75         return t;
76     }
77
78     /**
79      * Native method to create a new attribute with the given name.
80      *
81      * @param attrName
82      *            Name of the attribute.
83      */
84     public native void create(String attrName);
85
86     /**
87      * Native method for getting the attribute's name.
88      *
89      * @return Attribute's name
90      */
91     public native String getName();
92
93     /**
94      * Native method for getting the attribute's value.
95      *
96      * @return Attribute's value represented as {@link Object}.
97      */
98     public native Object getValue();
99
100     /**
101      * Native method for getting the number of values in the allowed values
102      * list.
103      *
104      * @return Count of allowed values
105      */
106     public native int allowedValuesSize();
107
108     /**
109      * Native method for returning the string representation of the attribute's
110      * value.
111      *
112      * @return Attribute's value as {@link String}.
113      */
114     public native String valueToString();
115
116     /**
117      * Native method for returning the string representation of the attribute's
118      * allowed values.
119      *
120      * @return Attribute's allowed values as {@link String}.
121      */
122     public native String allowedValuesToString();
123
124     /**
125      * Native function to release the memory allocated to the native object for
126      * SimulatorResourceAttribute.
127      */
128     private native void dispose();
129
130 }