KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > ObjectRef


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.registry;
21
22
23
24 /**
25  * Identification of the object bound in a context. Object is identified
26  * by the context in which it is bound and by its binding name.
27  *
28  * <p>ObjectRef instance can be stored to Context by method
29  * {@link Context#putObject}. There are two options for retrieval.
30  * Calling {@link Context#getRef} method on bound ObjectRef instance returns
31  * that instance. Calling {@link Context#getObject}, however, will
32  * recursively dereference ObjectRef instance and return directly object the
33  * ObjectRef points to. If ObjectRef is invalid or referenced binding does not
34  * exist the ObjectRef instance is returned.
35  *
36  * @author David Konecny
37  */

38 public final class ObjectRef {
39
40     private Context ctx;
41     private String JavaDoc bindingName;
42     private String JavaDoc ctxName;
43     
44
45     private ObjectRef(Context rootCtx, Context ctx, String JavaDoc ctxName, String JavaDoc bindingName) {
46         this.bindingName = bindingName;
47         
48         this.ctx = ctx;
49         this.ctxName = ctxName;
50         
51         if (this.ctx != null && this.ctxName == null) {
52             this.ctxName = ctx.getAbsoluteContextName();
53         }
54         if (this.ctx == null && this.ctxName != null && rootCtx != null) {
55             this.ctx = rootCtx.getSubcontext(ctxName.substring(1));
56         }
57     }
58
59     /**
60      * Constructs a new instance of <tt>ObjectRef</tt>.
61      * @param rootContext root context. See {@link Context#getRootContext}
62      * @param absoluteContextName absolute name of context relative to root context. See {@link Context#getAbsoluteContextName}
63      * @param bindingName name of binding
64      * @since 1.7
65      */

66     public ObjectRef(Context rootContext, String JavaDoc absoluteContextName, String JavaDoc bindingName) {
67         this (rootContext, null, absoluteContextName, bindingName);
68     }
69
70     /**
71      * Constructs a new <tt>ObjectRef</tt>.
72      * @param context context
73      * @param bindingName name of binding
74      * @since 1.7
75      */

76     public ObjectRef(Context context, String JavaDoc bindingName) {
77         this (null, context, null, bindingName);
78     }
79     
80     /**
81      * Context in which the object is bound.
82      *
83      * @return context
84      */

85     public Context getContext() {
86         return ctx;
87     }
88
89     /**
90      * Absolute context name of the context in which the
91      * object is bound.
92      *
93      */

94     public String JavaDoc getContextAbsoluteName() {
95         return ctxName;
96     }
97
98     /**
99      * Binding name under which is this object bound.
100      *
101      * @return binding name
102      */

103     public String JavaDoc getBindingName() {
104         return bindingName;
105     }
106
107     /**
108      * Getter for the object referenced by this instance.
109      *
110      * @return object or null if the object cannot be retrieved or is invalid
111      */

112     public Object JavaDoc getObject() {
113         if (isValid()) {
114             return getContext().getObject(getBindingName(), null);
115         } else {
116             return null;
117         }
118     }
119
120     /**
121      * Is the object reference valid?
122      *
123      * @return true if context exists
124      */

125     public boolean isValid() {
126         return ctx != null;
127     }
128
129     public boolean equals(Object JavaDoc o) {
130         if (o == this) {
131             return true;
132         }
133         if (!(o instanceof ObjectRef)) {
134             return false;
135         }
136         ObjectRef or = (ObjectRef)o;
137         return (this.ctx == or.ctx &&
138             this.bindingName.equals(or.bindingName) &&
139             this.ctxName.equals(or.ctxName));
140     }
141     
142     public int hashCode() {
143         int result = 7;
144         result = 31*result + ctx.hashCode();
145         result = 31*result + bindingName.hashCode();
146         result = 31*result + ctxName.hashCode();
147         return result;
148     }
149     
150     public String JavaDoc toString() {
151         return "ObjectRef [context="+getContext()+", ctxName="+
152             ctxName+", object="+getObject()+"] " + super.toString(); // NOI18N
153
}
154     
155 }
156
Popular Tags