summaryrefslogtreecommitdiffstats
path: root/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java
blob: cbfd93796f6a82fc306205051c149c93abae0c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.binding.erlang.impl.types;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException;

import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;

/**
 * @version $Rev$ $Date$
 */
public class TypeHelpersProxy {

	private static Map<Class<?>, TypeHelper> primitiveTypes = null;

	static {
		// initiate type helpers
		primitiveTypes = new HashMap<Class<?>, TypeHelper>();
		primitiveTypes.put(boolean.class, new BooleanTypeHelper());
		primitiveTypes.put(short.class, new ShortTypeHelper());
		primitiveTypes.put(byte.class, new ByteTypeHelper());
		primitiveTypes.put(char.class, new CharTypeHelper());
		primitiveTypes.put(int.class, new IntTypeHelper());
		primitiveTypes.put(long.class, new LongTypeHelper());
		primitiveTypes.put(float.class, new FloatTypeHelper());
		primitiveTypes.put(double.class, new DoubleTypeHelper());
		primitiveTypes.put(String.class, new StringTypeHelper());
		primitiveTypes.put(Boolean.class, primitiveTypes.get(boolean.class));
		primitiveTypes.put(Character.class, primitiveTypes.get(char.class));
		primitiveTypes.put(Short.class, primitiveTypes.get(char.class));
		primitiveTypes.put(Byte.class, primitiveTypes.get(byte.class));
		primitiveTypes.put(Short.class, primitiveTypes.get(short.class));
		primitiveTypes.put(Integer.class, primitiveTypes.get(int.class));
		primitiveTypes.put(Long.class, primitiveTypes.get(long.class));
		primitiveTypes.put(Float.class, primitiveTypes.get(float.class));
		primitiveTypes.put(Double.class, primitiveTypes.get(double.class));
		primitiveTypes.put(byte[].class, new BinaryTypeHelper());
	}

	private static TypeHelper getTypeHelper(Class<?> forClass) {
		TypeHelper typeHelper = primitiveTypes.get(forClass);
		if (typeHelper == null && forClass.isArray()) {
			typeHelper = new ListTypeHelper();
		}
		if (typeHelper == null) {
			typeHelper = new TupleTypeHelper();
		}
		return typeHelper;
	}

	/**
	 * Converts Java objects arrays to Erlang: 1. single object (if array arity
	 * == 1) or 2. tuple (if array arity > 1)
	 * 
	 * @param objects
	 * @return
	 */
	public static OtpErlangObject toErlang(Object[] objects) {
		OtpErlangObject result = null;
		if (objects != null) {
			TypeHelper helper = null;
			switch (objects.length) {
			case 0:
				result = new OtpErlangList();
				break;
			case 1:
				helper = getTypeHelper(objects[0].getClass());
				result = helper.toErlang(objects[0]);
				break;
			default:
				OtpErlangObject[] erlObjects = new OtpErlangObject[objects.length];
				for (int i = 0; i < objects.length; i++) {
					helper = getTypeHelper(objects[i].getClass());
					erlObjects[i] = helper.toErlang(objects[i]);
				}
				result = new OtpErlangTuple(erlObjects);
				break;
			}
		}
		return result;
	}

	/**
	 * Creates Erlang list basing on unknown Java arrays
	 * 
	 * @param array
	 * @return
	 */
	public static OtpErlangList toErlangAsList(Object array) {
		OtpErlangList result = null;
		if (array != null) {
			List<OtpErlangObject> attrsList = new ArrayList<OtpErlangObject>();
			int i = 0;
			while (true) {
				try {
					TypeHelper helper = getTypeHelper(Array.get(array, i)
							.getClass());
					attrsList.add(helper.toErlang(Array.get(array, i)));
					i++;
				} catch (ArrayIndexOutOfBoundsException e) {
					break;
				}
			}
			result = new OtpErlangList(attrsList
					.toArray(new OtpErlangObject[attrsList.size()]));
		} else {
			result = new OtpErlangList();
		}
		return result;
	}

	/**
	 * Converts single Erlang object to Java
	 * 
	 * @param object
	 * @param forClass
	 * @return
	 * @throws Exception
	 */
	public static Object toJava(OtpErlangObject object, Class<?> forClass)
			throws Exception {
		try {
			TypeHelper helper = getTypeHelper(forClass);
			return helper.toJava(object, forClass);
		} catch (ClassCastException e) {
			throw new TypeMismatchException(forClass, object.getClass());
		}
	}

	/**
	 * Creates array of Java objects from Erlang list
	 * 
	 * @param objects
	 * @param forClass
	 * @return
	 * @throws Exception
	 */
	public static Object[] toJavaFromList(OtpErlangList objects,
			Class<?>[] forClass) throws Exception {
		Object[] result = new Object[objects.arity()];
		try {
			for (int i = 0; i < objects.arity(); i++) {
				TypeHelper helper = getTypeHelper(forClass[i]);
				result[i] = helper.toJava(objects.elementAt(i), forClass[i]);
			}
		} catch (Exception e) {
			// type mismatch as mismatch of parameters count or parameters type
			if (e.getClass().equals(ClassCastException.class)
					|| e.getClass()
							.equals(ArrayIndexOutOfBoundsException.class))
				throw new TypeMismatchException();
		}
		return result;
	}

	/**
	 * Converts incoming Erlang message to operation arguments
	 * 
	 * @param objects
	 * @param forClass
	 * @return
	 * @throws Exception
	 */
	public static Object[] toJavaAsArgs(OtpErlangObject objects,
			Class<?>[] forClass) throws Exception {
		OtpErlangObject[] args = null;
		// normalize input
		if (objects.getClass().equals(OtpErlangTuple.class)) {
			args = new OtpErlangObject[((OtpErlangTuple) objects).arity()];
			for (int i = 0; i < ((OtpErlangTuple) objects).arity(); i++) {
				args[i] = ((OtpErlangTuple) objects).elementAt(i);
			}
		} else {
			args = new OtpErlangObject[1];
			args[0] = objects;
		}
		Object[] result = new Object[args.length];
		try {
			for (int i = 0; i < args.length; i++) {
				TypeHelper helper = getTypeHelper(forClass[i]);
				result[i] = helper.toJava(args[i], forClass[i]);
			}
		} catch (Exception e) {
			// type mismatch as mismatch of parameters count or parameters type
			if (e.getClass().equals(ClassCastException.class)
					|| e.getClass()
							.equals(ArrayIndexOutOfBoundsException.class))
				throw new TypeMismatchException();
		}
		return result;
	}

}