Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

MarshalledObject.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: MarshalledObject.java,v 12.1 2005/01/31 19:27:35 mark Exp $
00008  */
00009 
00010 package com.sleepycat.bind.serial.test;
00011 
00012 import java.io.Serializable;
00013 
00014 import com.sleepycat.bind.tuple.MarshalledTupleKeyEntity;
00015 import com.sleepycat.bind.tuple.TupleInput;
00016 import com.sleepycat.bind.tuple.TupleOutput;
00017 
00021 public class MarshalledObject
00022     implements Serializable, MarshalledTupleKeyEntity {
00023 
00024     private String data;
00025     private transient String primaryKey;
00026     private String indexKey1;
00027     private String indexKey2;
00028 
00029     public MarshalledObject(String data, String primaryKey,
00030                             String indexKey1, String indexKey2) {
00031         this.data = data;
00032         this.primaryKey = primaryKey;
00033         this.indexKey1 = indexKey1;
00034         this.indexKey2 = indexKey2;
00035     }
00036 
00037     public boolean equals(Object o) {
00038 
00039         try {
00040             MarshalledObject other = (MarshalledObject) o;
00041 
00042             return this.data.equals(other.data) &&
00043                    this.primaryKey.equals(other.primaryKey) &&
00044                    this.indexKey1.equals(other.indexKey1) &&
00045                    this.indexKey2.equals(other.indexKey2);
00046         } catch (Exception e) {
00047             return false;
00048         }
00049     }
00050 
00051     public String getData() {
00052 
00053         return data;
00054     }
00055 
00056     public String getPrimaryKey() {
00057 
00058         return primaryKey;
00059     }
00060 
00061     public String getIndexKey1() {
00062 
00063         return indexKey1;
00064     }
00065 
00066     public String getIndexKey2() {
00067 
00068         return indexKey2;
00069     }
00070 
00071     public int expectedKeyLength() {
00072 
00073         return primaryKey.length() + 1;
00074     }
00075 
00076     public void marshalPrimaryKey(TupleOutput keyOutput) {
00077 
00078         keyOutput.writeString(primaryKey);
00079     }
00080 
00081     public void unmarshalPrimaryKey(TupleInput keyInput) {
00082 
00083         primaryKey = keyInput.readString();
00084     }
00085 
00086     public boolean marshalSecondaryKey(String keyName, TupleOutput keyOutput) {
00087 
00088         if ("1".equals(keyName)) {
00089             if (indexKey1.length() > 0) {
00090                 keyOutput.writeString(indexKey1);
00091                 return true;
00092             } else {
00093                 return false;
00094             }
00095         } else if ("2".equals(keyName)) {
00096             if (indexKey2.length() > 0) {
00097                 keyOutput.writeString(indexKey2);
00098                 return true;
00099             } else {
00100                 return false;
00101             }
00102         } else {
00103             throw new IllegalArgumentException("Unknown keyName: " + keyName);
00104         }
00105     }
00106 
00107     public boolean nullifyForeignKey(String keyName) {
00108 
00109         if ("1".equals(keyName)) {
00110             if (indexKey1.length() > 0) {
00111                 indexKey1 = "";
00112                 return true;
00113             } else {
00114                 return false;
00115             }
00116         } else if ("2".equals(keyName)) {
00117             if (indexKey2.length() > 0) {
00118                 indexKey2 = "";
00119                 return true;
00120             } else {
00121                 return false;
00122             }
00123         } else {
00124             throw new IllegalArgumentException("Unknown keyName: " + keyName);
00125         }
00126     }
00127 }
00128 

Generated on Sun Dec 25 12:14:52 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2