1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SampleViews.java,v 12.7 2008/01/08 20:58:28 bostic Exp $
7 */
8
9package collections.ship.basic;
10
11import com.sleepycat.bind.EntryBinding;
12import com.sleepycat.bind.serial.ClassCatalog;
13import com.sleepycat.bind.serial.SerialBinding;
14import com.sleepycat.collections.StoredEntrySet;
15import com.sleepycat.collections.StoredMap;
16
17/**
18 * SampleViews defines the data bindings and collection views for the sample
19 * database.
20 *
21 * @author Mark Hayes
22 */
23public class SampleViews {
24
25    private StoredMap partMap;
26    private StoredMap supplierMap;
27    private StoredMap shipmentMap;
28
29    /**
30     * Create the data bindings and collection views.
31     */
32    public SampleViews(SampleDatabase db) {
33
34        // In this sample, the stored key and data entries are used directly
35        // rather than mapping them to separate objects. Therefore, no binding
36        // classes are defined here and the SerialBinding class is used.
37        //
38        ClassCatalog catalog = db.getClassCatalog();
39        EntryBinding partKeyBinding =
40            new SerialBinding(catalog, PartKey.class);
41        EntryBinding partDataBinding =
42            new SerialBinding(catalog, PartData.class);
43        EntryBinding supplierKeyBinding =
44            new SerialBinding(catalog, SupplierKey.class);
45        EntryBinding supplierDataBinding =
46            new SerialBinding(catalog, SupplierData.class);
47        EntryBinding shipmentKeyBinding =
48            new SerialBinding(catalog, ShipmentKey.class);
49        EntryBinding shipmentDataBinding =
50            new SerialBinding(catalog, ShipmentData.class);
51
52        // Create map views for all stores and indices.
53        // StoredSortedMap is not used since the stores and indices are
54        // ordered by serialized key objects, which do not provide a very
55        // useful ordering.
56        //
57        partMap =
58            new StoredMap(db.getPartDatabase(),
59                          partKeyBinding, partDataBinding, true);
60        supplierMap =
61            new StoredMap(db.getSupplierDatabase(),
62                          supplierKeyBinding, supplierDataBinding, true);
63        shipmentMap =
64            new StoredMap(db.getShipmentDatabase(),
65                          shipmentKeyBinding, shipmentDataBinding, true);
66    }
67
68    // The views returned below can be accessed using the java.util.Map or
69    // java.util.Set interfaces, or using the StoredMap and StoredEntrySet
70    // classes, which provide additional methods.  The entry sets could be
71    // obtained directly from the Map.entrySet() method, but convenience
72    // methods are provided here to return them in order to avoid down-casting
73    // elsewhere.
74
75    /**
76     * Return a map view of the part storage container.
77     */
78    public final StoredMap getPartMap() {
79
80        return partMap;
81    }
82
83    /**
84     * Return a map view of the supplier storage container.
85     */
86    public final StoredMap getSupplierMap() {
87
88        return supplierMap;
89    }
90
91    /**
92     * Return a map view of the shipment storage container.
93     */
94    public final StoredMap getShipmentMap() {
95
96        return shipmentMap;
97    }
98
99    /**
100     * Return an entry set view of the part storage container.
101     */
102    public final StoredEntrySet getPartEntrySet() {
103
104        return (StoredEntrySet) partMap.entrySet();
105    }
106
107    /**
108     * Return an entry set view of the supplier storage container.
109     */
110    public final StoredEntrySet getSupplierEntrySet() {
111
112        return (StoredEntrySet) supplierMap.entrySet();
113    }
114
115    /**
116     * Return an entry set view of the shipment storage container.
117     */
118    public final StoredEntrySet getShipmentEntrySet() {
119
120        return (StoredEntrySet) shipmentMap.entrySet();
121    }
122}
123