001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: MarshalledObject.java,v 1.20.2.2 2008/01/07 15:14:22 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.serial.test;
010:
011: import java.io.Serializable;
012:
013: import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
014: import com.sleepycat.bind.tuple.TupleInput;
015: import com.sleepycat.bind.tuple.TupleOutput;
016:
017: /**
018: * @author Mark Hayes
019: */
020: public class MarshalledObject implements Serializable,
021: MarshalledTupleKeyEntity {
022:
023: private String data;
024: private transient String primaryKey;
025: private String indexKey1;
026: private String indexKey2;
027:
028: public MarshalledObject(String data, String primaryKey,
029: String indexKey1, String indexKey2) {
030: this .data = data;
031: this .primaryKey = primaryKey;
032: this .indexKey1 = indexKey1;
033: this .indexKey2 = indexKey2;
034: }
035:
036: public boolean equals(Object o) {
037:
038: try {
039: MarshalledObject other = (MarshalledObject) o;
040:
041: return this .data.equals(other.data)
042: && this .primaryKey.equals(other.primaryKey)
043: && this .indexKey1.equals(other.indexKey1)
044: && this .indexKey2.equals(other.indexKey2);
045: } catch (Exception e) {
046: return false;
047: }
048: }
049:
050: public String getData() {
051:
052: return data;
053: }
054:
055: public String getPrimaryKey() {
056:
057: return primaryKey;
058: }
059:
060: public String getIndexKey1() {
061:
062: return indexKey1;
063: }
064:
065: public String getIndexKey2() {
066:
067: return indexKey2;
068: }
069:
070: public int expectedKeyLength() {
071:
072: return primaryKey.length() + 1;
073: }
074:
075: public void marshalPrimaryKey(TupleOutput keyOutput) {
076:
077: keyOutput.writeString(primaryKey);
078: }
079:
080: public void unmarshalPrimaryKey(TupleInput keyInput) {
081:
082: primaryKey = keyInput.readString();
083: }
084:
085: public boolean marshalSecondaryKey(String keyName,
086: TupleOutput keyOutput) {
087:
088: if ("1".equals(keyName)) {
089: if (indexKey1.length() > 0) {
090: keyOutput.writeString(indexKey1);
091: return true;
092: } else {
093: return false;
094: }
095: } else if ("2".equals(keyName)) {
096: if (indexKey2.length() > 0) {
097: keyOutput.writeString(indexKey2);
098: return true;
099: } else {
100: return false;
101: }
102: } else {
103: throw new IllegalArgumentException("Unknown keyName: "
104: + keyName);
105: }
106: }
107:
108: public boolean nullifyForeignKey(String keyName) {
109:
110: if ("1".equals(keyName)) {
111: if (indexKey1.length() > 0) {
112: indexKey1 = "";
113: return true;
114: } else {
115: return false;
116: }
117: } else if ("2".equals(keyName)) {
118: if (indexKey2.length() > 0) {
119: indexKey2 = "";
120: return true;
121: } else {
122: return false;
123: }
124: } else {
125: throw new IllegalArgumentException("Unknown keyName: "
126: + keyName);
127: }
128: }
129: }
|