1 24 package org.objectweb.jalisto.se.impl.meta; 25 26 import org.objectweb.jalisto.se.api.ClassDescription; 27 import org.objectweb.jalisto.se.api.query.FieldDescription; 28 import org.objectweb.jalisto.se.api.query.FielComparator; 29 import org.objectweb.jalisto.se.exception.SchemaException; 30 import org.objectweb.jalisto.se.impl.InFileAddress; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 35 public class ClassDescriptionImpl implements ClassDescription { 36 37 public ClassDescriptionImpl(String fullClassName) { 38 this.className = fullClassName; 39 this.fields = new FieldDescriptionImpl[0]; 40 } 41 42 public String getClassName() { 43 return className; 44 } 45 46 public int getIndex(String fieldName) { 47 for (short i = 0; i < fields.length; i++) { 48 if (fields[i].getFieldName().equals(fieldName)) { 49 return i; 50 } 51 } 52 throw new SchemaException("no field with the name " + fieldName + " in class " + className); 53 } 54 55 public FieldDescription getFieldDescription(int index) { 56 return fields[index]; 57 } 58 59 public FielComparator getComparator(int index) { 60 return fields[index].getType().getComparator(); 61 } 62 63 public boolean isIndexDefined(int index) { 64 return fields[index].isIndexDefined(); 65 } 66 67 public InFileAddress getIndexIfa(int index) { 68 return fields[index].getIndexDescription().getValueIndexIfa(); 69 } 70 71 public void setIndexIfa(int index, InFileAddress ifa) { 72 fields[index].getIndexDescription().setValueIndexIfa(ifa); 73 } 74 75 public Iterator getIndexedFieldDescription() { 76 ArrayList list = new ArrayList (); 77 for (short i = 0; i < fields.length; i++) { 78 if (fields[i].getIndexDescription().hasIndex()) { 79 list.add(fields[i].getIndexDescription()); 80 } 81 } 82 return list.iterator(); 83 } 84 85 public short getIndexType(int index) { 86 return fields[index].getIndexDescription().getValueIndexType(); 87 } 88 89 public void setIndexType(int index, short indexType) { 90 fields[index].getIndexDescription().setValueIndexType(indexType); 91 } 92 93 public short getNodePageSize(int index) { 94 return fields[index].getIndexDescription().getNodePageSize(); 95 } 96 97 public void setNodePageSize(int index, short size) { 98 fields[index].getIndexDescription().setNodePageSize(size); 99 } 100 101 public short getNodeSize(int index) { 102 return fields[index].getIndexDescription().getNodeSize(); 103 } 104 105 public void setNodeSize(int index, short size) { 106 fields[index].getIndexDescription().setNodeSize(size); 107 } 108 109 public short getLeafPageSize(int index) { 110 return fields[index].getIndexDescription().getLeafPageSize(); 111 } 112 113 public void setLeafPageSize(int index, short size) { 114 fields[index].getIndexDescription().setLeafPageSize(size); 115 } 116 117 public Object getValueFor(String fieldName, Object [] values) { 118 return values[getIndex(fieldName)]; 119 } 120 121 126 public String [] getFieldNames() { 127 String [] result = new String [fields.length]; 128 for (int i = 0; i < result.length; i++) { 129 result[i] = fields[i].getFieldName(); 130 } 131 return result; 132 } 133 134 139 public int getNbrFields() { 140 return fields.length; 141 } 142 143 148 public void addField(FieldDescription fieldDescription) { 149 fieldDescription.setClassMeta(this); 150 FieldDescriptionImpl[] nfd = new FieldDescriptionImpl[fields.length + 1]; 151 short index = fieldDescription.getIndex(); 152 if (index < 0) { 153 index = (short) fields.length; 154 fieldDescription.setIndex(index); 155 } 156 for (short i = 0; i < nfd.length; i++) { 157 if (i < index) { 158 nfd[i] = fields[i]; 159 } else if (i == index) { 160 nfd[i] = (FieldDescriptionImpl) fieldDescription; 161 } else { 162 fields[i - 1].setIndex(i); 163 nfd[i] = fields[i - 1]; 164 } 165 } 166 fields = nfd; 167 } 168 169 174 public void removeField(int index) { 175 FieldDescriptionImpl[] nfd = new FieldDescriptionImpl[fields.length - 1]; 176 for (short i = 0; i < nfd.length; i++) { 177 if (i < index) { 178 nfd[i] = fields[i]; 179 } else { 180 fields[i + 1].setIndex(i); 181 nfd[i] = fields[i + 1]; 182 } 183 } 184 fields = nfd; 185 } 186 187 public void updateInBase() { 188 repository.writePersistentClassMetaDescription(this); 189 } 190 191 public void setRepository(InternalMetaRepositoryImpl repository) { 192 this.repository = repository; 193 } 194 195 public String toString() { 196 return className + " with " + fields.length + " fields"; 197 } 198 199 public String toStringFull(int nbrTab) { 200 String tab = ""; 201 for (int i = 0; i < nbrTab; i++) { 202 tab = tab + "\t"; 203 } 204 StringBuffer sb = new StringBuffer (); 205 sb.append(tab).append(className).append("\n"); 206 for (int i = 0; i < fields.length; i++) { 207 sb.append(tab).append("\t").append(fields[i]).append("\n"); 208 } 209 return sb.toString(); 210 } 211 212 public boolean equals(Object o) { 213 try { 214 ClassDescriptionImpl candidate = (ClassDescriptionImpl) o; 215 boolean classTest = candidate.className.equals(this.className); 216 boolean sizeTest = (candidate.fields.length == this.fields.length); 217 if (classTest && sizeTest) { 218 for (int i = 0; i < fields.length; i++) { 219 if (!candidate.fields[i].equals(this.fields[i])) { 220 return false; 221 } 222 } 223 return true; 224 } 225 return false; 226 } catch (Exception e) { 227 } 228 return false; 229 } 230 231 private String className; 232 private FieldDescriptionImpl[] fields; 233 private transient InternalMetaRepositoryImpl repository; 234 235 236 static final long serialVersionUID = -7589377097934761459L; 237 } 238 | Popular Tags |