Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / AttributeValueVisitor.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 org.oic.simulator;
18
19 /**
20  * This class visits the given attribute value and invokes the right method
21  * based on the type of the attribute value.
22  */
23 public class AttributeValueVisitor {
24     private AttributeValue  mValue;
25     private VisitingMethods mListener;
26
27     public interface VisitingMethods<T extends Object> {
28         public T visitingValue(Integer value);
29
30         public T visitingValue(Double value);
31
32         public T visitingValue(Boolean value);
33
34         public T visitingValue(String value);
35
36         public T visitingValue(SimulatorResourceModel value);
37
38         public T visitingValue(Integer[] values);
39
40         public T visitingValue(Double[] values);
41
42         public T visitingValue(Boolean[] values);
43
44         public T visitingValue(String[] values);
45
46         public T visitingValue(SimulatorResourceModel[] values);
47
48         public T visitingValue(Integer[][] values);
49
50         public T visitingValue(Double[][] values);
51
52         public T visitingValue(Boolean[][] values);
53
54         public T visitingValue(String[][] values);
55
56         public T visitingValue(SimulatorResourceModel[][] values);
57
58         public T visitingValue(Integer[][][] values);
59
60         public T visitingValue(Double[][][] values);
61
62         public T visitingValue(Boolean[][][] values);
63
64         public T visitingValue(String[][][] values);
65
66         public T visitingValue(SimulatorResourceModel[][][] values);
67     }
68
69     /**
70      * Constructs {@link AttributeValueVisitor} from the given
71      * {@link AttributeValue} and the implementation of the
72      * {@link VisitingMethods} interface.
73      *
74      * @param value
75      *            {@link AttributeValue} object.
76      * @param listener
77      *            Implementation of {@link VisitingMethods} interface.
78      */
79     public AttributeValueVisitor(AttributeValue value, VisitingMethods listener) {
80         mValue = value;
81         mListener = listener;
82     }
83
84     /**
85      * API which processes an {@link AttributeValue} to find its value type and
86      * convert the Object to the appropriate type for invoking the right
87      * visitor(method) in the {@link VisitingMethods} interface.
88      *
89      * @return Object which holds the value returned by the visitor method.
90      */
91     public Object visit() {
92         if (null == mValue || null == mListener)
93             return null;
94
95         AttributeValue.TypeInfo typeInfo = mValue.typeInfo();
96         if (AttributeValue.ValueType.INTEGER == typeInfo.mBaseType) {
97             if (AttributeValue.ValueType.INTEGER == typeInfo.mType)
98                 return mListener.visitingValue((Integer) mValue.get());
99             else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
100                 if (1 == typeInfo.mDepth)
101                     return mListener.visitingValue((Integer[]) mValue.get());
102                 if (2 == typeInfo.mDepth)
103                     return mListener.visitingValue((Integer[][]) mValue.get());
104                 if (3 == typeInfo.mDepth)
105                     return mListener
106                             .visitingValue((Integer[][][]) mValue.get());
107             }
108         } else if (AttributeValue.ValueType.DOUBLE == typeInfo.mBaseType) {
109             if (AttributeValue.ValueType.DOUBLE == typeInfo.mType)
110                 return mListener.visitingValue((Double) mValue.get());
111             else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
112                 if (1 == typeInfo.mDepth)
113                     return mListener.visitingValue((Double[]) mValue.get());
114                 if (2 == typeInfo.mDepth)
115                     return mListener.visitingValue((Double[][]) mValue.get());
116                 if (3 == typeInfo.mDepth)
117                     return mListener.visitingValue((Double[][][]) mValue.get());
118             }
119         } else if (AttributeValue.ValueType.BOOLEAN == typeInfo.mBaseType) {
120             if (AttributeValue.ValueType.BOOLEAN == typeInfo.mType)
121                 return mListener.visitingValue((Boolean) mValue.get());
122             else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
123                 if (1 == typeInfo.mDepth)
124                     return mListener.visitingValue((Boolean[]) mValue.get());
125                 if (2 == typeInfo.mDepth)
126                     return mListener.visitingValue((Boolean[][]) mValue.get());
127                 if (3 == typeInfo.mDepth)
128                     return mListener
129                             .visitingValue((Boolean[][][]) mValue.get());
130             }
131         } else if (AttributeValue.ValueType.STRING == typeInfo.mBaseType) {
132             if (AttributeValue.ValueType.STRING == typeInfo.mType)
133                 return mListener.visitingValue((String) mValue.get());
134             else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
135                 if (1 == typeInfo.mDepth)
136                     return mListener.visitingValue((String[]) mValue.get());
137                 if (2 == typeInfo.mDepth)
138                     return mListener.visitingValue((String[][]) mValue.get());
139                 if (3 == typeInfo.mDepth)
140                     return mListener.visitingValue((String[][][]) mValue.get());
141             }
142         } else if (AttributeValue.ValueType.RESOURCEMODEL == typeInfo.mBaseType) {
143             if (AttributeValue.ValueType.RESOURCEMODEL == typeInfo.mType)
144                 return mListener.visitingValue((SimulatorResourceModel) mValue
145                         .get());
146             else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
147                 if (1 == typeInfo.mDepth)
148                     return mListener
149                             .visitingValue((SimulatorResourceModel[]) mValue
150                                     .get());
151                 if (2 == typeInfo.mDepth)
152                     return mListener
153                             .visitingValue((SimulatorResourceModel[][]) mValue
154                                     .get());
155                 if (3 == typeInfo.mDepth)
156                     return mListener
157                             .visitingValue((SimulatorResourceModel[][][]) mValue
158                                     .get());
159             }
160         }
161
162         return null;
163     }
164 }