001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Part.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 Part represents the combined key/data pair for a part entity.
018: *
019: * <p> In this sample, Part 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 PartData 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 Part implements Serializable, MarshalledEntity {
034:
035: private transient String number;
036: private String name;
037: private String color;
038: private Weight weight;
039: private String city;
040:
041: public Part(String number, String name, String color,
042: Weight weight, String city) {
043:
044: this .number = number;
045: this .name = name;
046: this .color = color;
047: this .weight = weight;
048: this .city = city;
049: }
050:
051: /**
052: * Set the transient key fields after deserializing. This method is only
053: * called by data bindings.
054: */
055: final void setKey(String number) {
056:
057: this .number = number;
058: }
059:
060: public final String getNumber() {
061:
062: return number;
063: }
064:
065: public final String getName() {
066:
067: return name;
068: }
069:
070: public final String getColor() {
071:
072: return color;
073: }
074:
075: public final Weight getWeight() {
076:
077: return weight;
078: }
079:
080: public final String getCity() {
081:
082: return city;
083: }
084:
085: public String toString() {
086:
087: return "[Part: number=" + number + " name=" + name + " color="
088: + color + " weight=" + weight + " city=" + city + ']';
089: }
090:
091: // --- MarshalledEntity implementation ---
092:
093: Part() {
094:
095: // A no-argument constructor is necessary only to allow the binding to
096: // instantiate objects of this class.
097: }
098:
099: public void unmarshalPrimaryKey(TupleInput keyInput) {
100:
101: this .number = keyInput.readString();
102: }
103:
104: public void marshalPrimaryKey(TupleOutput keyOutput) {
105:
106: keyOutput.writeString(this .number);
107: }
108:
109: public boolean marshalSecondaryKey(String keyName,
110: TupleOutput keyOutput) {
111:
112: throw new UnsupportedOperationException(keyName);
113: }
114: }
|