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.7 2008/01/08 20:58:55 bostic Exp $
7 */
8
9package com.sleepycat.bind.serial.test;
10
11import java.io.Serializable;
12
13import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
14import com.sleepycat.bind.tuple.TupleInput;
15import com.sleepycat.bind.tuple.TupleOutput;
16
17/**
18 * @author Mark Hayes
19 */
20public class MarshalledObject
21    implements Serializable, MarshalledTupleKeyEntity {
22
23    private String data;
24    private transient String primaryKey;
25    private String indexKey1;
26    private String indexKey2;
27
28    public MarshalledObject(String data, String primaryKey,
29                            String indexKey1, String indexKey2) {
30        this.data = data;
31        this.primaryKey = primaryKey;
32        this.indexKey1 = indexKey1;
33        this.indexKey2 = indexKey2;
34    }
35
36    public boolean equals(Object 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 e) {
46            return false;
47        }
48    }
49
50    public String getData() {
51
52        return data;
53    }
54
55    public String getPrimaryKey() {
56
57        return primaryKey;
58    }
59
60    public String getIndexKey1() {
61
62        return indexKey1;
63    }
64
65    public String 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 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("Unknown keyName: " + keyName);
103        }
104    }
105
106    public boolean nullifyForeignKey(String 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("Unknown keyName: " + keyName);
124        }
125    }
126}
127