KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > RawAccessor


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: RawAccessor.java,v 1.5 2006/10/30 21:14:32 bostic Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14
15 import com.sleepycat.persist.raw.RawObject;
16
17 /**
18  * Implements Accessor for RawObject access.
19  *
20  * @author Mark Hayes
21  */

22 class RawAccessor implements Accessor {
23
24     private Format parentFormat;
25     private Accessor superAccessor;
26     private FieldInfo priKeyField;
27     private List JavaDoc<FieldInfo> secKeyFields;
28     private List JavaDoc<FieldInfo> nonKeyFields;
29     private boolean isCompositeKey;
30
31     RawAccessor(Format parentFormat,
32                 Accessor superAccessor,
33                 FieldInfo priKeyField,
34                 List JavaDoc<FieldInfo> secKeyFields,
35                 List JavaDoc<FieldInfo> nonKeyFields) {
36         this.parentFormat = parentFormat;
37         this.superAccessor = superAccessor;
38         this.priKeyField = priKeyField;
39         this.secKeyFields = secKeyFields;
40         this.nonKeyFields = nonKeyFields;
41     }
42
43     RawAccessor(Format parentFormat,
44                 List JavaDoc<FieldInfo> nonKeyFields) {
45         this.parentFormat = parentFormat;
46         this.nonKeyFields = nonKeyFields;
47         secKeyFields = Collections.emptyList();
48         isCompositeKey = true;
49     }
50
51     public Object JavaDoc newInstance() {
52         RawObject superObject;
53         if (superAccessor != null) {
54             superObject = ((RawObject) superAccessor.newInstance());
55         } else {
56             superObject = null;
57         }
58         return new RawObject
59             (parentFormat, new HashMap JavaDoc<String JavaDoc,Object JavaDoc>(), superObject);
60     }
61
62     public Object JavaDoc newArray(int len) {
63         throw new UnsupportedOperationException JavaDoc();
64     }
65
66     public boolean isPriKeyFieldNullOrZero(Object JavaDoc o) {
67         if (priKeyField != null) {
68             Object JavaDoc val = getValue(o, priKeyField);
69             Format format = priKeyField.getType();
70             if (format.isPrimitive()) {
71                 return ((Number JavaDoc) val).longValue() == 0L;
72             } else {
73                 return val == null;
74             }
75         } else if (superAccessor != null) {
76             return superAccessor.isPriKeyFieldNullOrZero(getSuper(o));
77         } else {
78             throw new IllegalStateException JavaDoc("No primary key field");
79         }
80     }
81
82     public void writePriKeyField(Object JavaDoc o, EntityOutput output) {
83         if (priKeyField != null) {
84             Object JavaDoc val = getValue(o, priKeyField);
85             Format format = priKeyField.getType();
86             output.writeKeyObject(val, format);
87         } else if (superAccessor != null) {
88             superAccessor.writePriKeyField(getSuper(o), output);
89         } else {
90             throw new IllegalStateException JavaDoc("No primary key field");
91         }
92     }
93
94     public void readPriKeyField(Object JavaDoc o, EntityInput input) {
95         if (priKeyField != null) {
96             Format format = priKeyField.getType();
97             Object JavaDoc val = input.readKeyObject(format);
98             setValue(o, priKeyField, val);
99         } else if (superAccessor != null) {
100             superAccessor.readPriKeyField(getSuper(o), input);
101         } else {
102             throw new IllegalStateException JavaDoc("No primary key field");
103         }
104     }
105
106     public void writeSecKeyFields(Object JavaDoc o, EntityOutput output) {
107         if (priKeyField != null && !priKeyField.getType().isPrimitive()) {
108             output.registerPriKeyObject(getValue(o, priKeyField));
109         }
110         if (superAccessor != null) {
111             superAccessor.writeSecKeyFields(getSuper(o), output);
112         }
113         for (int i = 0; i < secKeyFields.size(); i += 1) {
114             writeField(o, secKeyFields.get(i), output);
115         }
116     }
117     
118     public void readSecKeyFields(Object JavaDoc o,
119                                  EntityInput input,
120                                  int startField,
121                                  int endField,
122                                  int superLevel) {
123         if (priKeyField != null && !priKeyField.getType().isPrimitive()) {
124             input.registerPriKeyObject(getValue(o, priKeyField));
125         }
126         if (superLevel != 0 && superAccessor != null) {
127             superAccessor.readSecKeyFields
128                 (getSuper(o), input, startField, endField, superLevel - 1);
129         } else {
130             if (superLevel > 0) {
131                 throw new IllegalStateException JavaDoc
132                     ("Super class does not exist");
133             }
134         }
135         if (superLevel <= 0) {
136             for (int i = startField;
137                  i <= endField && i < secKeyFields.size();
138                  i += 1) {
139                 readField(o, secKeyFields.get(i), input);
140             }
141         }
142     }
143
144     public void writeNonKeyFields(Object JavaDoc o, EntityOutput output) {
145         if (superAccessor != null) {
146             superAccessor.writeNonKeyFields(getSuper(o), output);
147         }
148         for (int i = 0; i < nonKeyFields.size(); i += 1) {
149             writeField(o, nonKeyFields.get(i), output);
150         }
151     }
152     
153     public void readNonKeyFields(Object JavaDoc o,
154                                  EntityInput input,
155                                  int startField,
156                                  int endField,
157                                  int superLevel) {
158         if (superLevel != 0 && superAccessor != null) {
159             superAccessor.readNonKeyFields
160                 (getSuper(o), input, startField, endField, superLevel - 1);
161         } else {
162             if (superLevel > 0) {
163                 throw new IllegalStateException JavaDoc
164                     ("Super class does not exist");
165             }
166         }
167         if (superLevel <= 0) {
168             for (int i = startField;
169                  i <= endField && i < nonKeyFields.size();
170                  i += 1) {
171                 readField(o, nonKeyFields.get(i), input);
172             }
173         }
174     }
175
176     public Object JavaDoc getField(Object JavaDoc o,
177                            int field,
178                            int superLevel,
179                            boolean isSecField) {
180         if (superLevel > 0) {
181             return superAccessor.getField
182                 (getSuper(o), field, superLevel - 1, isSecField);
183         }
184         FieldInfo fld = isSecField ? secKeyFields.get(field)
185                                    : nonKeyFields.get(field);
186         return getValue(o, fld);
187     }
188
189     public void setField(Object JavaDoc o,
190                          int field,
191                          int superLevel,
192                          boolean isSecField,
193                          Object JavaDoc value) {
194         if (superLevel > 0) {
195             superAccessor.setField
196                 (getSuper(o), field, superLevel - 1, isSecField, value);
197         }
198         FieldInfo fld = isSecField ? secKeyFields.get(field)
199                                    : nonKeyFields.get(field);
200         setValue(o, fld, value);
201     }
202
203     private RawObject getSuper(Object JavaDoc o) {
204         return ((RawObject) o).getSuper();
205     }
206
207     private Object JavaDoc getValue(Object JavaDoc o, FieldInfo field) {
208         return ((RawObject) o).getValues().get(field.getName());
209     }
210
211     private void setValue(Object JavaDoc o, FieldInfo field, Object JavaDoc val) {
212         ((RawObject) o).getValues().put(field.getName(), val);
213     }
214
215     private void writeField(Object JavaDoc o, FieldInfo field, EntityOutput output) {
216         Object JavaDoc val = getValue(o, field);
217         Format format = field.getType();
218         if (isCompositeKey || format.isPrimitive()) {
219             output.writeKeyObject(val, format);
220         } else {
221             output.writeObject(val, format);
222         }
223     }
224
225     private void readField(Object JavaDoc o, FieldInfo field, EntityInput input) {
226         Format format = field.getType();
227         Object JavaDoc val;
228         if (isCompositeKey || format.isPrimitive()) {
229             val = input.readKeyObject(format);
230         } else {
231             val = input.readObject();
232         }
233         setValue(o, field, val);
234     }
235 }
236
Popular Tags