xref: /aoo41x/main/ridljar/com/sun/star/uno/Any.java (revision a046d00f)
1*a046d00fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a046d00fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a046d00fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a046d00fSAndrew Rist  * distributed with this work for additional information
6*a046d00fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a046d00fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a046d00fSAndrew Rist  * "License"); you may not use this file except in compliance
9*a046d00fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a046d00fSAndrew Rist  *
11*a046d00fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a046d00fSAndrew Rist  *
13*a046d00fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a046d00fSAndrew Rist  * software distributed under the License is distributed on an
15*a046d00fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a046d00fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a046d00fSAndrew Rist  * specific language governing permissions and limitations
18*a046d00fSAndrew Rist  * under the License.
19*a046d00fSAndrew Rist  *
20*a046d00fSAndrew Rist  *************************************************************/
21*a046d00fSAndrew Rist 
22*a046d00fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir package com.sun.star.uno;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /**
29cdf0e10cSrcweir  * The UNO IDL type any is mapped to java type <code>java.lang.Object</code>.
30cdf0e10cSrcweir  * <p>
31cdf0e10cSrcweir  * In special cases it is necessary to have an explicit any to additionally transport
32cdf0e10cSrcweir  * an exact type. For instance if you want to pass an object reference via
33cdf0e10cSrcweir  * an interprocess connection using an any, you should use this class to add
34cdf0e10cSrcweir  * an explicit interface type, so the remote counterpart doesn't need to invoke
35cdf0e10cSrcweir  * a queryInterface).
36cdf0e10cSrcweir  * <p>
37cdf0e10cSrcweir  * @version 	$Revision: 1.11 $ $ $Date: 2008-04-11 11:11:43 $
38cdf0e10cSrcweir  */
39cdf0e10cSrcweir public class Any {
40cdf0e10cSrcweir 	/**
41cdf0e10cSrcweir 	 * The type of the any.
42cdf0e10cSrcweir 	 * <p>
43cdf0e10cSrcweir 	 * @see #getType
44cdf0e10cSrcweir 	 */
45cdf0e10cSrcweir 	protected Type  _type;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	/**
48cdf0e10cSrcweir 	 * The data of the any.
49cdf0e10cSrcweir 	 * <p>
50cdf0e10cSrcweir 	 * @see #getObject
51cdf0e10cSrcweir 	 */
52cdf0e10cSrcweir 	protected Object _object;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     public static final Any VOID = new Any(new Type("void", TypeClass.VOID),
55cdf0e10cSrcweir                                            null);
56cdf0e10cSrcweir         // do not use Type.VOID here to avoid circular dependencies between
57cdf0e10cSrcweir         // static members of Any and Type
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	/**
60cdf0e10cSrcweir 	 * Constructs a new any.
61cdf0e10cSrcweir 	 * <p>
62cdf0e10cSrcweir 	 * @param   zInterface  the type of the any.
63cdf0e10cSrcweir 	 * @param   object      the data of the any.
64cdf0e10cSrcweir 	 * @deprecated as of UDK 2.0
65cdf0e10cSrcweir 	 */
Any(Class zInterface, Object object)66cdf0e10cSrcweir 	public Any(Class zInterface, Object object) {
67cdf0e10cSrcweir         this(new Type(zInterface), object);
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /** Constructs a new any with a given type and value
71cdf0e10cSrcweir         @param type the UNO type of the any.
72cdf0e10cSrcweir         @param object the value of the any.
73cdf0e10cSrcweir      */
Any(Type type, Object object)74cdf0e10cSrcweir 	public Any(Type type, Object object) {
75cdf0e10cSrcweir         if (type.equals(Type.ANY)) {
76cdf0e10cSrcweir             throw new IllegalArgumentException("Any cannot contain Any");
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir 		_type   = type;
79cdf0e10cSrcweir 		_object = object;
80cdf0e10cSrcweir 	}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     /**
83cdf0e10cSrcweir        Complete a UNO <code>ANY</code> (make sure it is wrapped up as an
84cdf0e10cSrcweir        <code>Any</code> instance).
85cdf0e10cSrcweir 
86cdf0e10cSrcweir        @param any a Java value representing a UNO <code>ANY</code> value.
87cdf0e10cSrcweir 
88cdf0e10cSrcweir        @return a complete Java value (that is, an <code>Any</code> instance)
89cdf0e10cSrcweir        representing the same UNO <code>ANY</code> value as the given argument.
90cdf0e10cSrcweir 
91cdf0e10cSrcweir        @since UDK 3.2.3
92cdf0e10cSrcweir     */
complete(Object any)93cdf0e10cSrcweir     public static final Any complete(Object any) {
94cdf0e10cSrcweir         return any instanceof Any
95cdf0e10cSrcweir             ? (Any) any
96cdf0e10cSrcweir             : new Any(
97cdf0e10cSrcweir                 new Type(any == null ? XInterface.class : any.getClass()), any);
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	/**
101cdf0e10cSrcweir 	 * Gets the type of the value within the any.
102cdf0e10cSrcweir 	 * <p>
103cdf0e10cSrcweir 	 * @return   the type of the value within the any.
104cdf0e10cSrcweir 	 */
getType()105cdf0e10cSrcweir 	public Type getType() {
106cdf0e10cSrcweir 		return _type;
107cdf0e10cSrcweir 	}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	/**
110cdf0e10cSrcweir 	 * Gets the value within the any.
111cdf0e10cSrcweir 	 * <p>
112cdf0e10cSrcweir 	 * @return   gets the value within the any.
113cdf0e10cSrcweir 	 */
getObject()114cdf0e10cSrcweir 	public Object getObject() {
115cdf0e10cSrcweir 		return _object;
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     // @see java.lang.Object#equals
equals(Object obj)119cdf0e10cSrcweir     public boolean equals(Object obj) {
120cdf0e10cSrcweir         return obj instanceof Any && _type.equals(((Any) obj)._type)
121cdf0e10cSrcweir             && (_object == null
122cdf0e10cSrcweir                 ? ((Any) obj)._object == null
123cdf0e10cSrcweir                 : _object.equals(((Any) obj)._object));
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     // @see java.lang.Object#hashCode
hashCode()127cdf0e10cSrcweir     public int hashCode() {
128cdf0e10cSrcweir         return _type.hashCode() * 13
129cdf0e10cSrcweir             + (_object == null ? 0 : _object.hashCode());
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     // @see java.lang.Object#toString
toString()133cdf0e10cSrcweir     public String toString() {
134cdf0e10cSrcweir         return "Any[" + _type + ", " + _object + "]";
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir }
137