fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / jurt / com / sun / star / uno / AnyConverter.java
blob8d23dfd41c222ecaea93ce26c0bb622c368aef07
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.uno;
21 /** This class provides static methods which aim at exploring the contents of an
22 * Any and extracting its value. All public methods take an Object argument that
23 * either is the immediate object, such as Boolean, Type, interface implementation,
24 * or an Any that contains an object. <br>The methods which extract the value do a
25 * widening conversion. See the method comments for the respective conversions.
27 public class AnyConverter
29 /** Determines the type of an any object.
31 @param object any object
32 @return type object
34 static public Type getType( Object object )
36 Type t;
37 if (null == object)
39 t = m_XInterface_type;
41 else if (object instanceof Any)
43 t = ((Any)object).getType();
44 // nested any
45 if (TypeClass.ANY_value == t.getTypeClass().getValue())
46 return getType( ((Any)object).getObject() );
48 else
50 t = new Type( object.getClass() );
52 return t;
55 /** checks if the any contains the idl type <code>void</code>.
56 @param object the object to check
57 @return true when the any is void, false otherwise
59 static public boolean isVoid(Object object){
60 return containsType(TypeClass.VOID, object);
63 /** checks if the any contains a value of the idl type <code>char</code>.
64 @param object the object to check
65 @return true when the any contains a char, false otherwise.
67 static public boolean isChar(Object object){
68 return containsType(TypeClass.CHAR, object);
71 /** checks if the any contains a value of the idl type <code>boolean</code>.
72 @param object the object to check
73 @return true when the any contains a boolean, false otherwise.
75 static public boolean isBoolean(Object object){
76 return containsType(TypeClass.BOOLEAN, object);
79 /** checks if the any contains a value of the idl type <code>byte</code>.
80 @param object the object to check
81 @return true when the any contains a byte, false otherwise.
83 static public boolean isByte(Object object){
84 return containsType(TypeClass.BYTE, object);
87 /** checks if the any contains a value of the idl type <code>short</code>.
88 @param object the object to check
89 @return true when the any contains a short, false otherwise.
91 static public boolean isShort(Object object){
92 return containsType(TypeClass.SHORT, object);
95 /** checks if the any contains a value of the idl type <code>long</code> (which maps to a java-int).
96 @param object the object to check
97 @return true when the any contains a int, false otherwise.
99 static public boolean isInt(Object object){
100 return containsType(TypeClass.LONG, object);
103 /** checks if the any contains a value of the idl type <code>hyper</code> (which maps to a java-long).
104 @param object the object to check
105 @return true when the any contains a long, false otherwise.
107 static public boolean isLong(Object object){
108 return containsType(TypeClass.HYPER, object);
111 /** checks if the any contains a value of the idl type <code>float</code>.
112 @param object the object to check
113 @return true when the any contains a float, false otherwise.
115 static public boolean isFloat(Object object){
116 return containsType(TypeClass.FLOAT, object);
119 /** checks if the any contains a value of the idl type <code>double</code>.
120 @param object the object to check
121 @return true when the any contains a double, false otherwise.
123 static public boolean isDouble(Object object){
124 return containsType(TypeClass.DOUBLE, object);
127 /** checks if the any contains a value of the idl type <code>string</code>.
128 @param object the object to check
129 @return true when the any contains a string, false otherwise.
131 static public boolean isString(Object object){
132 return containsType(TypeClass.STRING, object);
135 /** checks if the any contains a value of the idl type <code>enum</code>.
136 @param object the object to check
137 @return true if the any contains an enum, false otherwise
139 static public boolean isEnum(Object object)
141 return containsType(TypeClass.ENUM, object);
144 /** checks if the any contains a value of the idl type <code>type</code>.
145 @param object the object to check
146 @return true when the any contains a type, false otherwise.
148 static public boolean isType(Object object){
149 return containsType(TypeClass.TYPE, object);
152 /** checks if the any contains an interface, struct, exception, sequence or enum.
153 If <em>object</em> is an any with an interface type, then true is also returned if
154 the any contains a null reference. This is because interfaces are allowed to have
155 a null value contrary to other UNO types.
156 @param object the object to check
157 @return true if the any contains an object
159 static public boolean isObject(Object object)
161 int tc = getType(object).getTypeClass().getValue();
162 return (TypeClass.INTERFACE_value == tc ||
163 TypeClass.STRUCT_value == tc ||
164 TypeClass.EXCEPTION_value == tc ||
165 TypeClass.SEQUENCE_value == tc ||
166 TypeClass.ENUM_value == tc);
169 /** checks if the any contains UNO idl sequence value (meaning a java array
170 containing elements which are values of UNO idl types).
171 @param object the object to check
172 @return true when the any contains an object which implements interfaces, false otherwise.
174 static public boolean isArray(Object object){
175 return containsType(TypeClass.SEQUENCE, object);
178 /** converts an Char object or an Any object containing a Char object into a simple char.
179 @param object the object to convert
180 @return the char contained within the object
181 @throws com.sun.star.lang.IllegalArgumentException in case no char is contained within object
182 @see #isChar
184 static public char toChar(Object object) throws com.sun.star.lang.IllegalArgumentException{
185 Character ret= (Character)convertSimple(TypeClass.CHAR, null, object);
186 return ret.charValue();
189 /** converts an Boolean object or an Any object containing a Boolean object into a simple boolean.
190 @param object the object to convert
191 @return the boolean contained within the object
192 @throws com.sun.star.lang.IllegalArgumentException in case no boolean is contained within object
193 @see #isBoolean
195 static public boolean toBoolean(Object object) throws com.sun.star.lang.IllegalArgumentException{
196 Boolean ret= (Boolean)convertSimple(TypeClass.BOOLEAN, null, object);
197 return ret.booleanValue();
200 /** converts an Byte object or an Any object containing a Byte object into a simple byte.
201 @param object the object to convert
202 @return the boolean contained within the object
203 @throws com.sun.star.lang.IllegalArgumentException in case no byte is contained within object
204 @see #isBoolean
206 static public byte toByte(Object object) throws com.sun.star.lang.IllegalArgumentException{
207 Byte ret= (Byte)convertSimple(TypeClass.BYTE, null, object);
208 return ret.byteValue();
211 /** converts a number object into a simple short and allows widening conversions.
212 Allowed argument types are Byte, Short or Any containing these types.
213 @param object the object to convert
214 @throws com.sun.star.lang.IllegalArgumentException in case no short or byte is contained within object
215 @return the short contained within the object
217 static public short toShort(Object object) throws com.sun.star.lang.IllegalArgumentException{
218 Short ret= (Short)convertSimple(TypeClass.SHORT, null, object);
219 return ret.shortValue();
221 /** converts a number object into an idl unsigned short and allows widening conversions.
222 Allowed argument types are Anies containing idl unsigned short values.
223 @param object the object to convert
224 @throws com.sun.star.lang.IllegalArgumentException
225 in case no idl unsigned short is contained within Any
226 @return an (unsigned) short
228 static public short toUnsignedShort(Object object)
229 throws com.sun.star.lang.IllegalArgumentException
231 Short ret= (Short)convertSimple(TypeClass.UNSIGNED_SHORT, null, object);
232 return ret.shortValue();
235 /** converts a number object into a simple int and allows widening conversions.
236 Allowed argument types are Byte, Short, Integer or Any containing these types.
237 @param object the object to convert
238 @throws com.sun.star.lang.IllegalArgumentException in case no short, byte or int is contained within object.
239 @return the int contained within the object
241 static public int toInt(Object object) throws com.sun.star.lang.IllegalArgumentException{
242 Integer ret= (Integer) convertSimple( TypeClass.LONG, null, object);
243 return ret.intValue();
245 /** converts a number object into an idl unsigned long and allows widening conversions.
246 Allowed argument types are Anies containing idl unsigned short or unsigned long values.
247 @param object the object to convert
248 @throws com.sun.star.lang.IllegalArgumentException
249 in case no idl unsigned short nor unsigned long is contained within Any
250 @return an (unsigned) int
252 static public int toUnsignedInt(Object object)
253 throws com.sun.star.lang.IllegalArgumentException
255 Integer ret = (Integer)convertSimple(TypeClass.UNSIGNED_LONG, null, object);
256 return ret.intValue();
259 /** converts a number object into a simple long and allows widening conversions.
260 Allowed argument types are Byte, Short, Integer, Long or Any containing these types.
261 @param object the object to convert
262 @throws com.sun.star.lang.IllegalArgumentException in case no short, byte, int or long
263 is contained within object.
264 @return the long contained within the object
266 static public long toLong(Object object) throws com.sun.star.lang.IllegalArgumentException{
267 Long ret= (Long) convertSimple( TypeClass.HYPER, null, object);
268 return ret.longValue();
270 /** converts a number object into an idl unsigned hyper and allows widening conversions.
271 Allowed argument types are Anies containing idl unsigned short, unsigned long or
272 unsigned hyper values.
273 @param object the object to convert
274 @throws com.sun.star.lang.IllegalArgumentException
275 in case no idl unsigned short, nor unsigned long nor unsigned hyper
276 is contained within object.
277 @return an (unsigned) long
279 static public long toUnsignedLong(Object object)
280 throws com.sun.star.lang.IllegalArgumentException
282 Long ret = (Long)convertSimple(TypeClass.UNSIGNED_HYPER, null, object);
283 return ret.longValue();
286 /** converts a number object into a simple float and allows widening conversions.
287 Allowed argument types are Byte, Short, Float or Any containing these types.
288 @param object the object to convert
289 @throws com.sun.star.lang.IllegalArgumentException in case no byte, short or float
290 is contained within object.
291 @return the float contained within the object
293 static public float toFloat(Object object) throws com.sun.star.lang.IllegalArgumentException{
294 Float ret= (Float) convertSimple( TypeClass.FLOAT,null, object);
295 return ret.floatValue();
298 /** converts a number object into a simple double and allows widening conversions.
299 Allowed argument types are Byte, Short, Int, Float, Double or Any containing these types.
300 @param object the object to convert
301 @throws com.sun.star.lang.IllegalArgumentException in case no byte, short, int, float
302 or double is contained within object.
303 @return the double contained within the object
305 static public double toDouble(Object object) throws com.sun.star.lang.IllegalArgumentException {
306 Double ret= (Double) convertSimple( TypeClass.DOUBLE, null, object);
307 return ret.doubleValue();
310 /** converts a string or an any containing a string into a string.
311 @param object the object to convert
312 @throws com.sun.star.lang.IllegalArgumentException in case no string is contained within object.
313 @return the string contained within the object
315 static public String toString(Object object) throws com.sun.star.lang.IllegalArgumentException {
316 return (String) convertSimple( TypeClass.STRING, null, object);
319 /** converts a Type or an any containing a Type into a Type.
320 @param object the object to convert
321 @throws com.sun.star.lang.IllegalArgumentException in case no type is contained within object.
322 @return the type contained within the object
324 static public Type toType(Object object) throws com.sun.star.lang.IllegalArgumentException {
325 return (Type) convertSimple( TypeClass.TYPE, null, object);
328 /** converts a UNO object (struct, exception, sequence, enum or interface) or an Any containing
329 * these types into an UNO object of a specified destination type.
330 * For interfaces, the argument <em>object</em> is queried for the interface specified
331 * by the <em>type</em> argument. That query (UnoRuntime.queryInterface) might return null,
332 * if the interface is not implemented or a null-ref or a VOID any is given.
334 * @param type type of the returned value
335 * @param object the object that is to be converted
336 * @return destination object
337 * @throws com.sun.star.lang.IllegalArgumentException
338 * in case conversion is not possible
340 static public Object toObject(Type type, Object object)
341 throws com.sun.star.lang.IllegalArgumentException
343 return convertSimple( type.getTypeClass(), type, object );
345 /** converts a UNO object (struct, exception, sequence, enum or interface) or an Any containing
346 * these types into an UNO object of a specified destination type.
347 * For interfaces, the argument <em>object</em> is queried for the interface specified
348 * by the <em>type</em> argument. That query (UnoRuntime.queryInterface) might return null,
349 * if the interface is not implemented or a null-ref or a VOID any is given.
351 * @param clazz class of the returned value
352 * @param object the object that is to be converted
353 * @return destination object
354 * @throws com.sun.star.lang.IllegalArgumentException
355 * in case conversion is not possible
357 static public Object toObject(Class<?> clazz, Object object)
358 throws com.sun.star.lang.IllegalArgumentException
360 return toObject( new Type( clazz ), object );
363 /** converts an array or an any containing an array into an array.
364 @param object the object to convert
365 @throws com.sun.star.lang.IllegalArgumentException in case no array is contained within object.
366 @return the array contained within the object
368 static public Object toArray( Object object) throws com.sun.star.lang.IllegalArgumentException {
369 return convertSimple( TypeClass.SEQUENCE, null, object);
373 Examines the argument <em>object</em> if is correspond to the type in argument <em>what</em>.
374 <em>object</em> is either matched directly against the type or if it is an any then the
375 contained object is matched against the type.
377 static private boolean containsType( TypeClass what, Object object){
378 return (getType(object).getTypeClass().getValue() == what.getValue());
381 static private final Type m_XInterface_type = new Type( XInterface.class );
383 static private Object convertSimple( TypeClass destTClass, Type destType, Object object_ )
384 throws com.sun.star.lang.IllegalArgumentException
386 Object object;
387 Type type;
388 if (object_ instanceof Any)
390 // unbox
391 Any a = (Any)object_;
392 object = a.getObject();
393 type = a.getType();
394 // nested any
395 if (TypeClass.ANY_value == type.getTypeClass().getValue())
396 return convertSimple( destTClass, destType, object );
398 else
400 object = object_;
401 type = (null == object ? m_XInterface_type : new Type( object.getClass() ));
404 int tc = type.getTypeClass().getValue();
405 int dest_tc = destTClass.getValue();
407 if (null == object)
409 // special for interfaces
410 if (TypeClass.INTERFACE_value == tc && dest_tc == tc)
411 return null;
413 else
415 switch (dest_tc)
417 case TypeClass.CHAR_value:
418 if (tc == TypeClass.CHAR_value)
419 return object;
420 break;
421 case TypeClass.BOOLEAN_value:
422 if (tc == TypeClass.BOOLEAN_value)
423 return object;
424 break;
425 case TypeClass.BYTE_value:
426 if (tc == TypeClass.BYTE_value)
427 return object;
428 break;
429 case TypeClass.SHORT_value:
430 switch (tc)
432 case TypeClass.BYTE_value:
433 return new Short( ((Byte)object).byteValue() );
434 case TypeClass.SHORT_value:
435 return object;
437 break;
438 case TypeClass.UNSIGNED_SHORT_value:
439 switch (tc)
441 case TypeClass.UNSIGNED_SHORT_value:
442 return object;
444 break;
445 case TypeClass.LONG_value:
446 switch (tc)
448 case TypeClass.BYTE_value:
449 return new Integer( ((Byte)object).byteValue() );
450 case TypeClass.SHORT_value:
451 case TypeClass.UNSIGNED_SHORT_value:
452 return new Integer( ((Short)object).shortValue() );
453 case TypeClass.LONG_value:
454 return object;
456 break;
457 case TypeClass.UNSIGNED_LONG_value:
458 switch (tc)
460 case TypeClass.UNSIGNED_SHORT_value:
461 return new Integer( ((Short)object).shortValue() );
462 case TypeClass.UNSIGNED_LONG_value:
463 return object;
465 break;
466 case TypeClass.HYPER_value:
467 switch (tc)
469 case TypeClass.BYTE_value:
470 return new Long( ((Byte)object).byteValue() );
471 case TypeClass.SHORT_value:
472 case TypeClass.UNSIGNED_SHORT_value:
473 return new Long( ((Short)object).shortValue() );
474 case TypeClass.LONG_value:
475 case TypeClass.UNSIGNED_LONG_value:
476 return new Long( ((Integer)object).intValue() );
477 case TypeClass.HYPER_value:
478 return object;
480 break;
481 case TypeClass.UNSIGNED_HYPER_value:
482 switch (tc)
484 case TypeClass.UNSIGNED_SHORT_value:
485 return new Long( ((Short)object).shortValue() );
486 case TypeClass.UNSIGNED_LONG_value:
487 return new Long( ((Integer)object).intValue() );
488 case TypeClass.UNSIGNED_HYPER_value:
489 return object;
491 break;
492 case TypeClass.FLOAT_value:
493 switch (tc)
495 case TypeClass.BYTE_value:
496 return new Float( ((Byte)object).byteValue() );
497 case TypeClass.SHORT_value:
498 return new Float( ((Short)object).shortValue() );
499 case TypeClass.FLOAT_value:
500 return object;
502 break;
503 case TypeClass.DOUBLE_value:
504 switch (tc)
506 case TypeClass.BYTE_value:
507 return new Double( ((Byte)object).byteValue() );
508 case TypeClass.SHORT_value:
509 return new Double( ((Short)object).shortValue() );
510 case TypeClass.LONG_value:
511 return new Double( ((Integer)object).intValue() );
512 case TypeClass.FLOAT_value:
513 return new Double( ((Float)object).floatValue() );
514 case TypeClass.DOUBLE_value:
515 return object;
517 break;
518 case TypeClass.ENUM_value:
519 if (tc == TypeClass.ENUM_value &&
520 (null == destTClass || destType.equals( type ) /* optional destType */))
522 return object;
524 break;
525 case TypeClass.STRING_value:
526 if (tc == TypeClass.STRING_value)
527 return object;
528 break;
529 case TypeClass.TYPE_value:
530 if (tc == TypeClass.TYPE_value)
531 return object;
532 break;
533 case TypeClass.INTERFACE_value:
534 // Because object is a class, not an interface, it is
535 // controversial what kind of Type "new Type(object.class)"
536 // above should return (UNKNOWN or INTERFACE), so that we should
537 // not check here for "tc == TypeClass.INTERFACE_value".
538 // Instead, we check whether object (indirectly) derives from
539 // XInterface:
540 if (object instanceof XInterface)
541 return UnoRuntime.queryInterface( destType, object );
542 break;
543 case TypeClass.STRUCT_value:
544 case TypeClass.EXCEPTION_value:
545 if (destType.isSupertypeOf(type)) {
546 return object;
548 break;
549 case TypeClass.SEQUENCE_value:
550 if (tc == TypeClass.SEQUENCE_value &&
551 (null == destType || destType.equals( type ) /* optional destType */))
553 return object;
555 break;
558 throw new com.sun.star.lang.IllegalArgumentException(
559 "The Argument did not hold the proper type");