Creating Collections with Entity Bindings

Stored map objects are created in this example in the same way as in prior examples, but using entity bindings in place of value bindings. All value objects passed and returned to the Java collections API are then actually entity objects (Part, Supplier and Shipment). The application no longer deals directly with plain value objects (PartData, SupplierData and ShipmentData).

Since the partDataBinding, supplierDataBinding and shipmentDataBinding were defined as entity bindings in the prior section, there are no source code changes necessary for creating the stored map objects.

public class SampleViews
{
    ...
    public SampleViews(SampleDatabase db)
    {
        ...
        partMap =
            new StoredSortedMap(db.getPartDatabase(),
                          partKeyBinding, partDataBinding, true);
        supplierMap =
            new StoredSortedMap(db.getSupplierDatabase(),
                          supplierKeyBinding, supplierDataBinding, true);
        shipmentMap =
            new StoredSortedMap(db.getShipmentDatabase(),
                          shipmentKeyBinding, shipmentDataBinding, true);
      ...
    } 

Specifying an EntityBinding will select a different StoredSortedMap constructor, but the syntax is the same. In general, an entity binding may be used anywhere that a value binding is used.

The following getter methods are defined for use by other classes in the example program. Instead of returning the map's entry set (Map.entrySet), the map's value set (Map.values) is returned. The entry set was convenient in prior examples because it allowed enumerating all key/value pairs in the collection. Since an entity contains the key and the value, enumerating the value set can now be used more conveniently for the same purpose.

import com.sleepycat.collections.StoredValueSet;
...
public class SampleViews
{
    ...
    public StoredValueSet getPartSet()
    {
        return (StoredValueSet) partMap.values();
    }

    public StoredValueSet getSupplierSet()
    {
        return (StoredValueSet) supplierMap.values();
    }

    public StoredValueSet getShipmentSet()
    {
        return (StoredValueSet) shipmentMap.values();
    }
    ...
} 

Notice that the collection returned by the StoredSortedMap.values method is actually a StoredValueSet and not just a Collection as defined by the Map.values interface. As long as duplicate keys are not allowed, this collection will behave as a true set and will disallow the addition of duplicates, etc.