Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SampleViews.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SampleViews.java,v 12.2 2005/06/16 20:22:26 bostic Exp $
00008  */
00009 
00010 package collections.ship.entity;
00011 
00012 import com.sleepycat.bind.EntityBinding;
00013 import com.sleepycat.bind.serial.ClassCatalog;
00014 import com.sleepycat.bind.serial.SerialBinding;
00015 import com.sleepycat.bind.serial.SerialSerialBinding;
00016 import com.sleepycat.collections.StoredSortedMap;
00017 import com.sleepycat.collections.StoredValueSet;
00018 
00025 public class SampleViews {
00026 
00027     private StoredSortedMap partMap;
00028     private StoredSortedMap supplierMap;
00029     private StoredSortedMap shipmentMap;
00030     private StoredSortedMap shipmentByPartMap;
00031     private StoredSortedMap shipmentBySupplierMap;
00032     private StoredSortedMap supplierByCityMap;
00033 
00037     public SampleViews(SampleDatabase db) {
00038 
00039         // Create the data bindings.
00040         // In this sample, EntityBinding classes are used to bind the stored
00041         // key/data entry pair to a combined data object.  For keys, however,
00042         // the stored entry is used directly via a SerialBinding and no
00043         // special binding class is needed.
00044         //
00045         ClassCatalog catalog = db.getClassCatalog();
00046         SerialBinding partKeyBinding =
00047             new SerialBinding(catalog, PartKey.class);
00048         EntityBinding partDataBinding =
00049             new PartBinding(catalog, PartKey.class, PartData.class);
00050         SerialBinding supplierKeyBinding =
00051             new SerialBinding(catalog, SupplierKey.class);
00052         EntityBinding supplierDataBinding =
00053             new SupplierBinding(catalog, SupplierKey.class,
00054                                 SupplierData.class);
00055         SerialBinding shipmentKeyBinding =
00056             new SerialBinding(catalog, ShipmentKey.class);
00057         EntityBinding shipmentDataBinding =
00058             new ShipmentBinding(catalog, ShipmentKey.class,
00059                                 ShipmentData.class);
00060         SerialBinding cityKeyBinding =
00061             new SerialBinding(catalog, String.class);
00062 
00063         // Create map views for all stores and indices.
00064         // StoredSortedMap is not used since the stores and indices are
00065         // ordered by serialized key objects, which do not provide a very
00066         // useful ordering.
00067         //
00068         partMap =
00069             new StoredSortedMap(db.getPartDatabase(),
00070                                 partKeyBinding, partDataBinding, true);
00071         supplierMap =
00072             new StoredSortedMap(db.getSupplierDatabase(),
00073                                 supplierKeyBinding, supplierDataBinding, true);
00074         shipmentMap =
00075             new StoredSortedMap(db.getShipmentDatabase(),
00076                                 shipmentKeyBinding, shipmentDataBinding, true);
00077         shipmentByPartMap =
00078             new StoredSortedMap(db.getShipmentByPartDatabase(),
00079                                 partKeyBinding, shipmentDataBinding, true);
00080         shipmentBySupplierMap =
00081             new StoredSortedMap(db.getShipmentBySupplierDatabase(),
00082                                 supplierKeyBinding, shipmentDataBinding, true);
00083         supplierByCityMap =
00084             new StoredSortedMap(db.getSupplierByCityDatabase(),
00085                                 cityKeyBinding, supplierDataBinding, true);
00086     }
00087 
00088     // The views returned below can be accessed using the java.util.Map or
00089     // java.util.Set interfaces, or using the StoredSortedMap and
00090     // StoredValueSet classes, which provide additional methods.  The entity
00091     // sets could be obtained directly from the Map.values() method but
00092     // convenience methods are provided here to return them in order to avoid
00093     // down-casting elsewhere.
00094 
00098     public StoredSortedMap getPartMap() {
00099 
00100         return partMap;
00101     }
00102 
00106     public StoredSortedMap getSupplierMap() {
00107 
00108         return supplierMap;
00109     }
00110 
00114     public StoredSortedMap getShipmentMap() {
00115 
00116         return shipmentMap;
00117     }
00118 
00122     public StoredValueSet getPartSet() {
00123 
00124         return (StoredValueSet) partMap.values();
00125     }
00126 
00130     public StoredValueSet getSupplierSet() {
00131 
00132         return (StoredValueSet) supplierMap.values();
00133     }
00134 
00138     public StoredValueSet getShipmentSet() {
00139 
00140         return (StoredValueSet) shipmentMap.values();
00141     }
00142 
00146     public StoredSortedMap getShipmentByPartMap() {
00147 
00148         return shipmentByPartMap;
00149     }
00150 
00154     public StoredSortedMap getShipmentBySupplierMap() {
00155 
00156         return shipmentBySupplierMap;
00157     }
00158 
00162     public final StoredSortedMap getSupplierByCityMap() {
00163 
00164         return supplierByCityMap;
00165     }
00166 
00171     private static class PartBinding extends SerialSerialBinding {
00172 
00176         private PartBinding(ClassCatalog classCatalog,
00177                             Class keyClass,
00178                             Class dataClass) {
00179 
00180             super(classCatalog, keyClass, dataClass);
00181         }
00182 
00186         public Object entryToObject(Object keyInput, Object dataInput) {
00187 
00188             PartKey key = (PartKey) keyInput;
00189             PartData data = (PartData) dataInput;
00190             return new Part(key.getNumber(), data.getName(), data.getColor(),
00191                             data.getWeight(), data.getCity());
00192         }
00193 
00197         public Object objectToKey(Object object) {
00198 
00199             Part part = (Part) object;
00200             return new PartKey(part.getNumber());
00201         }
00202 
00206         public Object objectToData(Object object) {
00207 
00208             Part part = (Part) object;
00209             return new PartData(part.getName(), part.getColor(),
00210                                  part.getWeight(), part.getCity());
00211         }
00212     }
00213 
00218     private static class SupplierBinding extends SerialSerialBinding {
00219 
00223         private SupplierBinding(ClassCatalog classCatalog,
00224                                 Class keyClass,
00225                                 Class dataClass) {
00226 
00227             super(classCatalog, keyClass, dataClass);
00228         }
00229 
00233         public Object entryToObject(Object keyInput, Object dataInput) {
00234 
00235             SupplierKey key = (SupplierKey) keyInput;
00236             SupplierData data = (SupplierData) dataInput;
00237             return new Supplier(key.getNumber(), data.getName(),
00238                                 data.getStatus(), data.getCity());
00239         }
00240 
00244         public Object objectToKey(Object object) {
00245 
00246             Supplier supplier = (Supplier) object;
00247             return new SupplierKey(supplier.getNumber());
00248         }
00249 
00253         public Object objectToData(Object object) {
00254 
00255             Supplier supplier = (Supplier) object;
00256             return new SupplierData(supplier.getName(), supplier.getStatus(),
00257                                      supplier.getCity());
00258         }
00259     }
00260 
00265     private static class ShipmentBinding extends SerialSerialBinding {
00266 
00270         private ShipmentBinding(ClassCatalog classCatalog,
00271                                 Class keyClass,
00272                                 Class dataClass) {
00273 
00274             super(classCatalog, keyClass, dataClass);
00275         }
00276 
00280         public Object entryToObject(Object keyInput, Object dataInput) {
00281 
00282             ShipmentKey key = (ShipmentKey) keyInput;
00283             ShipmentData data = (ShipmentData) dataInput;
00284             return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
00285                                 data.getQuantity());
00286         }
00287 
00291         public Object objectToKey(Object object) {
00292 
00293             Shipment shipment = (Shipment) object;
00294             return new ShipmentKey(shipment.getPartNumber(),
00295                                    shipment.getSupplierNumber());
00296         }
00297 
00301         public Object objectToData(Object object) {
00302 
00303             Shipment shipment = (Shipment) object;
00304             return new ShipmentData(shipment.getQuantity());
00305         }
00306     }
00307 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2