1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: MarshalledObject.java,v 12.6 2008/01/08 20:58:56 bostic Exp $
7 */
8
9package com.sleepycat.bind.tuple.test;
10
11import com.sleepycat.bind.tuple.MarshalledTupleEntry;
12import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
13import com.sleepycat.bind.tuple.TupleInput;
14import com.sleepycat.bind.tuple.TupleOutput;
15
16/**
17 * @author Mark Hayes
18 */
19public class MarshalledObject
20    implements MarshalledTupleEntry, MarshalledTupleKeyEntity {
21
22    private String data;
23    private String primaryKey;
24    private String indexKey1;
25    private String indexKey2;
26
27    public MarshalledObject() {
28    }
29
30    MarshalledObject(String data, String primaryKey,
31                     String indexKey1, String indexKey2) {
32
33        this.data = data;
34        this.primaryKey = primaryKey;
35        this.indexKey1 = indexKey1;
36        this.indexKey2 = indexKey2;
37    }
38
39    String getData() {
40
41        return data;
42    }
43
44    String getPrimaryKey() {
45
46        return primaryKey;
47    }
48
49    String getIndexKey1() {
50
51        return indexKey1;
52    }
53
54    String 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 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("Unknown keyName: " + keyName);
113        }
114    }
115
116    public boolean nullifyForeignKey(String 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("Unknown keyName: " + keyName);
134        }
135    }
136}
137
138