KickJava   Java API By Example, From Geeks To Geeks.

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


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.NoSuchInterfaceException;
27 import org.objectweb.fractal.api.Type;
28 import org.objectweb.fractal.api.factory.InstantiationException;
29 import org.objectweb.fractal.api.type.ComponentType;
30 import org.objectweb.fractal.api.type.InterfaceType;
31
32 import org.objectweb.fractal.julia.ChainedNoSuchInterfaceException;
33 import org.objectweb.fractal.julia.factory.ChainedInstantiationException;
34
35 import java.io.Serializable JavaDoc;
36
37 /**
38  * Provides a basic implementation of the {@link ComponentType} interface.
39  */

40
41 public class BasicComponentType implements ComponentType, Serializable JavaDoc {
42
43   /**
44    * The types of the interfaces of components of this type.
45    */

46
47   private final InterfaceType[] interfaceTypes;
48
49   // -------------------------------------------------------------------------
50
// Constructors
51
// -------------------------------------------------------------------------
52

53   /**
54    * Constructs a {@link BasicComponentType} object.
55    *
56    * @param itfTypes the types of the interfaces of components of this type.
57    * @throws InstantiationException if two interface types have the same name,
58    * or if the name of an interface type is a prefix of the name of a
59    * collection interface type.
60    */

61
62   public BasicComponentType (final InterfaceType[] itfTypes)
63     throws InstantiationException JavaDoc
64   {
65     interfaceTypes = clone(itfTypes);
66     // verifications
67
for (int i = 0; i < interfaceTypes.length; ++i) {
68       String JavaDoc p = interfaceTypes[i].getFcItfName();
69       boolean collection = interfaceTypes[i].isFcCollectionItf();
70       for (int j = i + 1; j < interfaceTypes.length; ++j) {
71         String JavaDoc q = interfaceTypes[j].getFcItfName();
72         if (p.equals(q)) {
73           throw new ChainedInstantiationException(
74             null, null, "Two interfaces have the same name '" + q + "'");
75         }
76         if (collection && q.startsWith(p)) {
77           throw new ChainedInstantiationException(
78             null,
79             null,
80             "The name of the interface '" + q + "' starts with '" +
81             p + "', which is the name of a collection interface");
82         }
83         if (interfaceTypes[j].isFcCollectionItf() && p.startsWith(q)) {
84           throw new ChainedInstantiationException(
85             null,
86             null,
87             "The name of the interface '" + p + "' starts with '" +
88             q + "', which is the name of a collection interface");
89         }
90       }
91     }
92   }
93
94   // -------------------------------------------------------------------------
95
// Implementation of the ComponentType interface
96
// -------------------------------------------------------------------------
97

98   public InterfaceType[] getFcInterfaceTypes () {
99     return clone(interfaceTypes);
100   }
101
102   public InterfaceType getFcInterfaceType (final String JavaDoc name)
103     throws NoSuchInterfaceException
104   {
105     for (int i = 0; i < interfaceTypes.length; ++i) {
106       InterfaceType itfType = interfaceTypes[i];
107       String JavaDoc itfName = itfType.getFcItfName();
108       if (itfType.isFcCollectionItf()) {
109         if (name.startsWith(itfName)) {
110           return itfType;
111         }
112       } else if (name.equals(itfName)) {
113         return itfType;
114       }
115     }
116     throw new ChainedNoSuchInterfaceException(null, null, name);
117   }
118
119   public boolean isFcSubTypeOf (final Type type) {
120     try {
121       if (type instanceof ComponentType) {
122         ComponentType t = (ComponentType)type;
123         InterfaceType[] itfs = interfaceTypes;
124         for (int i = 0; i < itfs.length; ++i) {
125           InterfaceType i1 = itfs[i];
126           if (i1.isFcClientItf()) {
127             InterfaceType i2 = t.getFcInterfaceType(i1.getFcItfName());
128             if (!i1.isFcSubTypeOf(i2)) {
129               return false;
130             }
131           }
132         }
133         itfs = t.getFcInterfaceTypes();
134         for (int i = 0; i < itfs.length; ++i) {
135           InterfaceType i2 = itfs[i];
136           if (!i2.isFcClientItf()) {
137             InterfaceType i1 = getFcInterfaceType(i2.getFcItfName());
138             if (!i1.isFcSubTypeOf(i2)) {
139               return false;
140             }
141           }
142         }
143         return true;
144       }
145     } catch (NoSuchInterfaceException e) {
146     }
147     return false;
148   }
149
150   /**
151    * Returns a copy of the given interface type array. This method is used to
152    * return a copy of the field of this class, instead of the field itself, so
153    * that its content can not be modified from outside.
154    *
155    * @param types the array to be cloned.
156    * @return a clone of the given array, or an empty array if <tt>type</tt> is
157    * <tt>null</tt>.
158    */

159
160   private static InterfaceType[] clone (final InterfaceType[] types) {
161     if (types == null) {
162       return new InterfaceType[0];
163     } else {
164       InterfaceType[] clone = new InterfaceType[types.length];
165       System.arraycopy(types, 0, clone, 0, types.length);
166       return clone;
167     }
168   }
169 }
170
Popular Tags