KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > MOFIDInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*; /* for MOFID */
21 import org.netbeans.mdr.persistence.SinglevaluedIndex;
22 import org.netbeans.mdr.persistence.Storage;
23 import org.netbeans.mdr.persistence.MOFID;
24 import org.netbeans.mdr.util.Logger;
25
26 /**
27  * EntryTypeInfo implementation for type MOFID
28  *
29  * @author Dana Bergen
30  * @version 1.0
31  */

32
33 public class MOFIDInfo extends EntryTypeInfo{
34
35     private BtreeStorage storage;
36
37     MOFIDInfo(Storage storage) {
38         this.storage = (BtreeStorage) storage;
39     }
40         
41     public String JavaDoc typeName() {
42         return "MOFID";
43     }
44
45     /**
46      * Compares two MOFIDs byte-by-byte.
47      *
48      * @param key byte array containing search key
49      * @param buffer buffer containing target key
50      * @param offset offset into buffer of target key
51      * @param length should always be MOFID.LENGTH
52      *
53      * @return Returns one of:
54      * <p>EQUAL if the two keys are equal
55      * <p>GREATER if search key is greater than target key
56      * <p>LESS if search key is less than target key
57      */

58     public byte compare(byte[] key, byte[] buffer, int offset, int targetLength) {
59
60     byte k, b;
61     for (int i = 0; i < MOFID.LENGTH; i++) {
62         k = key[i];
63         b = buffer[offset+i];
64         if (k < b) {
65             return LESS;
66         } else if (k > b) {
67             return GREATER;
68         }
69         }
70         return EQUAL;
71     }
72
73     public int getLength() {
74         return MOFID.LENGTH;
75     }
76
77     public boolean isFixedLength() {
78         return true;
79     }
80
81     public byte[] toBuffer(Object JavaDoc object) {
82
83         try {
84             MOFID m = (MOFID) object;
85             if (m != null) {
86                 byte[] mofBytes = new byte[MOFID.LENGTH];
87                 Converter.writeLong(mofBytes, 0, m.getSerialNumber());
88                 String JavaDoc storageId = m.getStorageID ();
89                 int s;
90                 if (storage.getStorageId ().equals(storageId)) {
91                     s = BtreeFactory.SAME_PREFIX_CODE;
92                 } else if (BtreeFactory.INTERNAL_PREFIX.equals (storageId)) {
93                     s = BtreeFactory.INTERNAL_PREFIX_CODE;
94                 } else {
95                     s = storage.storageIdToNumber (storageId);
96                 }
97                 Converter.writeShort(mofBytes, 0, (short)s);
98                 return mofBytes;
99             }
100         } catch (Exception JavaDoc e) {
101             Logger.getDefault().notify(Logger.INFORMATIONAL, e);
102         }
103     return null;
104     }
105
106     public Object JavaDoc fromBuffer(byte[] buffer) {
107     try {
108             int storageNumber = Converter.readShort (buffer, 0);
109             String JavaDoc storageId = null;
110             switch (storageNumber) {
111                 case BtreeFactory.INTERNAL_PREFIX_CODE:
112                     storageId = BtreeFactory.INTERNAL_PREFIX;
113                 break;
114                 case BtreeFactory.SAME_PREFIX_CODE:
115                     storageId = storage.getStorageId ();
116                 break;
117                 default:
118                     storageId = storage.numberToStorageId (storageNumber);
119             }
120             buffer [0] = 0;
121             buffer [1] = 0;
122             long serialNumber = Converter.readLong (buffer, 0);
123             return new MOFID (serialNumber, storageId);
124     } catch (Exception JavaDoc e) {
125             Logger.getDefault().notify(Logger.INFORMATIONAL, e);
126             return null;
127         }
128     }
129    
130     public Object JavaDoc objectFromBuffer(byte[] buffer, SinglevaluedIndex repos) {
131         try {
132             return storage.resolveObject((MOFID) fromBuffer(buffer));
133         } catch (Exception JavaDoc e) {
134             Logger.getDefault().notify(Logger.INFORMATIONAL, e);
135         }
136         return null;
137     }
138 }
139
Popular Tags