KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > BasicComponentInterface


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.Interface;
28 import org.objectweb.fractal.api.Type;
29
30 /**
31  * Provides a basic implementation of the {@link ComponentInterface} interface.
32  */

33
34 public abstract class BasicComponentInterface implements ComponentInterface {
35
36   /**
37    * The component to which this component interface belongs.
38    */

39
40   protected Component owner;
41
42   /**
43    * The name of this component interface.
44    */

45
46   protected String JavaDoc name;
47
48   /**
49    * The type of this component interface.
50    */

51
52   protected Type type;
53
54   /**
55    * The flags of this component interfaces. These flags indicates if this
56    * component interface is an internal interface or not, and if it has an
57    * permanently associated interceptor or not.
58    */

59
60   protected int flags;
61
62   // -------------------------------------------------------------------------
63
// Constructors
64
// -------------------------------------------------------------------------
65

66   /**
67    * Constructs an uninitialized {@link BasicComponentInterface}. This default
68    * public constructor is needed for the implementation of the {@link #clone
69    * clone} method.
70    */

71
72   public BasicComponentInterface () {
73   }
74
75   /**
76    * Constructs a new {@link BasicComponentInterface}.
77    *
78    * @param owner the component to which the interface belongs. If this
79    * parameter is <tt>null</tt> the owner is set to the interface itself,
80    * which must therefore be a {@link Component} interface.
81    * @param name the name of the interface.
82    * @param type the type of the interface.
83    * @param isInternal <tt>true</tt> if the interface is an internal interface.
84    * @param impl the object that implements the interface.
85    */

86
87   public BasicComponentInterface (
88     final Component owner,
89     final String JavaDoc name,
90     final Type type,
91     final boolean isInternal,
92     final Object JavaDoc impl)
93   {
94     boolean hasInterceptor = impl instanceof Interceptor;
95     this.owner = owner == null ? (Component)this : owner;
96     this.name = name;
97     this.type = type;
98     this.flags = (isInternal ? 0x01 : 0x00) | (hasInterceptor ? 0x02 : 0x00);
99     this.setFcItfImpl(impl);
100   }
101
102   // -------------------------------------------------------------------------
103
// Implementation of the Interface interface
104
// -------------------------------------------------------------------------
105

106   public Component getFcItfOwner () {
107     return owner;
108   }
109
110   public String JavaDoc getFcItfName () {
111     return name;
112   }
113
114   public Type getFcItfType () {
115     return type;
116   }
117
118   public boolean isFcInternalItf () {
119     return (flags & 0x01) != 0;
120   }
121
122   // -------------------------------------------------------------------------
123
// Implementation of the ComponentInterface interface
124
// -------------------------------------------------------------------------
125

126   public void setFcItfName (final String JavaDoc name) {
127     this.name = name;
128   }
129
130   public boolean hasFcInterceptor () {
131     return (flags & 0x02) != 0;
132   }
133
134   // -------------------------------------------------------------------------
135
// Overriden Object methods
136
// -------------------------------------------------------------------------
137

138   /**
139    * Returns the hash code of this component interface.
140    *
141    * @return the hash code of this component interface.
142    */

143
144   public int hashCode () {
145     return System.identityHashCode(getFcItfOwner()) * getFcItfName().hashCode();
146   }
147
148   /**
149    * Tests if the given object is equal to this component interface.
150    *
151    * @param o the object to be compared to this component interface.
152    * @return <tt>true</tt> if o is a component interface with the same name and
153    * owner as this component interface, and if both are internal (resp.
154    * external) component interfaces.
155    */

156
157   public boolean equals (final Object JavaDoc o) {
158     if (o == this) {
159       return true;
160     }
161     if (o instanceof Interface) {
162       Interface itf = (Interface)o;
163       return
164         getFcItfOwner() == itf.getFcItfOwner() && // TODO ou equals?
165
getFcItfName().equals(itf.getFcItfName()) &&
166         isFcInternalItf() == itf.isFcInternalItf();
167     }
168     return false;
169   }
170
171   public Object JavaDoc clone () {
172     // cannot use super.clone(), because the clone method is not defined in all
173
// Java platforms (such as J2ME, CLDC profile)
174
BasicComponentInterface clone;
175     try {
176       clone = (BasicComponentInterface)getClass().newInstance();
177     } catch (Exception JavaDoc e) {
178       throw new Error JavaDoc("Internal error"); // should not happen
179
}
180     clone.owner = this.owner;
181     clone.name = name;
182     clone.type = type;
183     clone.flags = flags;
184     Object JavaDoc impl = getFcItfImpl();
185     if (hasFcInterceptor()) {
186       impl = ((Interceptor)impl).clone();
187     }
188     clone.setFcItfImpl(impl);
189     return clone;
190   }
191 }
192
Popular Tags