KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*; /* for Converter */
22
23 /**
24  * EntryTypeInfo implementation for integer data.
25  */

26
27 public class IntInfo extends EntryTypeInfo {
28
29     public String JavaDoc typeName() {
30         return "Integer";
31     }
32
33     /**
34      * Store an integer in a byte array
35      *
36      * @param i Integer to be stored
37      *
38      * @return byte array containing the integer
39      */

40     public byte[] toBuffer(Object JavaDoc i) {
41
42     byte[] buffer = new byte[4];
43
44     if (!(i instanceof Integer JavaDoc)) {
45         return null;
46     }
47     Converter.writeInt(buffer, 0, ((Integer JavaDoc) i).intValue());
48     return buffer;
49     }
50
51     /**
52      * Reads an integer from a byte array
53      *
54      * @param buffer byte array containing an integer
55      *
56      * @return new Integer containing the value read from the byte array
57      */

58     public Object JavaDoc fromBuffer(byte[] buffer) {
59
60     return new Integer JavaDoc(Converter.readInt(buffer, 0));
61     }
62
63     /**
64      * Returns an int read from the 4 bytes starting at the location
65      * passed in.
66      *
67      */

68     public int fromBuffer(byte[] buffer, int offset) {
69
70     return Converter.readInt(buffer, offset);
71     }
72
73     /**
74      * Compares two integers stored in byte arrays
75      *
76      * @param key1Buffer byte array containing integer search key
77      * @param key2Buffer byte array containing integer target key
78      * @param offset offset into key2Buffer of target key
79      * @param length should always be 4
80      *
81      * @return Returns one of:
82      * <p>EQUAL if the two keys are equal
83      * <p>GREATER if search key is greater than target key
84      * <p>LESS if search key is less than target key
85      */

86     public byte compare(byte[] key1Buffer, byte[] key2Buffer, int offset,
87                                 int length) {
88     int key1, key2;
89
90     key1 = Converter.readInt(key1Buffer, 0);
91     key2 = Converter.readInt(key2Buffer, offset);
92
93     if (key1 == key2) {
94         return EQUAL;
95     } else if (key1 > key2) {
96         return GREATER;
97     } else {
98         return LESS;
99     }
100     }
101     
102     /**
103      * Returns the length of an integer
104      *
105      * @return always returns 4
106      */

107     public int getLength() {
108         return 4;
109     }
110
111     public boolean isFixedLength() {
112         return true;
113     }
114
115 }
116
Popular Tags