Skip to content

Commit 8824603

Browse files
oschwaldclaude
andcommitted
Convert CacheKey to record
Converts the CacheKey class to use Java 17 records, reducing boilerplate and improving memory layout. Updates all getter method calls to use record accessors and adds proper Javadoc for record parameters. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0d7b549 commit 8824603

File tree

2 files changed

+7
-59
lines changed

2 files changed

+7
-59
lines changed

src/main/java/com/maxmind/db/CacheKey.java

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,9 @@
66
* of the value.
77
*
88
* @param <T> the type of value
9+
* @param offset the offset of the value in the database file
10+
* @param cls the class of the value
11+
* @param type the type of the value
912
*/
10-
public final class CacheKey<T> {
11-
private final int offset;
12-
private final Class<T> cls;
13-
private final java.lang.reflect.Type type;
14-
15-
CacheKey(int offset, Class<T> cls, java.lang.reflect.Type type) {
16-
this.offset = offset;
17-
this.cls = cls;
18-
this.type = type;
19-
}
20-
21-
int getOffset() {
22-
return this.offset;
23-
}
24-
25-
Class<T> getCls() {
26-
return this.cls;
27-
}
28-
29-
java.lang.reflect.Type getType() {
30-
return this.type;
31-
}
32-
33-
@Override
34-
public boolean equals(Object o) {
35-
if (o == null) {
36-
return false;
37-
}
38-
39-
CacheKey other = (CacheKey) o;
40-
41-
if (this.offset != other.offset) {
42-
return false;
43-
}
44-
45-
if (this.cls == null) {
46-
if (other.cls != null) {
47-
return false;
48-
}
49-
} else if (!this.cls.equals(other.cls)) {
50-
return false;
51-
}
52-
53-
if (this.type == null) {
54-
return other.type == null;
55-
}
56-
return this.type.equals(other.type);
57-
}
58-
59-
@Override
60-
public int hashCode() {
61-
int result = offset;
62-
result = 31 * result + (cls == null ? 0 : cls.hashCode());
63-
result = 31 * result + (type == null ? 0 : type.hashCode());
64-
return result;
65-
}
13+
public record CacheKey<T>(int offset, Class<T> cls, java.lang.reflect.Type type) {
6614
}

src/main/java/com/maxmind/db/Decoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ <T> T decode(int offset, Class<T> cls) throws IOException {
7373
}
7474

7575
private <T> DecodedValue decode(CacheKey<T> key) throws IOException {
76-
int offset = key.getOffset();
76+
int offset = key.offset();
7777
if (offset >= this.buffer.capacity()) {
7878
throw new InvalidDatabaseException(
7979
"The MaxMind DB file's data section contains bad data: "
8080
+ "pointer larger than the database.");
8181
}
8282

8383
this.buffer.position(offset);
84-
Class<T> cls = key.getCls();
85-
return decode(cls, key.getType());
84+
Class<T> cls = key.cls();
85+
return decode(cls, key.type());
8686
}
8787

8888
private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType)

0 commit comments

Comments
 (0)