1 | /* |
2 | * Holder.java |
3 | * |
4 | * Created on 14.09.2004. |
5 | * |
6 | * eaio: NativeCall - calling operating system methods from Java |
7 | * Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>) |
8 | * <http://eaio.com> |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a |
11 | * copy of this software and associated documentation files (the "Software"), |
12 | * to deal in the Software without restriction, including without limitation |
13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
14 | * and/or sell copies of the Software, and to permit persons to whom the |
15 | * Software is furnished to do so, subject to the following conditions: |
16 | * |
17 | * The above copyright notice and this permission notice shall be included |
18 | * in all copies or substantial portions of the Software. |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
23 | * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
24 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
25 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
26 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
27 | * |
28 | */ |
29 | package com.eaio.nativecall; |
30 | |
31 | /** |
32 | * Holder is a class that encapsulates another Object. Use this class for output |
33 | * parameters. |
34 | * |
35 | * @author <a href="mailto:jb@eaio.com">Johann Burkard</a> |
36 | * @version $Id: Holder.java,v 1.3 2006/04/19 20:54:58 grnull Exp $ |
37 | */ |
38 | public class Holder { |
39 | |
40 | /** |
41 | * The encapsulated object. |
42 | * <p> |
43 | * Accessed by native code. DO NOT RENAME THIS FIELD. |
44 | */ |
45 | private Object o; |
46 | |
47 | /** |
48 | * Constructor for Holder. |
49 | * |
50 | * @param o the Object to encapsulate, may be <code>null</code>, but cannot |
51 | * be of type Holder |
52 | * @throws ClassCastException if o is of type Holder |
53 | */ |
54 | public Holder(Object o) { |
55 | if (o instanceof Holder) { |
56 | throw new ClassCastException(); |
57 | } |
58 | else if (o == null) { |
59 | o = new Integer(0); |
60 | } |
61 | this.o = o; |
62 | } |
63 | |
64 | /** |
65 | * Returns the referenced Object. |
66 | * |
67 | * @return an Object |
68 | */ |
69 | public final Object get() { |
70 | return o; |
71 | } |
72 | |
73 | /** |
74 | * Returns the hashCode of the encapsulated Object or |
75 | * {@link java.lang.Object#hashCode()} if the Object is <code>null</code>. |
76 | * |
77 | * @return the hashCode |
78 | * @see java.lang.Object#hashCode() |
79 | */ |
80 | public int hashCode() { |
81 | return getClass().getName().hashCode() ^ (o == null ? 0 : o.hashCode()); |
82 | } |
83 | |
84 | /** |
85 | * Returns if this Object is equal to another Object. |
86 | * |
87 | * @param obj the other Object, may be <code>null</code> |
88 | * @return if both Objects are equal |
89 | * @see java.lang.Object#equals(java.lang.Object) |
90 | */ |
91 | public boolean equals(Object obj) { |
92 | if (this == obj) { |
93 | return true; |
94 | } |
95 | if (!(obj instanceof Holder)) { |
96 | return false; |
97 | } |
98 | Holder h = (Holder) obj; |
99 | return o == null ? h.o == null : o.equals(h.o); |
100 | } |
101 | |
102 | /** |
103 | * Returns a String representation of this Object. |
104 | * |
105 | * @return a String, never <code>null</code> |
106 | * @see java.lang.Object#toString() |
107 | * @see #toStringBuffer(StringBuffer) |
108 | */ |
109 | public final String toString() { |
110 | return toStringBuffer(null).toString(); |
111 | } |
112 | |
113 | /** |
114 | * Appends a String representation of this Object to the given |
115 | * {@link StringBuffer} or creates a new one if none is given. |
116 | * |
117 | * @param in the StringBuffer to append to, may be <code>null</code> |
118 | * @return a StringBuffer, never <code>null</code> |
119 | */ |
120 | public StringBuffer toStringBuffer(StringBuffer in) { |
121 | if (in == null) { |
122 | in = new StringBuffer(32); |
123 | } |
124 | else { |
125 | in.ensureCapacity(in.length() + 32); |
126 | } |
127 | in.append("{ Holder: o = "); |
128 | in.append(o); |
129 | in.append(" }"); |
130 | return in; |
131 | } |
132 | |
133 | } |