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:23 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.tuple.test;
010:
011: import com.sleepycat.bind.tuple.MarshalledTupleEntry;
012: import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
013: import com.sleepycat.bind.tuple.TupleInput;
014: import com.sleepycat.bind.tuple.TupleOutput;
015:
016: /**
017: * @author Mark Hayes
018: */
019: public class MarshalledObject implements MarshalledTupleEntry,
020: MarshalledTupleKeyEntity {
021:
022: private String data;
023: private String primaryKey;
024: private String indexKey1;
025: private String indexKey2;
026:
027: public MarshalledObject() {
028: }
029:
030: MarshalledObject(String data, String primaryKey, String indexKey1,
031: String indexKey2) {
032:
033: this .data = data;
034: this .primaryKey = primaryKey;
035: this .indexKey1 = indexKey1;
036: this .indexKey2 = indexKey2;
037: }
038:
039: String getData() {
040:
041: return data;
042: }
043:
044: String getPrimaryKey() {
045:
046: return primaryKey;
047: }
048:
049: String getIndexKey1() {
050:
051: return indexKey1;
052: }
053:
054: String getIndexKey2() {
055:
056: return indexKey2;
057: }
058:
059: int expectedDataLength() {
060:
061: return data.length() + 1 + indexKey1.length() + 1
062: + indexKey2.length() + 1;
063: }
064:
065: int expectedKeyLength() {
066:
067: return primaryKey.length() + 1;
068: }
069:
070: public void marshalEntry(TupleOutput dataOutput) {
071:
072: dataOutput.writeString(data);
073: dataOutput.writeString(indexKey1);
074: dataOutput.writeString(indexKey2);
075: }
076:
077: public void unmarshalEntry(TupleInput dataInput) {
078:
079: data = dataInput.readString();
080: indexKey1 = dataInput.readString();
081: indexKey2 = dataInput.readString();
082: }
083:
084: public void marshalPrimaryKey(TupleOutput keyOutput) {
085:
086: keyOutput.writeString(primaryKey);
087: }
088:
089: public void unmarshalPrimaryKey(TupleInput keyInput) {
090:
091: primaryKey = keyInput.readString();
092: }
093:
094: public boolean marshalSecondaryKey(String keyName,
095: TupleOutput keyOutput) {
096:
097: if ("1".equals(keyName)) {
098: if (indexKey1.length() > 0) {
099: 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: "
113: + keyName);
114: }
115: }
116:
117: public boolean nullifyForeignKey(String keyName) {
118:
119: if ("1".equals(keyName)) {
120: if (indexKey1.length() > 0) {
121: indexKey1 = "";
122: return true;
123: } else {
124: return false;
125: }
126: } else if ("2".equals(keyName)) {
127: if (indexKey1.length() > 0) {
128: indexKey2 = "";
129: return true;
130: } else {
131: return false;
132: }
133: } else {
134: throw new IllegalArgumentException("Unknown keyName: "
135: + keyName);
136: }
137: }
138: }
|