KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > type > BasicInterfaceType


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.type;
25
26 import org.objectweb.fractal.api.Type;
27 import org.objectweb.fractal.api.type.InterfaceType;
28
29 import java.io.Serializable JavaDoc;
30
31 /**
32  * Provides a basic implementation of the {@link InterfaceType} interface.
33  */

34
35 public class BasicInterfaceType implements InterfaceType, Serializable JavaDoc {
36
37   /**
38    * The {@link #flags flags} bit that indicates a client interface.
39    */

40
41   private final static int CLIENT_FLAG = 1;
42
43   /**
44    * The {@link #flags flags} bit that indicates an optional interface.
45    */

46
47   private final static int OPTIONAL_FLAG = 2;
48
49   /**
50    * The {@link #flags flags} bit that indicates a collection interface.
51    */

52
53   private final static int COLLECTION_FLAG = 4;
54
55   /**
56    * The name of the interface described by this type.
57    */

58
59   private String JavaDoc name;
60
61   /**
62    * The Java type of the interface described by this type.
63    */

64
65   private String JavaDoc signature;
66
67   /**
68    * Indicates if the interface described by this type is a client interface,
69    * if it is optional...
70    */

71
72   private int flags;
73
74   // -------------------------------------------------------------------------
75
// Constructors
76
// -------------------------------------------------------------------------
77

78   /**
79    * Constructs a new {@link BasicInterfaceType} object.
80    *
81    * @param name the name of the described interface.
82    * @param signature the fully qualified name of the Java type of the described
83    * interface, or a Class object.
84    * @param isClient <tt>true</tt> if the described interface is a client
85    * interface.
86    * @param isOptional <tt>true</tt> if the described interface is an
87    * optional interface.
88    * @param isCollection <tt>true</tt> if the described interface is a
89    * collection interface.
90    */

91
92   public BasicInterfaceType (
93     final String JavaDoc name,
94     final String JavaDoc signature,
95     final boolean isClient,
96     final boolean isOptional,
97     final boolean isCollection)
98   {
99     int flags = 0;
100     flags = (isClient ? flags | CLIENT_FLAG : flags);
101     flags = (isOptional ? flags | OPTIONAL_FLAG : flags);
102     flags = (isCollection ? flags | COLLECTION_FLAG : flags);
103     this.name = name;
104     this.signature = signature;
105     this.flags = flags;
106   }
107
108   // -------------------------------------------------------------------------
109
// Implementation of the InterfaceType interface
110
// -------------------------------------------------------------------------
111

112   public String JavaDoc getFcItfName () {
113     return name;
114   }
115
116   public String JavaDoc getFcItfSignature () {
117     return signature;
118   }
119
120   public boolean isFcClientItf () {
121     return (flags & CLIENT_FLAG) != 0;
122   }
123
124   public boolean isFcOptionalItf () {
125     return (flags & OPTIONAL_FLAG) != 0;
126   }
127
128   public boolean isFcCollectionItf () {
129     return (flags & COLLECTION_FLAG) != 0;
130   }
131
132   public boolean isFcSubTypeOf (final Type type) {
133     if (type instanceof InterfaceType) {
134       InterfaceType t = (InterfaceType)type;
135       // role
136
if (t.isFcClientItf() != isFcClientItf()) {
137         return false;
138       }
139       // name
140
if (!t.getFcItfName().equals(getFcItfName())) {
141         return false;
142       }
143       // cardinality
144
if (t.isFcCollectionItf() && !isFcCollectionItf()) {
145         return false;
146       }
147       // contingency
148
if (isFcClientItf()) {
149         if (t.isFcOptionalItf() && !isFcOptionalItf()) {
150           return false;
151         }
152       } else {
153         if (!t.isFcOptionalItf() && isFcOptionalItf()) {
154           return false;
155         }
156       }
157       // signature
158
if (t.getFcItfSignature().equals(getFcItfSignature())) {
159         return true;
160       }
161       if (t instanceof BasicInterfaceType) {
162         try {
163           Class JavaDoc c1 = Class.forName(signature);
164           Class JavaDoc c2 = Class.forName(((BasicInterfaceType)t).signature);
165           if (c1 != null && c2 != null && isFcClientItf()) {
166             return c1.isAssignableFrom(c2);
167           } else {
168             return c2.isAssignableFrom(c1);
169           }
170         } catch (ClassNotFoundException JavaDoc e) {
171           return true;
172         }
173       }
174     }
175     return false;
176   }
177
178   // -------------------------------------------------------------------------
179
// Overriden Object methods
180
// -------------------------------------------------------------------------
181

182   /**
183    * Tests if this interface type is equal to the given object.
184    *
185    * @param o the object to be compared to this interface type.
186    * @return <tt>true</tt> if this interface type is equal to the given object.
187    */

188
189   public boolean equals (final Object JavaDoc o) {
190     if (o instanceof InterfaceType) {
191       InterfaceType type = (InterfaceType)o;
192       if (getFcItfName().equals(type.getFcItfName()) &&
193         isFcClientItf() == type.isFcClientItf() &&
194         isFcOptionalItf() == type.isFcOptionalItf() &&
195         isFcCollectionItf() == type.isFcCollectionItf())
196       {
197         if (getFcItfName().equals("attribute-controller") ||
198             getFcItfSignature().equals(type.getFcItfSignature()))
199         {
200           return true;
201         }
202       }
203     }
204     return false;
205   }
206 }
207
Popular Tags