KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DatabaseId


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseId.java,v 1.32 2006/12/04 18:47:41 cwl Exp $
7  */

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.UnsupportedEncodingException JavaDoc;
12 import java.nio.ByteBuffer JavaDoc;
13
14 import com.sleepycat.je.DatabaseException;
15 import com.sleepycat.je.log.LogReadable;
16 import com.sleepycat.je.log.LogUtils;
17 import com.sleepycat.je.log.LogWritable;
18
19 /**
20  * DatabaseImpl Ids are wrapped in a class so they can be logged.
21  */

22 public class DatabaseId implements Comparable JavaDoc, LogWritable, LogReadable {
23
24     /**
25      * Static log size used by LNLogEntry.getStaticLogSize.
26      */

27     public static final int LOG_SIZE = LogUtils.INT_BYTES;
28
29     /**
30      * The unique id of this database.
31      */

32     private int id;
33
34     /**
35      *
36      */

37     public DatabaseId(int id) {
38         this.id = id;
39     }
40
41     /**
42      * Uninitialized database id, for logging.
43      */

44     public DatabaseId() {
45     }
46
47     /**
48      * @return id value
49      */

50     public int getId() {
51     return id;
52     }
53
54     /**
55      * @return id as bytes, for use as a key
56      */

57     public byte[] getBytes()
58     throws DatabaseException {
59
60     try {
61         return toString().getBytes("UTF-8");
62     } catch (UnsupportedEncodingException JavaDoc UEE) {
63         throw new DatabaseException(UEE);
64     }
65     }
66
67     /**
68      * Compare two DatabaseImpl Id's.
69      */

70     public boolean equals(Object JavaDoc obj) {
71         if (this == obj) {
72             return true;
73         }
74
75         if (!(obj instanceof DatabaseId)) {
76             return false;
77         }
78
79     return ((DatabaseId) obj).id == id;
80     }
81
82     public int hashCode() {
83     return id;
84     }
85
86     public String JavaDoc toString() {
87         return Integer.toString(id);
88     }
89
90     /**
91      * see Comparable#compareTo
92      */

93     public int compareTo(Object JavaDoc o) {
94     if (o == null) {
95         throw new NullPointerException JavaDoc();
96     }
97
98         DatabaseId argId = (DatabaseId) o;
99         if (id == argId.id) {
100             return 0;
101         } else if (id > argId.id) {
102             return 1;
103         } else {
104             return -1;
105         }
106     }
107
108     /*
109      * Logging support.
110      */

111
112     /**
113      * @see LogWritable#getLogSize
114      */

115     public int getLogSize() {
116         return LOG_SIZE;
117     }
118
119     /**
120      * @see LogWritable#writeToLog
121      */

122     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
123         LogUtils.writeInt(logBuffer, id);
124     }
125
126     /**
127      * @see LogReadable#readFromLog
128      */

129     public void readFromLog(ByteBuffer JavaDoc itemBuffer, byte entryTypeVersion) {
130         id = LogUtils.readInt(itemBuffer);
131     }
132
133     /**
134      * @see LogReadable#dumpLog
135      */

136     public void dumpLog(StringBuffer JavaDoc sb, boolean verbose) {
137         sb.append("<dbId id=\"");
138         sb.append(id);
139         sb.append("\"/>");
140     }
141
142     /**
143      * @see LogReadable#logEntryIsTransactional
144      */

145     public boolean logEntryIsTransactional() {
146     return false;
147     }
148
149     /**
150      * @see LogReadable#getTransactionId
151      */

152     public long getTransactionId() {
153     return 0;
154     }
155 }
156
Popular Tags