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 org.oic.simulator;
20 * This class visits the given attribute value and invokes the right method
21 * based on the type of the attribute value.
23 public class AttributeValueVisitor {
24 private AttributeValue mValue;
25 private VisitingMethods mListener;
27 public interface VisitingMethods<T extends Object> {
28 public T visitingValue(Integer value);
30 public T visitingValue(Double value);
32 public T visitingValue(Boolean value);
34 public T visitingValue(String value);
36 public T visitingValue(SimulatorResourceModel value);
38 public T visitingValue(Integer[] values);
40 public T visitingValue(Double[] values);
42 public T visitingValue(Boolean[] values);
44 public T visitingValue(String[] values);
46 public T visitingValue(SimulatorResourceModel[] values);
48 public T visitingValue(Integer[][] values);
50 public T visitingValue(Double[][] values);
52 public T visitingValue(Boolean[][] values);
54 public T visitingValue(String[][] values);
56 public T visitingValue(SimulatorResourceModel[][] values);
58 public T visitingValue(Integer[][][] values);
60 public T visitingValue(Double[][][] values);
62 public T visitingValue(Boolean[][][] values);
64 public T visitingValue(String[][][] values);
66 public T visitingValue(SimulatorResourceModel[][][] values);
70 * Constructs {@link AttributeValueVisitor} from the given
71 * {@link AttributeValue} and the implementation of the
72 * {@link VisitingMethods} interface.
75 * {@link AttributeValue} object.
77 * Implementation of {@link VisitingMethods} interface.
79 public AttributeValueVisitor(AttributeValue value, VisitingMethods listener) {
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.
89 * @return Object which holds the value returned by the visitor method.
91 public Object visit() {
92 if (null == mValue || null == mListener)
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)
106 .visitingValue((Integer[][][]) mValue.get());
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());
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)
129 .visitingValue((Boolean[][][]) mValue.get());
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());
142 } else if (AttributeValue.ValueType.RESOURCEMODEL == typeInfo.mBaseType) {
143 if (AttributeValue.ValueType.RESOURCEMODEL == typeInfo.mType)
144 return mListener.visitingValue((SimulatorResourceModel) mValue
146 else if (AttributeValue.ValueType.ARRAY == typeInfo.mType) {
147 if (1 == typeInfo.mDepth)
149 .visitingValue((SimulatorResourceModel[]) mValue
151 if (2 == typeInfo.mDepth)
153 .visitingValue((SimulatorResourceModel[][]) mValue
155 if (3 == typeInfo.mDepth)
157 .visitingValue((SimulatorResourceModel[][][]) mValue