001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Shipment.java,v 1.15.2.2 2008/01/07 15:14:02 cwl Exp $
007: */
008:
009: package collections.ship.marshal;
010:
011: import java.io.Serializable;
012:
013: import com.sleepycat.bind.tuple.TupleInput;
014: import com.sleepycat.bind.tuple.TupleOutput;
015:
016: /**
017: * A Shipment represents the combined key/data pair for a shipment entity.
018: *
019: * <p> In this sample, Shipment is bound to the stored key/data entry by
020: * implementing the MarshalledEntity interface, which is called by {@link
021: * SampleViews.MarshalledEntityBinding}. </p>
022: *
023: * <p> The binding is "tricky" in that it uses this class for both the stored
024: * data entry and the combined entity object. To do this, the key field(s) are
025: * transient and are set by the binding after the data object has been
026: * deserialized. This avoids the use of a ShipmentData class completely. </p>
027: *
028: * <p> Since this class is used directly for data storage, it must be
029: * Serializable. </p>
030: *
031: * @author Mark Hayes
032: */
033: public class Shipment implements Serializable, MarshalledEntity {
034:
035: static final String PART_KEY = "part";
036: static final String SUPPLIER_KEY = "supplier";
037:
038: private transient String partNumber;
039: private transient String supplierNumber;
040: private int quantity;
041:
042: public Shipment(String partNumber, String supplierNumber,
043: int quantity) {
044:
045: this .partNumber = partNumber;
046: this .supplierNumber = supplierNumber;
047: this .quantity = quantity;
048: }
049:
050: /**
051: * Set the transient key fields after deserializing. This method is only
052: * called by data bindings.
053: */
054: void setKey(String partNumber, String supplierNumber) {
055:
056: this .partNumber = partNumber;
057: this .supplierNumber = supplierNumber;
058: }
059:
060: public final String getPartNumber() {
061:
062: return partNumber;
063: }
064:
065: public final String getSupplierNumber() {
066:
067: return supplierNumber;
068: }
069:
070: public final int getQuantity() {
071:
072: return quantity;
073: }
074:
075: public String toString() {
076:
077: return "[Shipment: part=" + partNumber + " supplier="
078: + supplierNumber + " quantity=" + quantity + ']';
079: }
080:
081: // --- MarshalledEntity implementation ---
082:
083: Shipment() {
084:
085: // A no-argument constructor is necessary only to allow the binding to
086: // instantiate objects of this class.
087: }
088:
089: public void unmarshalPrimaryKey(TupleInput keyInput) {
090:
091: this .partNumber = keyInput.readString();
092: this .supplierNumber = keyInput.readString();
093: }
094:
095: public void marshalPrimaryKey(TupleOutput keyOutput) {
096:
097: keyOutput.writeString(this .partNumber);
098: keyOutput.writeString(this .supplierNumber);
099: }
100:
101: public boolean marshalSecondaryKey(String keyName,
102: TupleOutput keyOutput) {
103:
104: if (keyName.equals(PART_KEY)) {
105: keyOutput.writeString(this .partNumber);
106: return true;
107: } else if (keyName.equals(SUPPLIER_KEY)) {
108: keyOutput.writeString(this .supplierNumber);
109: return true;
110: } else {
111: throw new UnsupportedOperationException(keyName);
112: }
113: }
114: }
|