KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > marshal > Shipment


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Shipment.java,v 1.15 2006/10/30 21:14:01 bostic Exp $
7  */

8
9 package collections.ship.marshal;
10
11 import java.io.Serializable JavaDoc;
12
13 import com.sleepycat.bind.tuple.TupleInput;
14 import com.sleepycat.bind.tuple.TupleOutput;
15
16 /**
17  * A Shipment represents the combined key/data pair for a shipment entity.
18  *
19  * <p> In this sample, Shipment is bound to the stored key/data entry by
20  * implementing the MarshalledEntity interface, which is called by {@link
21  * SampleViews.MarshalledEntityBinding}. </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) are
25  * 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  */

33 public class Shipment implements Serializable JavaDoc, MarshalledEntity {
34
35     static final String JavaDoc PART_KEY = "part";
36     static final String JavaDoc SUPPLIER_KEY = "supplier";
37
38     private transient String JavaDoc partNumber;
39     private transient String JavaDoc supplierNumber;
40     private int quantity;
41
42     public Shipment(String JavaDoc partNumber, String JavaDoc supplierNumber, int quantity) {
43
44         this.partNumber = partNumber;
45         this.supplierNumber = supplierNumber;
46         this.quantity = quantity;
47     }
48
49     /**
50      * Set the transient key fields after deserializing. This method is only
51      * called by data bindings.
52      */

53     void setKey(String JavaDoc partNumber, String JavaDoc supplierNumber) {
54
55         this.partNumber = partNumber;
56         this.supplierNumber = supplierNumber;
57     }
58
59     public final String JavaDoc getPartNumber() {
60
61         return partNumber;
62     }
63
64     public final String JavaDoc getSupplierNumber() {
65
66         return supplierNumber;
67     }
68
69     public final int getQuantity() {
70
71         return quantity;
72     }
73
74     public String JavaDoc toString() {
75
76         return "[Shipment: part=" + partNumber +
77                 " supplier=" + supplierNumber +
78                 " quantity=" + quantity + ']';
79     }
80
81     // --- MarshalledEntity implementation ---
82

83     Shipment() {
84
85         // A no-argument constructor is necessary only to allow the binding to
86
// instantiate objects of this class.
87
}
88
89     public void unmarshalPrimaryKey(TupleInput keyInput) {
90
91         this.partNumber = keyInput.readString();
92         this.supplierNumber = keyInput.readString();
93     }
94
95     public void marshalPrimaryKey(TupleOutput keyOutput) {
96
97         keyOutput.writeString(this.partNumber);
98         keyOutput.writeString(this.supplierNumber);
99     }
100
101     public boolean marshalSecondaryKey(String JavaDoc keyName, TupleOutput keyOutput) {
102
103         if (keyName.equals(PART_KEY)) {
104             keyOutput.writeString(this.partNumber);
105             return true;
106         } else if (keyName.equals(SUPPLIER_KEY)) {
107             keyOutput.writeString(this.supplierNumber);
108             return true;
109         } else {
110             throw new UnsupportedOperationException JavaDoc(keyName);
111         }
112     }
113 }
114
Popular Tags