xref: /aoo41x/main/ridljar/com/sun/star/uno/Any.java (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 package com.sun.star.uno;
30 
31 
32 /**
33  * The UNO IDL type any is mapped to java type <code>java.lang.Object</code>.
34  * <p>
35  * In special cases it is necessary to have an explicit any to additionally transport
36  * an exact type. For instance if you want to pass an object reference via
37  * an interprocess connection using an any, you should use this class to add
38  * an explicit interface type, so the remote counterpart doesn't need to invoke
39  * a queryInterface).
40  * <p>
41  * @version 	$Revision: 1.11 $ $ $Date: 2008-04-11 11:11:43 $
42  */
43 public class Any {
44 	/**
45 	 * The type of the any.
46 	 * <p>
47 	 * @see #getType
48 	 */
49 	protected Type  _type;
50 
51 	/**
52 	 * The data of the any.
53 	 * <p>
54 	 * @see #getObject
55 	 */
56 	protected Object _object;
57 
58     public static final Any VOID = new Any(new Type("void", TypeClass.VOID),
59                                            null);
60         // do not use Type.VOID here to avoid circular dependencies between
61         // static members of Any and Type
62 
63 	/**
64 	 * Constructs a new any.
65 	 * <p>
66 	 * @param   zInterface  the type of the any.
67 	 * @param   object      the data of the any.
68 	 * @deprecated as of UDK 2.0
69 	 */
70 	public Any(Class zInterface, Object object) {
71         this(new Type(zInterface), object);
72 	}
73 
74     /** Constructs a new any with a given type and value
75         @param type the UNO type of the any.
76         @param object the value of the any.
77      */
78 	public Any(Type type, Object object) {
79         if (type.equals(Type.ANY)) {
80             throw new IllegalArgumentException("Any cannot contain Any");
81         }
82 		_type   = type;
83 		_object = object;
84 	}
85 
86     /**
87        Complete a UNO <code>ANY</code> (make sure it is wrapped up as an
88        <code>Any</code> instance).
89 
90        @param any a Java value representing a UNO <code>ANY</code> value.
91 
92        @return a complete Java value (that is, an <code>Any</code> instance)
93        representing the same UNO <code>ANY</code> value as the given argument.
94 
95        @since UDK 3.2.3
96     */
97     public static final Any complete(Object any) {
98         return any instanceof Any
99             ? (Any) any
100             : new Any(
101                 new Type(any == null ? XInterface.class : any.getClass()), any);
102     }
103 
104 	/**
105 	 * Gets the type of the value within the any.
106 	 * <p>
107 	 * @return   the type of the value within the any.
108 	 */
109 	public Type getType() {
110 		return _type;
111 	}
112 
113 	/**
114 	 * Gets the value within the any.
115 	 * <p>
116 	 * @return   gets the value within the any.
117 	 */
118 	public Object getObject() {
119 		return _object;
120 	}
121 
122     // @see java.lang.Object#equals
123     public boolean equals(Object obj) {
124         return obj instanceof Any && _type.equals(((Any) obj)._type)
125             && (_object == null
126                 ? ((Any) obj)._object == null
127                 : _object.equals(((Any) obj)._object));
128     }
129 
130     // @see java.lang.Object#hashCode
131     public int hashCode() {
132         return _type.hashCode() * 13
133             + (_object == null ? 0 : _object.hashCode());
134     }
135 
136     // @see java.lang.Object#toString
137     public String toString() {
138         return "Any[" + _type + ", " + _object + "]";
139     }
140 }
141