1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: Shipment.java,v 12.7 2008/01/08 20:58:29 bostic Exp $ 7 */ 8 9package collections.ship.factory; 10 11import java.io.Serializable; 12 13import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity; 14import com.sleepycat.bind.tuple.TupleInput; 15import com.sleepycat.bind.tuple.TupleOutput; 16 17/** 18 * A Shipment represents the combined key/data pair for a shipment entity. 19 * 20 * <p> In this sample, Shipment is bound to the stored key/data entry by 21 * implementing the MarshalledTupleKeyEntity interface. </p> 22 * 23 * <p> The binding is "tricky" in that it uses this class for both the stored 24 * data entry and the combined entity object. To do this, the key field(s) 25 * are transient and are set by the binding after the data object has been 26 * deserialized. This avoids the use of a ShipmentData class completely. </p> 27 * 28 * <p> Since this class is used directly for data storage, it must be 29 * Serializable. </p> 30 * 31 * @author Mark Hayes 32 */ 33public class Shipment implements Serializable, MarshalledTupleKeyEntity { 34 35 static final String PART_KEY = "part"; 36 static final String SUPPLIER_KEY = "supplier"; 37 38 private transient String partNumber; 39 private transient String supplierNumber; 40 private int quantity; 41 42 public Shipment(String partNumber, String supplierNumber, int quantity) { 43 44 this.partNumber = partNumber; 45 this.supplierNumber = supplierNumber; 46 this.quantity = quantity; 47 } 48 49 public final String getPartNumber() { 50 51 return partNumber; 52 } 53 54 public final String getSupplierNumber() { 55 56 return supplierNumber; 57 } 58 59 public final int getQuantity() { 60 61 return quantity; 62 } 63 64 public String toString() { 65 66 return "[Shipment: part=" + partNumber + 67 " supplier=" + supplierNumber + 68 " quantity=" + quantity + ']'; 69 } 70 71 // --- MarshalledTupleKeyEntity implementation --- 72 73 public void marshalPrimaryKey(TupleOutput keyOutput) { 74 75 keyOutput.writeString(this.partNumber); 76 keyOutput.writeString(this.supplierNumber); 77 } 78 79 public void unmarshalPrimaryKey(TupleInput keyInput) { 80 81 this.partNumber = keyInput.readString(); 82 this.supplierNumber = keyInput.readString(); 83 } 84 85 public boolean marshalSecondaryKey(String keyName, TupleOutput keyOutput) { 86 87 if (keyName.equals(PART_KEY)) { 88 keyOutput.writeString(this.partNumber); 89 return true; 90 } else if (keyName.equals(SUPPLIER_KEY)) { 91 keyOutput.writeString(this.supplierNumber); 92 return true; 93 } else { 94 throw new UnsupportedOperationException(keyName); 95 } 96 } 97 98 public boolean nullifyForeignKey(String keyName) { 99 100 throw new UnsupportedOperationException(keyName); 101 } 102} 103