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