KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > Variant


1 package com4j;
2
3 import java.nio.ByteBuffer JavaDoc;
4 import java.nio.ByteOrder JavaDoc;
5
6
7 /**
8  * Wraps COM VARIANT data structure.
9  *
10  * This class allows you to deal with the raw VARIANT type in case you need it,
11  * but in general you should bind <tt>VARIANT*</tt> to {@link Object} or
12  * {@link Holder<Object>} for more natural Java binding.
13  *
14  * TODO: more documentation.
15  *
16  * <h2>Notes</h2>
17  * <ol>
18  * <li>
19  * Calling methods defined on {@link Number} changes the variant
20  * type (i.e., similar to a cast in Java) accordingly and returns its value.
21  * </ol>
22  *
23  * <p>
24  * Method names that end with '0' are native methods.
25  *
26  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
27  */

28 public final class Variant extends Number JavaDoc {
29     /**
30      * The memory image of the VARIANT.
31      */

32     final ByteBuffer JavaDoc image = ByteBuffer.allocateDirect(16);
33
34     /**
35      * VARIANT type.
36      *
37      * This enum only defines constants that are legal for VARIANTs.
38      */

39     public static enum Type implements ComEnum {
40         VT_EMPTY(0),
41         VT_NULL(1),
42         VT_I2(2),
43         VT_I4(3),
44         VT_R4(4),
45         VT_R8(5),
46         VT_CY(6),
47         VT_DATE(7),
48         VT_BSTR(8),
49         VT_DISPATCH(9),
50         VT_ERROR(10),
51         VT_BOOL(11),
52         VT_VARIANT(12),
53         VT_UNKNOWN(13),
54         VT_DECIMAL(14),
55         VT_RECORD(36),
56         VT_I1(16),
57         VT_UI1(17),
58         VT_UI2(18),
59         VT_UI4(19),
60         VT_INT(22),
61         VT_UINT(23),
62 // VT_ARRAY(),
63
// VT_BYREF
64
;
65
66         private final int value;
67
68         private Type( int value ) {
69             this.value = value;
70         }
71
72         public int comEnumValue() {
73             return value;
74         }
75     }
76
77     /**
78      * Creates an empty {@link Variant}.
79      */

80     public Variant() {
81         image.order(ByteOrder.LITTLE_ENDIAN);
82     }
83
84     /**
85      * Empties the current contents.
86      *
87      * <p>
88      * Sometimes a {@link Variant} holds things like interface pointers or
89      * arrays, which require some clean up actions. Therefore, when you
90      * want to reuse an existing {@link Variant} that may hold a value,
91      * you should first clear it.
92      */

93     public void clear() {
94         clear0(image);
95     }
96
97     /**
98      * Makes sure the variant is cleared before GC-ed.
99      */

100     public void finalize() {
101         clear();
102     }
103
104     /**
105      * Calls <tt>VariantClear</tt> method.
106      */

107     private static native void clear0( ByteBuffer JavaDoc image );
108
109     /**
110      * Sets the type of the variant.
111      */

112     public void setType( Type t ) {
113         image.putLong(0,t.comEnumValue());
114     }
115
116     /**
117      * Gets the type of the variant.
118      */

119     public Type getType() {
120         return EnumDictionary.get(Type.class).constant((int)image.getLong(0));
121     }
122
123
124     private static native void changeType0( int type, ByteBuffer JavaDoc image );
125
126     /**
127      * Changes the variant type to the specified one.
128      */

129     private void changeType( Type t ) {
130         changeType0( t.comEnumValue(), image );
131     }
132
133     public int intValue() {
134         changeType(Type.VT_I4);
135         return image.getInt(8);
136     }
137
138     public long longValue() {
139         // VARIANT doesn't seem to support 64bit int
140
return intValue();
141     }
142
143     public float floatValue() {
144         changeType(Type.VT_R4);
145         return image.getFloat(8);
146     }
147
148     public double doubleValue() {
149         changeType(Type.VT_R8);
150         return image.getDouble(8);
151     }
152
153     public <T extends Com4jObject> T object( Class JavaDoc<T> type ) {
154         changeType(Type.VT_UNKNOWN);
155         int ptr = image.getInt(8);
156         if(ptr==0) return null;
157         Native.addRef(ptr);
158         return Wrapper.create(type,ptr);
159     }
160 }
161
Popular Tags