1 23 24 package org.objectweb.jorm.metainfo.lib; 25 26 import org.objectweb.jorm.metainfo.api.CompositeName; 27 import org.objectweb.jorm.metainfo.api.MetaObject; 28 import org.objectweb.jorm.metainfo.api.ScalarField; 29 import org.objectweb.jorm.metainfo.api.Package; 30 import org.objectweb.jorm.type.api.PType; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Collection ; 38 import java.util.Collections ; 39 40 46 public class BasicCompositeName extends BasicMetaObject implements CompositeName { 47 48 51 private ArrayList inheritedCompositeNames; 52 53 58 private Map scalarFields; 59 60 63 private String name = null; 64 65 72 public BasicCompositeName(String className, 73 MetaObject parent) { 74 super(parent); 75 this.name = className; 76 scalarFields = new HashMap (5); 77 inheritedCompositeNames = new ArrayList (); 78 } 79 80 84 public String getName() { 85 return name; 86 } 87 88 92 public String getFQName() { 93 String res = ((Package ) parent).getName(); 94 if (res == null || res.length() == 0) 95 return name; 96 else 97 return res + "." + name; 98 } 99 100 104 public void addInheritedCompositeName(CompositeName cn) { 105 if (debug && cn != null) 106 logger.log(BasicLevel.DEBUG, 107 "The current compositename (" + name + ") inherits from the composite name (" + cn.getName() + ")"); 108 if (cn != null) 109 inheritedCompositeNames.add(cn); 110 } 111 112 public Iterator iterateInheritedCompositeName() { 113 return getSuperCompositeNames().iterator(); 114 } 115 116 public Collection getSuperCompositeNames() { 117 return inheritedCompositeNames; 118 } 119 120 125 public CompositeName getInheritedCompositeName(String name) { 126 127 CompositeName res = null; 128 boolean found = false; 129 130 Iterator iter = iterateInheritedCompositeName(); 131 while (iter.hasNext() && !found) { 132 res = (CompositeName) iter.next(); 133 if (res.getName().equals(name)) found = true; 134 } 135 if (found) return res; 136 return null; 137 138 } 139 140 145 public ScalarField getScalarField(String fieldname) { 146 if (debug) { 147 logger.log(BasicLevel.DEBUG, 148 "try to return field (" + fieldname + ")"); 149 } 150 ScalarField res = (ScalarField) scalarFields.get(fieldname); 153 if (debug && res == null) { 154 logger.log(BasicLevel.DEBUG, "the field (" + fieldname 156 + ") is not defined in the current class"); 157 } 158 return res; 159 } 160 161 165 public int getInheritedCompositeNameNumber() { 166 return inheritedCompositeNames.size(); 167 } 168 169 175 public ScalarField createCompositeNameField(String fieldname, PType type, int size, int scale) { 176 if (debug) 177 logger.log(BasicLevel.DEBUG, 178 "create a new NameField (" + fieldname + ") for the current CompositeName (" + name + ")"); 179 180 ScalarField result = (ScalarField) scalarFields.get(fieldname); 182 if (result != null) { 183 if (logger != null) { 184 logger.log(BasicLevel.WARN, 185 "attention, try to create an existing ScalarField (" + 186 fieldname + "), return existing one"); 187 } 188 } else { 189 result = new BasicScalarField(fieldname, type, size, scale, this); 191 setLoggingOnChild(result); 192 scalarFields.put(fieldname, result); 193 } 194 return result; 195 } 196 197 201 public int getFieldNumber() { 202 return scalarFields.size(); 203 } 204 205 209 public Iterator iterateField() { 210 return scalarFields.values().iterator(); 211 } 212 213 public Collection getFields() { 214 return scalarFields.values(); 215 } 216 217 public Collection getAllFields() { 218 ArrayList allFields = new ArrayList (); 219 allFields.addAll(scalarFields.values()); 220 221 for (Iterator iter = inheritedCompositeNames.iterator(); iter.hasNext();) { 223 CompositeName aCN = (CompositeName) iter.next(); 224 allFields.addAll(aCN.getAllFields()); 225 } 226 Collections.sort(allFields, FieldComparator.instance); 227 return allFields; 228 } 229 230 234 public Iterator iterateAllField() { 235 return getAllFields().iterator(); 236 } 237 238 241 public Collection getAllField() { 242 return getAllFields(); 243 } 244 245 protected Collection getChildren() { 246 return getAllFields(); 247 } 248 } 249 | Popular Tags |