001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.tck.util;
027:
028: import java.lang.reflect.Member;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: /**
033: *
034: *
035: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
036: * @version $Id: MemberInvocation.java,v 1.2 2004/03/23 13:37:54 lsimons Exp $
037: */
038: public class MemberInvocation {
039: // ----------------------------------------------------------------------
040: // Properties
041: // ----------------------------------------------------------------------
042:
043: protected Object m_proxy;
044: protected Object m_target;
045: protected Member m_member;
046: protected Object[] m_args;
047: protected Object m_result;
048: protected Throwable m_throwable;
049:
050: protected Map m_invocationContext;
051:
052: // ----------------------------------------------------------------------
053: // Constructors
054: // ----------------------------------------------------------------------
055:
056: /**
057: * Create an empty invocation instance.
058: */
059: public MemberInvocation() {
060: m_invocationContext = new HashMap();
061: }
062:
063: /**
064: * Create a fully populated invocation instance.
065: *
066: * @param target the actual recipient of the invocation
067: * @param proxy the proxy around the target
068: * @param method the method to call on the target
069: * @param args the arguments to the method
070: */
071: public MemberInvocation(final Object target, final Object proxy,
072: final Member method, final Object[] args) {
073: this ();
074: m_target = target;
075: m_proxy = proxy;
076: m_member = method;
077: m_args = args;
078: }
079:
080: // ----------------------------------------------------------------------
081: // Members
082: // ----------------------------------------------------------------------
083:
084: /**
085: * Recycle the invocation.
086: *
087: * @see org.jicarilla.lang.Recyclable#recycle()
088: */
089: public final void recycle() {
090: m_target = null;
091: m_proxy = null;
092: m_member = null;
093: m_args = null;
094: m_result = null;
095: m_throwable = null;
096:
097: if (m_invocationContext != null) {
098: try {
099: m_invocationContext.clear();
100: } catch (UnsupportedOperationException uoe) {
101: m_invocationContext = new HashMap();
102: }
103: } else {
104: m_invocationContext = new HashMap();
105: }
106: }
107:
108: /**
109: * Get the proxy around the target.
110: *
111: * @return the proxy
112: */
113: public final Object getProxy() {
114: return m_proxy;
115: }
116:
117: /**
118: * Set the proxy around the target.
119: *
120: * @param proxy the proxy
121: */
122: public final void setProxy(final Object proxy) {
123: m_proxy = proxy;
124: }
125:
126: /**
127: * Get the recipient of the invocation.
128: *
129: * @return the recipient
130: */
131: public final Object getTarget() {
132: return m_target;
133: }
134:
135: /**
136: * Set the recipient of the invocation.
137: *
138: * @param target the recipient
139: */
140: public final void setTarget(final Object target) {
141: m_target = target;
142: }
143:
144: /**
145: * Get the method to invoke on the recipient.
146: *
147: * @return the method to invoke
148: */
149: public final Member getMember() {
150: return m_member;
151: }
152:
153: /**
154: * Set the method to invoke on the recipient.
155: *
156: * @param method the method to invoke
157: */
158: public final void setMember(final Member method) {
159: m_member = method;
160: }
161:
162: /**
163: * Get the arguments to the method.
164: *
165: * @return the arguments
166: */
167: public final Object[] getArgs() {
168: return m_args;
169: }
170:
171: /**
172: * Set the arguments to the method.
173: *
174: * @param args the arguments
175: */
176: public final void setArgs(final Object[] args) {
177: m_args = args;
178: }
179:
180: /**
181: * Get the result of the invocation.
182: *
183: * @return the result
184: */
185: public final Object getResult() {
186: return m_result;
187: }
188:
189: /**
190: * Set the result of the invocation.
191: *
192: * @param result the result
193: */
194: public final void setResult(final Object result) {
195: m_result = result;
196: }
197:
198: /**
199: * Get the exception or error caused by the invocation.
200: *
201: * @return the exception or error
202: */
203: public final Throwable getThrowable() {
204: return m_throwable;
205: }
206:
207: /**
208: * Set the exception or error caused by the invocation.
209: *
210: * @param throwable the exception or error
211: */
212: public final void setThrowable(final Throwable throwable) {
213: m_throwable = throwable;
214: }
215:
216: /**
217: * Get the map of arbitrary values associated with this invocation, the
218: * invocation context.
219: *
220: * @return the map containing the invocation context
221: */
222: public final Map getInvocationContext() {
223: return m_invocationContext;
224: }
225:
226: /**
227: * Set the map of arbitrary values associated with this invocation, the
228: * invocation context.
229: *
230: * @param invocationContext the map containing the invocation context
231: */
232: public final void setInvocationContext(final Map invocationContext) {
233: m_invocationContext = invocationContext;
234: }
235:
236: // ----------------------------------------------------------------------
237: // equals, hashcode, clone
238: // ----------------------------------------------------------------------
239:
240: /**
241: * Compares objects for equality.
242: *
243: * @param object the object to test against this one
244: *
245: * @return true if the two objects are equal, false otherwise
246: * @see java.lang.Object#equals(Object)
247: */
248: public boolean equals(final Object object) {
249: if (object == null) {
250: return false;
251: }
252: if (super .equals(object)) {
253: return true;
254: }
255:
256: if (!(object instanceof MemberInvocation)) {
257: return false;
258: }
259:
260: final MemberInvocation other = (MemberInvocation) object;
261:
262: if (equalField(this .getArgs(), other.getArgs())
263: && equalField(this .getInvocationContext(), other
264: .getInvocationContext())
265: && equalField(this .getMember(), other.getMember())
266: && equalField(this .getProxy(), other.getProxy())
267: && equalField(this .getResult(), other.getResult())
268: && equalField(this .getTarget(), other.getTarget())
269: && equalField(this .getThrowable(), other.getThrowable())) {
270: return true;
271: }
272:
273: return false;
274: }
275:
276: /**
277: * Test whether two fields are the same. A helper method for equals().
278: *
279: * @param thisF the first field to test
280: * @param otherF the second field to test
281: *
282: * @return true if the arguments are equal, false otherwise
283: */
284: protected boolean equalField(final Object this F, final Object otherF) {
285: if (otherF == null) {
286: if (this F != null) {
287: return false;
288: }
289: } else {
290: if (this F == null) {
291: return false;
292: }
293: if (!this F.equals(otherF)) {
294: return false;
295: }
296: }
297: return true;
298: }
299:
300: /**
301: * Return a code for placing the invocation in a hash.
302: *
303: * @return a code hash for use in hash mapping
304: * @see Object#hashCode()
305: */
306: public int hashCode() {
307: int code = 0;
308:
309: if (m_proxy != null) {
310: code = m_proxy.hashCode();
311: }
312: if (m_target != null) {
313: code = code | m_proxy.hashCode();
314: }
315: if (m_member != null) {
316: code = code | m_member.hashCode();
317: }
318: if (m_args != null) {
319: code = code | m_args.hashCode();
320: }
321: if (m_result != null) {
322: code = code | m_result.hashCode();
323: }
324: if (m_throwable != null) {
325: code = code | m_throwable.hashCode();
326: }
327:
328: return code;
329: }
330: }
|