bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / uno / AnyConverter.java
blobefe46250198c1004719013a184f2744bae90eda2
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 /**
22 * This class provides static methods which aim at exploring the contents of an
23 * Any and extracting its value.
25 * <p>All public methods take an Object argument that either is the immediate object,
26 * such as Boolean, Type, interface implementation, or an Any that contains an
27 * object.</p>
29 * <p>The methods which extract the value do a widening conversion. See the
30 * method comments for the respective conversions.</p>
32 public class AnyConverter
34 /**
35 * Determines the type of an any object.
37 * @param object any object.
38 * @return type object.
40 static public Type getType( Object object )
42 Type t;
43 if (null == object)
45 t = m_XInterface_type;
47 else if (object instanceof Any)
49 t = ((Any)object).getType();
50 // nested any
51 if (TypeClass.ANY_value == t.getTypeClass().getValue())
52 return getType( ((Any)object).getObject() );
54 else
56 t = new Type( object.getClass() );
58 return t;
61 /**
62 * Checks if the any contains the idl type <code>void</code>.
64 * @param object the object to check.
65 * @return true when the any is void, false otherwise.
67 static public boolean isVoid(Object object){
68 return containsType(TypeClass.VOID, object);
71 /**
72 * Checks if the any contains a value of the idl type <code>char</code>.
74 * @param object the object to check.
75 * @return true when the any contains a char, false otherwise.
77 static public boolean isChar(Object object){
78 return containsType(TypeClass.CHAR, object);
81 /**
82 * Checks if the any contains a value of the idl type <code>boolean</code>.
84 * @param object the object to check.
85 * @return true when the any contains a boolean, false otherwise.
87 static public boolean isBoolean(Object object){
88 return containsType(TypeClass.BOOLEAN, object);
91 /**
92 * Checks if the any contains a value of the idl type <code>byte</code>.
94 * @param object the object to check.
95 * @return true when the any contains a byte, false otherwise.
97 static public boolean isByte(Object object){
98 return containsType(TypeClass.BYTE, object);
102 * Checks if the any contains a value of the idl type <code>short</code>.
104 * @param object the object to check.
105 * @return true when the any contains a short, false otherwise.
107 static public boolean isShort(Object object){
108 return containsType(TypeClass.SHORT, object);
112 * Checks if the any contains a value of the idl type <code>long</code>
113 * (which maps to a java-int).
115 * @param object the object to check.
116 * @return true when the any contains a int, false otherwise.
118 static public boolean isInt(Object object){
119 return containsType(TypeClass.LONG, object);
123 * Checks if the any contains a value of the idl type <code>hyper</code>
124 * (which maps to a java-long).
126 * @param object the object to check.
127 * @return true when the any contains a long, false otherwise.
129 static public boolean isLong(Object object){
130 return containsType(TypeClass.HYPER, object);
134 * Checks if the any contains a value of the idl type <code>float</code>.
136 * @param object the object to check.
137 * @return true when the any contains a float, false otherwise.
139 static public boolean isFloat(Object object){
140 return containsType(TypeClass.FLOAT, object);
144 * Checks if the any contains a value of the idl type <code>double</code>.
146 * @param object the object to check.
147 * @return true when the any contains a double, false otherwise.
149 static public boolean isDouble(Object object){
150 return containsType(TypeClass.DOUBLE, object);
154 * Checks if the any contains a value of the idl type <code>string</code>.
156 * @param object the object to check.
157 * @return true when the any contains a string, false otherwise.
159 static public boolean isString(Object object){
160 return containsType(TypeClass.STRING, object);
164 * Checks if the any contains a value of the idl type <code>enum</code>.
166 * @param object the object to check.
167 * @return true if the any contains an enum, false otherwise.
169 static public boolean isEnum(Object object)
171 return containsType(TypeClass.ENUM, object);
175 * Checks if the any contains a value of the idl type <code>type</code>.
177 * @param object the object to check.
178 * @return true when the any contains a type, false otherwise.
180 static public boolean isType(Object object){
181 return containsType(TypeClass.TYPE, object);
185 * Checks if the any contains an interface, struct, exception, sequence or enum.
187 * <p>If <em>object</em> is an any with an interface type, then true is also
188 * returned if the any contains a null reference. This is because interfaces
189 * are allowed to havea null value contrary to other UNO types.</p>
191 * @param object the object to check.
192 * @return true if the any contains an object.
194 static public boolean isObject(Object object)
196 int tc = getType(object).getTypeClass().getValue();
197 return (TypeClass.INTERFACE_value == tc ||
198 TypeClass.STRUCT_value == tc ||
199 TypeClass.EXCEPTION_value == tc ||
200 TypeClass.SEQUENCE_value == tc ||
201 TypeClass.ENUM_value == tc);
205 * Checks if the any contains UNO idl sequence value (meaning a java array
206 * containing elements which are values of UNO idl types).
208 * @param object the object to check.
209 * @return true when the any contains an object which implements interfaces,
210 * false otherwise.
212 static public boolean isArray(Object object){
213 return containsType(TypeClass.SEQUENCE, object);
217 * Converts an Char object or an Any object containing a Char object into a
218 * simple char.
220 * @param object the object to convert.
221 * @return the char contained within the object.
222 * @throws com.sun.star.lang.IllegalArgumentException in case no char is
223 * contained within object.
225 * @see #isChar
227 static public char toChar(Object object) throws com.sun.star.lang.IllegalArgumentException{
228 Character ret= (Character)convertSimple(TypeClass.CHAR, null, object);
229 return ret.charValue();
233 * Converts an Boolean object or an Any object containing a Boolean object
234 * into a simple boolean.
236 * @param object the object to convert.
237 * @return the boolean contained within the object
238 * @throws com.sun.star.lang.IllegalArgumentException in case no boolean is
239 * contained within object
241 * @see #isBoolean
243 static public boolean toBoolean(Object object) throws com.sun.star.lang.IllegalArgumentException{
244 Boolean ret= (Boolean)convertSimple(TypeClass.BOOLEAN, null, object);
245 return ret.booleanValue();
249 * Converts an Byte object or an Any object containing a Byte object into a
250 * simple byte.
252 * @param object the object to convert.
253 * @return the boolean contained within the object.
254 * @throws com.sun.star.lang.IllegalArgumentException in case no byte is
255 * contained within object.
257 * @see #isBoolean
259 static public byte toByte(Object object) throws com.sun.star.lang.IllegalArgumentException{
260 Byte ret= (Byte)convertSimple(TypeClass.BYTE, null, object);
261 return ret.byteValue();
265 * Converts a number object into a simple short and allows widening conversions.
267 * <p>Allowed argument types are Byte, Short or Any containing these types.</p>
269 * @param object the object to convert.
270 * @throws com.sun.star.lang.IllegalArgumentException in case no short or
271 * byte is contained within object.
273 * @return the short contained within the object.
275 static public short toShort(Object object) throws com.sun.star.lang.IllegalArgumentException{
276 Short ret= (Short)convertSimple(TypeClass.SHORT, null, object);
277 return ret.shortValue();
280 * Converts a number object into an idl unsigned short and allows widening
281 * conversions.
283 * <p>Allowed argument types are Anies containing idl unsigned short values.</p>
285 * @param object the object to convert.
286 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
287 * short is contained within Any.
289 * @return an (unsigned) short.
291 static public short toUnsignedShort(Object object)
292 throws com.sun.star.lang.IllegalArgumentException
294 Short ret= (Short)convertSimple(TypeClass.UNSIGNED_SHORT, null, object);
295 return ret.shortValue();
299 * Converts a number object into a simple int and allows widening conversions.
301 * <p>Allowed argument types are Byte, Short, Integer or Any containing these
302 * types.</p>
304 * @param object the object to convert.
305 * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte
306 * or int is contained within object.
308 * @return the int contained within the object.
310 static public int toInt(Object object) throws com.sun.star.lang.IllegalArgumentException{
311 Integer ret= (Integer) convertSimple( TypeClass.LONG, null, object);
312 return ret.intValue();
315 * Converts a number object into an idl unsigned long and allows widening
316 * conversions.
318 * <p>Allowed argument types are Anies containing idl unsigned short or
319 * unsigned long values.</p>
321 * @param object the object to convert.
322 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
323 * short nor unsigned long is contained within Any.
325 * @return an (unsigned) int.
327 static public int toUnsignedInt(Object object)
328 throws com.sun.star.lang.IllegalArgumentException
330 Integer ret = (Integer)convertSimple(TypeClass.UNSIGNED_LONG, null, object);
331 return ret.intValue();
335 * Converts a number object into a simple long and allows widening conversions.
337 * <p>Allowed argument types are Byte, Short, Integer, Long or Any containing
338 * these types.</p>
340 * @param object the object to convert.
341 * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte,
342 * int or long is contained within object.
344 * @return the long contained within the object.
346 static public long toLong(Object object) throws com.sun.star.lang.IllegalArgumentException{
347 Long ret= (Long) convertSimple( TypeClass.HYPER, null, object);
348 return ret.longValue();
351 * Converts a number object into an idl unsigned hyper and allows widening
352 * conversions.
354 * <p>Allowed argument types are Anies containing idl unsigned short, unsigned
355 * long or unsigned hyper values.</p>
357 * @param object the object to convert.
358 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
359 * short, nor unsigned long nor unsigned hyper is contained within object.
361 * @return an (unsigned) long.
363 static public long toUnsignedLong(Object object)
364 throws com.sun.star.lang.IllegalArgumentException
366 Long ret = (Long)convertSimple(TypeClass.UNSIGNED_HYPER, null, object);
367 return ret.longValue();
371 * Converts a number object into a simple float and allows widening conversions.
373 * <p>Allowed argument types are Byte, Short, Float or Any containing these
374 * types.</p>
376 * @param object the object to convert.
377 * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short
378 * or float is contained within object.
380 * @return the float contained within the object.
382 static public float toFloat(Object object) throws com.sun.star.lang.IllegalArgumentException{
383 Float ret= (Float) convertSimple( TypeClass.FLOAT,null, object);
384 return ret.floatValue();
388 * Converts a number object into a simple double and allows widening conversions.
390 * <p>Allowed argument types are Byte, Short, Int, Float, Double or Any
391 * containing these types.</p>
393 * @param object the object to convert.
394 * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short,
395 * int, float or double is contained within object.
397 * @return the double contained within the object.
399 static public double toDouble(Object object) throws com.sun.star.lang.IllegalArgumentException {
400 Double ret= (Double) convertSimple( TypeClass.DOUBLE, null, object);
401 return ret.doubleValue();
405 * Converts a string or an any containing a string into a string.
407 * @param object the object to convert.
408 * @throws com.sun.star.lang.IllegalArgumentException in case no string is
409 * contained within object.
411 * @return the string contained within the object.
413 static public String toString(Object object) throws com.sun.star.lang.IllegalArgumentException {
414 return (String) convertSimple( TypeClass.STRING, null, object);
418 * Converts a Type or an any containing a Type into a Type.
420 * @param object the object to convert.
421 * @throws com.sun.star.lang.IllegalArgumentException in case no type is
422 * contained within object.
424 * @return the type contained within the object.
426 static public Type toType(Object object) throws com.sun.star.lang.IllegalArgumentException {
427 return (Type) convertSimple( TypeClass.TYPE, null, object);
431 * Converts a UNO object (struct, exception, sequence, enum or interface) or
432 * an Any containing these types into an UNO object of a specified destination
433 * type.
435 * <p> For interfaces, the argument <em>object</em> is queried for the interface
436 * specified by the <em>type</em> argument.</p>
438 * <p>That query (UnoRuntime.queryInterface) might return null, if the interface
439 * is not implemented or a null-ref or a VOID any is given.</p>
441 * @param type type of the returned value.
442 * @param object the object that is to be converted.
443 * @throws com.sun.star.lang.IllegalArgumentException in case conversion is
444 * not possible.
446 * @return destination object.
448 static public Object toObject(Type type, Object object)
449 throws com.sun.star.lang.IllegalArgumentException
451 return convertSimple( type.getTypeClass(), type, object );
454 * Converts a UNO object (struct, exception, sequence, enum or interface) or
455 * an Any containing these types into an UNO object of a specified destination
456 * type.
458 * <p>For interfaces, the argument <em>object</em> is queried for the interface
459 * specified by the <em>type</em> argument. That query (UnoRuntime.queryInterface)
460 * might return null, if the interface is not implemented or a null-ref or a
461 * VOID any is given.</p>
463 * @param clazz class of the returned value.
464 * @param object the object that is to be converted.
465 * @throws com.sun.star.lang.IllegalArgumentException in case conversion is
466 * not possible.
468 * @return destination object.
470 static public Object toObject(Class<?> clazz, Object object)
471 throws com.sun.star.lang.IllegalArgumentException
473 return toObject( new Type( clazz ), object );
477 * Converts an array or an any containing an array into an array.
479 * @param object the object to convert.
480 * @throws com.sun.star.lang.IllegalArgumentException in case no array is
481 * contained within object.
483 * @return the array contained within the object.
485 static public Object toArray( Object object) throws com.sun.star.lang.IllegalArgumentException {
486 return convertSimple( TypeClass.SEQUENCE, null, object);
490 * Examines the argument <em>object</em> if is correspond to the type in
491 * argument <em>what</em>.
493 * <p><em>object</em> is either matched directly against the type or if it is
494 * an any then the contained object is matched against the type.</p>
496 static private boolean containsType( TypeClass what, Object object){
497 return (getType(object).getTypeClass().getValue() == what.getValue());
500 static private final Type m_XInterface_type = new Type( XInterface.class );
502 static private Object convertSimple( TypeClass destTClass, Type destType, Object object_ )
503 throws com.sun.star.lang.IllegalArgumentException
505 Object object;
506 Type type;
507 if (object_ instanceof Any) {
508 // unbox
509 Any a = (Any)object_;
510 object = a.getObject();
511 type = a.getType();
512 // nested any
513 if (TypeClass.ANY_value == type.getTypeClass().getValue())
514 return convertSimple( destTClass, destType, object );
515 } else {
516 object = object_;
517 type = (null == object ? m_XInterface_type : new Type( object.getClass() ));
520 int tc = type.getTypeClass().getValue();
521 int dest_tc = destTClass.getValue();
523 if (null == object) {
524 // special for interfaces
525 if (TypeClass.INTERFACE_value == tc && dest_tc == tc)
526 return null;
527 } else {
528 switch (dest_tc) {
529 case TypeClass.CHAR_value:
530 if (tc == TypeClass.CHAR_value)
531 return object;
532 break;
533 case TypeClass.BOOLEAN_value:
534 if (tc == TypeClass.BOOLEAN_value)
535 return object;
536 break;
537 case TypeClass.BYTE_value:
538 if (tc == TypeClass.BYTE_value)
539 return object;
540 break;
541 case TypeClass.SHORT_value:
542 switch (tc) {
543 case TypeClass.BYTE_value:
544 return Short.valueOf( ((Byte)object).byteValue() );
545 case TypeClass.SHORT_value:
546 return object;
548 break;
549 case TypeClass.UNSIGNED_SHORT_value:
550 switch (tc) {
551 case TypeClass.UNSIGNED_SHORT_value:
552 return object;
554 break;
555 case TypeClass.LONG_value:
556 switch (tc) {
557 case TypeClass.BYTE_value:
558 return Integer.valueOf( ((Byte)object).byteValue() );
559 case TypeClass.SHORT_value:
560 case TypeClass.UNSIGNED_SHORT_value:
561 return Integer.valueOf( ((Short)object).shortValue() );
562 case TypeClass.LONG_value:
563 return object;
565 break;
566 case TypeClass.UNSIGNED_LONG_value:
567 switch (tc) {
568 case TypeClass.UNSIGNED_SHORT_value:
569 return Integer.valueOf( ((Short)object).shortValue() );
570 case TypeClass.UNSIGNED_LONG_value:
571 return object;
573 break;
574 case TypeClass.HYPER_value:
575 switch (tc) {
576 case TypeClass.BYTE_value:
577 return Long.valueOf( ((Byte)object).byteValue() );
578 case TypeClass.SHORT_value:
579 case TypeClass.UNSIGNED_SHORT_value:
580 return Long.valueOf( ((Short)object).shortValue() );
581 case TypeClass.LONG_value:
582 case TypeClass.UNSIGNED_LONG_value:
583 return Long.valueOf( ((Integer)object).intValue() );
584 case TypeClass.HYPER_value:
585 return object;
587 break;
588 case TypeClass.UNSIGNED_HYPER_value:
589 switch (tc) {
590 case TypeClass.UNSIGNED_SHORT_value:
591 return Long.valueOf( ((Short)object).shortValue() );
592 case TypeClass.UNSIGNED_LONG_value:
593 return Long.valueOf( ((Integer)object).intValue() );
594 case TypeClass.UNSIGNED_HYPER_value:
595 return object;
597 break;
598 case TypeClass.FLOAT_value:
599 switch (tc) {
600 case TypeClass.BYTE_value:
601 return new Float( ((Byte)object).byteValue() );
602 case TypeClass.SHORT_value:
603 return new Float( ((Short)object).shortValue() );
604 case TypeClass.FLOAT_value:
605 return object;
607 break;
608 case TypeClass.DOUBLE_value:
609 switch (tc) {
610 case TypeClass.BYTE_value:
611 return new Double( ((Byte)object).byteValue() );
612 case TypeClass.SHORT_value:
613 return new Double( ((Short)object).shortValue() );
614 case TypeClass.LONG_value:
615 return new Double( ((Integer)object).intValue() );
616 case TypeClass.FLOAT_value:
617 return new Double( ((Float)object).floatValue() );
618 case TypeClass.DOUBLE_value:
619 return object;
621 break;
622 case TypeClass.ENUM_value:
623 if (tc == TypeClass.ENUM_value &&
624 (destType.equals( type ) /* optional destType */))
626 return object;
628 break;
629 case TypeClass.STRING_value:
630 if (tc == TypeClass.STRING_value)
631 return object;
632 break;
633 case TypeClass.TYPE_value:
634 if (tc == TypeClass.TYPE_value)
635 return object;
636 break;
637 case TypeClass.INTERFACE_value:
638 // Because object is a class, not an interface, it is
639 // controversial what kind of Type "new Type(object.class)"
640 // above should return (UNKNOWN or INTERFACE), so that we should
641 // not check here for "tc == TypeClass.INTERFACE_value".
642 // Instead, we check whether object (indirectly) derives from
643 // XInterface:
644 if (object instanceof XInterface)
645 return UnoRuntime.queryInterface( destType, object );
646 break;
647 case TypeClass.STRUCT_value:
648 case TypeClass.EXCEPTION_value:
649 if (destType.isSupertypeOf(type)) {
650 return object;
652 break;
653 case TypeClass.SEQUENCE_value:
654 if (tc == TypeClass.SEQUENCE_value &&
655 (null == destType || destType.equals( type ) /* optional destType */))
657 return object;
659 break;
662 throw new com.sun.star.lang.IllegalArgumentException(
663 "The Argument did not hold the proper type");