KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > TableKey


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import java.lang.ref.*;
4
5 import com.daffodilwoods.database.resource.*;
6
7 /**
8  * It is the Key by which we identify a particular record in database File. It is made of cluster address
9  * and recordNumber in that particular cluster.
10
11  */

12
13
14 public class TableKey implements Comparable JavaDoc{
15
16   /**
17    * start address of cluster.
18    */

19   public int startAddress;
20   /**
21    * record number of the record in cluster whose start address is stored in it.
22    */

23   public short recordNumber;
24   /**
25    * weak reference of cluster whose start address is stored in it.
26    */

27   public WeakReference cluster;
28
29    private static WeakReference nullWeakReference = new WeakReference(null);
30
31    public TableKey(int startAddress0,short recordNumber0) throws DException {
32       cluster = nullWeakReference;
33       startAddress = startAddress0;
34       recordNumber = recordNumber0;
35    }
36
37    public TableKey(WeakReference cluster0,int startAddress0,short recordNumber0) throws DException {
38       cluster = cluster0;
39       startAddress = startAddress0;
40       recordNumber = recordNumber0;
41    }
42
43
44    public int hashCode() {
45        long value = startAddress*11 + recordNumber*13;
46        return (int)(value ^ (value >> 32));
47    }
48    /**
49     * To check for equality of object passed to it.
50     * It checks recordNuber and startAddress and returns false if object is not a instance of table key.
51     * @param object Object - to be compared.
52     * @return boolean - true if start address and record number is same.
53     */

54    public boolean equals(Object JavaDoc object) {
55       if(!(object instanceof TableKey))
56          return false;
57       TableKey tk = (TableKey)object;
58       if(tk == null)
59          return false;
60       return tk.startAddress == startAddress && recordNumber == tk.recordNumber;
61    }
62
63    public String JavaDoc toString() {
64       return startAddress + " -- " + recordNumber;
65    }
66    /**
67     * To compare a object passsed with itself and returns -1 if
68     * start address is less than other wise 1 and if both are equals
69     * than decision is taken on record number's difference.
70     *
71     * @param key Object - tableKey to be compared.
72     * @return int - difference between passed key and itself.
73     */

74    public int compareTo(Object JavaDoc key) {
75       TableKey object = (TableKey)key;
76       long diff = object.startAddress - startAddress;
77       if(diff == 0)
78          diff = object.recordNumber - recordNumber;
79       int cmp = diff < 0 ? -1 : diff > 0 ? 1 : 0;
80       return cmp;
81    }
82    /**
83     * @return int start address of cluster of which this table key.
84     */

85    public int getStartAddress() {
86        return startAddress;
87    }
88    /**
89     * @return short record number of table key.
90     */

91    public short getRecordNumber() {
92        return recordNumber;
93    }
94
95    /**
96     *
97     * @param cluster0 WeakReference of cluster which is to be seted.
98     */

99    public void setCluster(WeakReference cluster0) {
100        cluster = cluster0;
101    }
102 }
103
Popular Tags