KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > meta > ClassDescriptionImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
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.1 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  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 public class ClassDescriptionImpl implements ClassDescription {
36
37     public ClassDescriptionImpl(String JavaDoc fullClassName) {
38         this.className = fullClassName;
39         this.fields = new FieldDescriptionImpl[0];
40     }
41
42     public String JavaDoc getClassName() {
43         return className;
44     }
45
46     public int getIndex(String JavaDoc 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 JavaDoc getIndexedFieldDescription() {
76         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc getValueFor(String JavaDoc fieldName, Object JavaDoc[] values) {
118         return values[getIndex(fieldName)];
119     }
120
121     /**
122      * Return an array of the names of the declared fields of the described class.
123      *
124      * @return String[] The names of the described class fields.
125      */

126     public String JavaDoc[] getFieldNames() {
127         String JavaDoc[] result = new String JavaDoc[fields.length];
128         for (int i = 0; i < result.length; i++) {
129             result[i] = fields[i].getFieldName();
130         }
131         return result;
132     }
133
134     /**
135      * Return the number of field already declared for the described class.
136      *
137      * @return int The number of declared fields.
138      */

139     public int getNbrFields() {
140         return fields.length;
141     }
142
143     /**
144      * Insert a new field in the description of the class.
145      *
146      * @param fieldDescription The name of the field to insert.
147      */

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     /**
170      * Remove a field from the description of the class.
171      *
172      * @param index The index of the place where to remove the field.
173      */

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 JavaDoc toString() {
196         return className + " with " + fields.length + " fields";
197     }
198
199     public String JavaDoc toStringFull(int nbrTab) {
200         String JavaDoc tab = "";
201         for (int i = 0; i < nbrTab; i++) {
202             tab = tab + "\t";
203         }
204         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc e) {
227         }
228         return false;
229     }
230
231     private String JavaDoc className;
232     private FieldDescriptionImpl[] fields;
233     private transient InternalMetaRepositoryImpl repository;
234
235
236     static final long serialVersionUID = -7589377097934761459L;
237 }
238
Popular Tags