KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > tuple > test > MarshalledObject


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

8
9 package com.sleepycat.bind.tuple.test;
10
11 import com.sleepycat.bind.tuple.MarshalledTupleEntry;
12 import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
13 import com.sleepycat.bind.tuple.TupleInput;
14 import com.sleepycat.bind.tuple.TupleOutput;
15
16 /**
17  * @author Mark Hayes
18  */

19 public class MarshalledObject
20     implements MarshalledTupleEntry, MarshalledTupleKeyEntity {
21
22     private String JavaDoc data;
23     private String JavaDoc primaryKey;
24     private String JavaDoc indexKey1;
25     private String JavaDoc indexKey2;
26
27     public MarshalledObject() {
28     }
29
30     MarshalledObject(String JavaDoc data, String JavaDoc primaryKey,
31                      String JavaDoc indexKey1, String JavaDoc indexKey2) {
32
33         this.data = data;
34         this.primaryKey = primaryKey;
35         this.indexKey1 = indexKey1;
36         this.indexKey2 = indexKey2;
37     }
38
39     String JavaDoc getData() {
40
41         return data;
42     }
43
44     String JavaDoc getPrimaryKey() {
45
46         return primaryKey;
47     }
48
49     String JavaDoc getIndexKey1() {
50
51         return indexKey1;
52     }
53
54     String JavaDoc getIndexKey2() {
55
56         return indexKey2;
57     }
58
59     int expectedDataLength() {
60
61         return data.length() + 1 +
62                indexKey1.length() + 1 +
63                indexKey2.length() + 1;
64     }
65
66     int expectedKeyLength() {
67
68         return primaryKey.length() + 1;
69     }
70
71     public void marshalEntry(TupleOutput dataOutput) {
72
73         dataOutput.writeString(data);
74         dataOutput.writeString(indexKey1);
75         dataOutput.writeString(indexKey2);
76     }
77
78     public void unmarshalEntry(TupleInput dataInput) {
79
80         data = dataInput.readString();
81         indexKey1 = dataInput.readString();
82         indexKey2 = dataInput.readString();
83     }
84
85     public void marshalPrimaryKey(TupleOutput keyOutput) {
86
87         keyOutput.writeString(primaryKey);
88     }
89
90     public void unmarshalPrimaryKey(TupleInput keyInput) {
91
92         primaryKey = keyInput.readString();
93     }
94
95     public boolean marshalSecondaryKey(String JavaDoc keyName, TupleOutput keyOutput) {
96
97         if ("1".equals(keyName)) {
98             if (indexKey1.length() > 0) {
99                 keyOutput.writeString(indexKey1);
100                 return true;
101             } else {
102                 return false;
103             }
104         } else if ("2".equals(keyName)) {
105             if (indexKey1.length() > 0) {
106                 keyOutput.writeString(indexKey2);
107                 return true;
108             } else {
109                 return false;
110             }
111         } else {
112             throw new IllegalArgumentException JavaDoc("Unknown keyName: " + keyName);
113         }
114     }
115
116     public boolean nullifyForeignKey(String JavaDoc keyName) {
117
118         if ("1".equals(keyName)) {
119             if (indexKey1.length() > 0) {
120                 indexKey1 = "";
121                 return true;
122             } else {
123                 return false;
124             }
125         } else if ("2".equals(keyName)) {
126             if (indexKey1.length() > 0) {
127                 indexKey2 = "";
128                 return true;
129             } else {
130                 return false;
131             }
132         } else {
133             throw new IllegalArgumentException JavaDoc("Unknown keyName: " + keyName);
134         }
135     }
136 }
137
138
Popular Tags