KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > ref > lib > ReferenceBase


1 /*====================================================================
2
3 ObjectWeb Util Ref Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 --------------------------------------------------------------------
26 $Id: ReferenceBase.java,v 1.1 2004/02/13 17:46:08 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.ref.lib;
30
31 import org.objectweb.util.ref.api.Referencable;
32 import org.objectweb.util.ref.api.Reference;
33
34 /**
35  * Base implementation of Reference objects.
36  *
37  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
38  * @version 0.1
39  */

40 public class ReferenceBase
41   implements Reference
42 {
43     // ==================================================================
44
//
45
// Internal state.
46
//
47
// ==================================================================
48

49     /** The owner of the reference. */
50     private Referencable owner_;
51     
52     /** The target of the reference. */
53     private Referencable target_;
54
55     // ==================================================================
56
//
57
// Constructor.
58
//
59
// ==================================================================
60

61     /** The default constructor. */
62     public ReferenceBase() {
63         // Inits internal state.
64
owner_ = null;
65         target_ = null;
66     }
67
68     /**
69      * The constructor with only the owner of the reference.
70      *
71      * @param owner The owner of the reference.
72      */

73     public ReferenceBase(Referencable owner) {
74         // Inits internal state.
75
owner_ = owner;
76         target_ = null;
77     }
78
79     /**
80      * The constructor with both the owner and the target of the reference.
81      *
82      * @param owner The owner of the reference.
83      * @param target The target of the reference.
84      */

85     public ReferenceBase(Referencable owner, Referencable target) {
86         // Inits internal state.
87
owner_ = owner;
88         target_ = null;
89         setTarget(target);
90     }
91
92     // ==================================================================
93
//
94
// Internal methods.
95
//
96
// ==================================================================
97

98     // ==================================================================
99
//
100
// Public methods for interface org.objectweb.util.ref.api.Reference
101
//
102
// ==================================================================
103

104     /**
105      * Obtains the owner of the reference.
106      *
107      * @return The owner of the reference.
108      */

109     public Referencable getOwner() {
110         return owner_;
111     }
112
113     /**
114      * Sets the owner of the reference.
115      *
116      * @param ref The owner of the reference.
117      */

118     public void setOwner(Referencable ref) {
119         owner_ = ref;
120     }
121
122     /**
123      * Obtains the target of the reference.
124      *
125      * @return The target of the reference.
126      */

127     public Referencable getTarget() {
128         return target_;
129     }
130
131     /**
132      * Sets the target of the reference.
133      *
134      * @param ref The target of the reference.
135      */

136     public void setTarget(Referencable ref) {
137         // Does nothing if there is no change.
138
if (ref == target_) return;
139         
140         // Cuts the previous dependency.
141
cutIt();
142
143         // Creates the new dependency.
144
target_ = ref;
145         target_.addReferencedBy(owner_);
146     }
147
148     /**
149      * Cuts the reference.
150      */

151     public void cutIt() {
152         if (target_ != null) {
153             // Cuts the previous dependency.
154
target_.removeReferencedBy(owner_);
155             target_ = null;
156         }
157     }
158 }
159
Popular Tags