1 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 ; 36 37 40 41 public class BasicComponentType implements ComponentType, Serializable { 42 43 46 47 private final InterfaceType[] interfaceTypes; 48 49 53 61 62 public BasicComponentType (final InterfaceType[] itfTypes) 63 throws InstantiationException 64 { 65 interfaceTypes = clone(itfTypes); 66 for (int i = 0; i < interfaceTypes.length; ++i) { 68 String p = interfaceTypes[i].getFcItfName(); 69 boolean collection = interfaceTypes[i].isFcCollectionItf(); 70 for (int j = i + 1; j < interfaceTypes.length; ++j) { 71 String 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 98 public InterfaceType[] getFcInterfaceTypes () { 99 return clone(interfaceTypes); 100 } 101 102 public InterfaceType getFcInterfaceType (final String name) 103 throws NoSuchInterfaceException 104 { 105 for (int i = 0; i < interfaceTypes.length; ++i) { 106 InterfaceType itfType = interfaceTypes[i]; 107 String 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 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 |