KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > serial > 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:36 bostic Exp $
7  */

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

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