Changed license to Creative Commons Zero (CC0).
[sixth-data.git] / src / main / java / eu / svjatoslav / sixth / data / store / file / EntryRecord.java
1 /*
2  * Sixth Data. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  *
5 */
6
7 package eu.svjatoslav.sixth.data.store.file;
8
9 import java.io.IOException;
10
11 class EntryRecord implements Comparable<EntryRecord> {
12     public static final int ENTRY_RECORD_LENGTH = 12;
13     final int id;
14     long location = 0;
15     int length = 0;
16
17     public EntryRecord(final FileDataStore dataStore, final int id)
18             throws IOException {
19
20         this.id = id;
21
22         final long entryRecordLocation = dataStore.entryAllocationTable
23                 .getEntryRecordLocation(id);
24
25         location = dataStore.readLong(entryRecordLocation);
26
27         if (isUsed())
28             length = dataStore.readInt(entryRecordLocation + 8);
29     }
30
31     public EntryRecord(final int id, final long location, final int length) {
32         this.id = id;
33         this.location = location;
34         this.length = length;
35     }
36
37     public void clear() {
38         location = 0;
39         length = 0;
40     }
41
42     @Override
43     public boolean equals(final Object o) {
44         if (o == null) return false;
45         return o instanceof EntryRecord && compareTo((EntryRecord) o) == 0;
46     }
47
48     @Override
49     public int compareTo(final EntryRecord o) {
50         if (location < o.location)
51             return -1;
52         if (location > o.location)
53             return 1;
54
55         if (id < o.id)
56             return -1;
57         if (id > o.id)
58             return 1;
59
60         return 0;
61     }
62
63     @Override
64     public int hashCode() {
65         int result = id;
66         result = 31 * result + (int) (location ^ (location >>> 32));
67         return result;
68     }
69
70     public boolean isUsed() {
71         return location != 0;
72
73     }
74
75     public void save(final FileDataStore dataStore) throws IOException {
76
77         final long entryRecordLocation = dataStore.entryAllocationTable
78                 .getEntryRecordLocation(id);
79
80         dataStore.writeLong(entryRecordLocation, location);
81         dataStore.writeInt(entryRecordLocation + 8, length);
82     }
83 }