KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > tuple > SampleViews


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

8
9 package collections.ship.tuple;
10
11 import com.sleepycat.bind.EntityBinding;
12 import com.sleepycat.bind.EntryBinding;
13 import com.sleepycat.bind.serial.ClassCatalog;
14 import com.sleepycat.bind.serial.TupleSerialBinding;
15 import com.sleepycat.bind.tuple.TupleBinding;
16 import com.sleepycat.bind.tuple.TupleInput;
17 import com.sleepycat.bind.tuple.TupleOutput;
18 import com.sleepycat.collections.StoredSortedMap;
19 import com.sleepycat.collections.StoredSortedValueSet;
20
21 /**
22  * SampleViews defines the data bindings and collection views for the sample
23  * database.
24  *
25  * @author Mark Hayes
26  */

27 public class SampleViews {
28
29     private StoredSortedMap partMap;
30     private StoredSortedMap supplierMap;
31     private StoredSortedMap shipmentMap;
32     private StoredSortedMap shipmentByPartMap;
33     private StoredSortedMap shipmentBySupplierMap;
34     private StoredSortedMap supplierByCityMap;
35
36     /**
37      * Create the data bindings and collection views.
38      */

39     public SampleViews(SampleDatabase db) {
40
41         // Create the data bindings.
42
// In this sample, EntityBinding classes are used to bind the stored
43
// key/data entry pair to a combined data object. For keys, a
44
// one-to-one binding is implemented with EntryBinding classes to bind
45
// the stored tuple entry to a key Object.
46
//
47
ClassCatalog catalog = db.getClassCatalog();
48         EntryBinding partKeyBinding =
49             new PartKeyBinding();
50         EntityBinding partDataBinding =
51             new PartBinding(catalog, PartData.class);
52         EntryBinding supplierKeyBinding =
53             new SupplierKeyBinding();
54         EntityBinding supplierDataBinding =
55             new SupplierBinding(catalog, SupplierData.class);
56         EntryBinding shipmentKeyBinding =
57             new ShipmentKeyBinding();
58         EntityBinding shipmentDataBinding =
59             new ShipmentBinding(catalog, ShipmentData.class);
60         EntryBinding cityKeyBinding =
61             TupleBinding.getPrimitiveBinding(String JavaDoc.class);
62
63         // Create map views for all stores and indices.
64
// StoredSortedMap is used since the stores and indices are ordered
65
// (they use the DB_BTREE access method).
66
//
67
partMap =
68             new StoredSortedMap(db.getPartDatabase(),
69                 partKeyBinding, partDataBinding, true);
70         supplierMap =
71             new StoredSortedMap(db.getSupplierDatabase(),
72                 supplierKeyBinding, supplierDataBinding, true);
73         shipmentMap =
74             new StoredSortedMap(db.getShipmentDatabase(),
75                 shipmentKeyBinding, shipmentDataBinding, true);
76         shipmentByPartMap =
77             new StoredSortedMap(db.getShipmentByPartDatabase(),
78                                 partKeyBinding, shipmentDataBinding, true);
79         shipmentBySupplierMap =
80             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
81                                 supplierKeyBinding, shipmentDataBinding, true);
82         supplierByCityMap =
83             new StoredSortedMap(db.getSupplierByCityDatabase(),
84                                 cityKeyBinding, supplierDataBinding, true);
85     }
86
87     // The views returned below can be accessed using the java.util.Map or
88
// java.util.Set interfaces, or using the StoredSortedMap and
89
// StoredValueSet classes, which provide additional methods. The entity
90
// sets could be obtained directly from the Map.values() method but
91
// convenience methods are provided here to return them in order to avoid
92
// down-casting elsewhere.
93

94     /**
95      * Return a map view of the part storage container.
96      */

97     public StoredSortedMap getPartMap() {
98
99         return partMap;
100     }
101
102     /**
103      * Return a map view of the supplier storage container.
104      */

105     public StoredSortedMap getSupplierMap() {
106
107         return supplierMap;
108     }
109
110     /**
111      * Return a map view of the shipment storage container.
112      */

113     public StoredSortedMap getShipmentMap() {
114
115         return shipmentMap;
116     }
117
118     /**
119      * Return an entity set view of the part storage container.
120      */

121     public StoredSortedValueSet getPartSet() {
122
123         return (StoredSortedValueSet) partMap.values();
124     }
125
126     /**
127      * Return an entity set view of the supplier storage container.
128      */

129     public StoredSortedValueSet getSupplierSet() {
130
131         return (StoredSortedValueSet) supplierMap.values();
132     }
133
134     /**
135      * Return an entity set view of the shipment storage container.
136      */

137     public StoredSortedValueSet getShipmentSet() {
138
139         return (StoredSortedValueSet) shipmentMap.values();
140     }
141
142     /**
143      * Return a map view of the shipment-by-part index.
144      */

145     public StoredSortedMap getShipmentByPartMap() {
146
147         return shipmentByPartMap;
148     }
149
150     /**
151      * Return a map view of the shipment-by-supplier index.
152      */

153     public StoredSortedMap getShipmentBySupplierMap() {
154
155         return shipmentBySupplierMap;
156     }
157
158     /**
159      * Return a map view of the supplier-by-city index.
160      */

161     public final StoredSortedMap getSupplierByCityMap() {
162
163         return supplierByCityMap;
164     }
165
166     /**
167      * PartKeyBinding is used to bind the stored key tuple entry for a part to
168      * a key object representation.
169      */

170     private static class PartKeyBinding extends TupleBinding {
171
172         /**
173          * Construct the binding object.
174          */

175         private PartKeyBinding() {
176         }
177
178         /**
179          * Create the key object from the stored key tuple entry.
180          */

181         public Object JavaDoc entryToObject(TupleInput input) {
182
183             String JavaDoc number = input.readString();
184             return new PartKey(number);
185         }
186
187         /**
188          * Create the stored key tuple entry from the key object.
189          */

190         public void objectToEntry(Object JavaDoc object, TupleOutput output) {
191
192             PartKey key = (PartKey) object;
193             output.writeString(key.getNumber());
194         }
195     }
196
197     /**
198      * PartBinding is used to bind the stored key/data entry pair for a part
199      * to a combined data object (entity).
200      */

201     private static class PartBinding extends TupleSerialBinding {
202
203         /**
204          * Construct the binding object.
205          */

206         private PartBinding(ClassCatalog classCatalog, Class JavaDoc dataClass) {
207
208             super(classCatalog, dataClass);
209         }
210
211         /**
212          * Create the entity by combining the stored key and data.
213          */

214         public Object JavaDoc entryToObject(TupleInput keyInput, Object JavaDoc dataInput) {
215
216             String JavaDoc number = keyInput.readString();
217             PartData data = (PartData) dataInput;
218             return new Part(number, data.getName(), data.getColor(),
219                             data.getWeight(), data.getCity());
220         }
221
222         /**
223          * Create the stored key from the entity.
224          */

225         public void objectToKey(Object JavaDoc object, TupleOutput output) {
226
227             Part part = (Part) object;
228             output.writeString(part.getNumber());
229         }
230
231         /**
232          * Create the stored data from the entity.
233          */

234         public Object JavaDoc objectToData(Object JavaDoc object) {
235
236             Part part = (Part) object;
237             return new PartData(part.getName(), part.getColor(),
238                                  part.getWeight(), part.getCity());
239         }
240     }
241
242     /**
243      * SupplierKeyBinding is used to bind the stored key tuple entry for a
244      * supplier to a key object representation.
245      */

246     private static class SupplierKeyBinding extends TupleBinding {
247
248         /**
249          * Construct the binding object.
250          */

251         private SupplierKeyBinding() {
252         }
253
254         /**
255          * Create the key object from the stored key tuple entry.
256          */

257         public Object JavaDoc entryToObject(TupleInput input) {
258
259             String JavaDoc number = input.readString();
260             return new SupplierKey(number);
261         }
262
263         /**
264          * Create the stored key tuple entry from the key object.
265          */

266         public void objectToEntry(Object JavaDoc object, TupleOutput output) {
267
268             SupplierKey key = (SupplierKey) object;
269             output.writeString(key.getNumber());
270         }
271     }
272
273     /**
274      * SupplierBinding is used to bind the stored key/data entry pair for a
275      * supplier to a combined data object (entity).
276      */

277     private static class SupplierBinding extends TupleSerialBinding {
278
279         /**
280          * Construct the binding object.
281          */

282         private SupplierBinding(ClassCatalog classCatalog, Class JavaDoc dataClass) {
283
284             super(classCatalog, dataClass);
285         }
286
287         /**
288          * Create the entity by combining the stored key and data.
289          */

290         public Object JavaDoc entryToObject(TupleInput keyInput, Object JavaDoc dataInput) {
291
292             String JavaDoc number = keyInput.readString();
293             SupplierData data = (SupplierData) dataInput;
294             return new Supplier(number, data.getName(),
295                                 data.getStatus(), data.getCity());
296         }
297
298         /**
299          * Create the stored key from the entity.
300          */

301         public void objectToKey(Object JavaDoc object, TupleOutput output) {
302
303             Supplier supplier = (Supplier) object;
304             output.writeString(supplier.getNumber());
305         }
306
307         /**
308          * Create the stored data from the entity.
309          */

310         public Object JavaDoc objectToData(Object JavaDoc object) {
311
312             Supplier supplier = (Supplier) object;
313             return new SupplierData(supplier.getName(), supplier.getStatus(),
314                                      supplier.getCity());
315         }
316     }
317
318     /**
319      * ShipmentKeyBinding is used to bind the stored key tuple entry for a
320      * shipment to a key object representation.
321      */

322     private static class ShipmentKeyBinding extends TupleBinding {
323
324         /**
325          * Construct the binding object.
326          */

327         private ShipmentKeyBinding() {
328         }
329
330         /**
331          * Create the key object from the stored key tuple entry.
332          */

333         public Object JavaDoc entryToObject(TupleInput input) {
334
335             String JavaDoc partNumber = input.readString();
336             String JavaDoc supplierNumber = input.readString();
337             return new ShipmentKey(partNumber, supplierNumber);
338         }
339
340         /**
341          * Create the stored key tuple entry from the key object.
342          */

343         public void objectToEntry(Object JavaDoc object, TupleOutput output) {
344
345             ShipmentKey key = (ShipmentKey) object;
346             output.writeString(key.getPartNumber());
347             output.writeString(key.getSupplierNumber());
348         }
349     }
350
351     /**
352      * ShipmentBinding is used to bind the stored key/data entry pair for a
353      * shipment to a combined data object (entity).
354      */

355     private static class ShipmentBinding extends TupleSerialBinding {
356
357         /**
358          * Construct the binding object.
359          */

360         private ShipmentBinding(ClassCatalog classCatalog, Class JavaDoc dataClass) {
361
362             super(classCatalog, dataClass);
363         }
364
365         /**
366          * Create the entity by combining the stored key and data.
367          */

368         public Object JavaDoc entryToObject(TupleInput keyInput, Object JavaDoc dataInput) {
369
370             String JavaDoc partNumber = keyInput.readString();
371             String JavaDoc supplierNumber = keyInput.readString();
372             ShipmentData data = (ShipmentData) dataInput;
373             return new Shipment(partNumber, supplierNumber,
374                                 data.getQuantity());
375         }
376
377         /**
378          * Create the stored key from the entity.
379          */

380         public void objectToKey(Object JavaDoc object, TupleOutput output) {
381
382             Shipment shipment = (Shipment) object;
383             output.writeString(shipment.getPartNumber());
384             output.writeString(shipment.getSupplierNumber());
385         }
386
387         /**
388          * Create the stored data from the entity.
389          */

390         public Object JavaDoc objectToData(Object JavaDoc object) {
391
392             Shipment shipment = (Shipment) object;
393             return new ShipmentData(shipment.getQuantity());
394         }
395     }
396 }
397
Popular Tags