Skip to content

Commit 5560ee5

Browse files
committed
Use records where appropriate
This introduces some breaking changes due to the accessor method naming pattern.
1 parent ffc744b commit 5560ee5

File tree

10 files changed

+121
-264
lines changed

10 files changed

+121
-264
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ CHANGELOG
1010
2GB ByteBuffer limit. Files under 2GB continue to use a single ByteBuffer
1111
for optimal performance. Requested by nonetallt. GitHub #154. Fixed by
1212
Silvano Cerza. GitHub #289.
13+
* `DatabaseRecord`, `Metadata`, `Network`, and internal `DecodedValue` classes
14+
have been converted to records. The following API changes were made:
15+
* `DatabaseRecord.getData()` and `DatabaseRecord.getNetwork()` have been
16+
replaced with record accessor methods `data()` and `network()`.
17+
* `Metadata.getBuildDate()` has been renamed to `buildDate()` to follow record
18+
naming conventions. All other simple getter methods on `Metadata` (e.g.,
19+
`getBinaryFormatMajorVersion()`, `getDatabaseType()`, etc.) have been
20+
replaced with their corresponding record accessor methods (e.g.,
21+
`binaryFormatMajorVersion()`, `databaseType()`, etc.).
22+
* `Network.getNetworkAddress()` and `Network.getPrefixLength()` have been
23+
replaced with record accessor methods `networkAddress()` and `prefixLength()`.
1324

1425
3.2.0 (2025-05-28)
1526
------------------

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
* lookup.
88
*
99
* @param <T> the type to deserialize the returned value to
10+
* @param data the data for the record in the database. The record will be
11+
* {@code null} if there was no data for the address in the
12+
* database.
13+
* @param network the network associated with the record in the database. This is
14+
* the largest network where all of the IPs in the network have the same
15+
* data.
1016
*/
11-
public final class DatabaseRecord<T> {
12-
private final T data;
13-
private final Network network;
14-
17+
public record DatabaseRecord<T>(T data, Network network) {
1518
/**
1619
* Create a new record.
1720
*
@@ -20,25 +23,6 @@ public final class DatabaseRecord<T> {
2023
* @param prefixLength the network prefix length associated with the record in the database.
2124
*/
2225
public DatabaseRecord(T data, InetAddress ipAddress, int prefixLength) {
23-
this.data = data;
24-
this.network = new Network(ipAddress, prefixLength);
25-
}
26-
27-
/**
28-
* @return the data for the record in the database. The record will be
29-
* <code>null</code> if there was no data for the address in the
30-
* database.
31-
*/
32-
public T getData() {
33-
return data;
34-
}
35-
36-
/**
37-
* @return the network associated with the record in the database. This is
38-
* the largest network where all of the IPs in the network have the same
39-
* data.
40-
*/
41-
public Network getNetwork() {
42-
return network;
26+
this(data, new Network(ipAddress, prefixLength));
4327
}
4428
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
/**
44
* {@code DecodedValue} is a wrapper for the decoded value and the number of bytes used
55
* to decode it.
6+
*
7+
* @param value the decoded value
68
*/
7-
public final class DecodedValue {
8-
final Object value;
9-
10-
DecodedValue(Object value) {
11-
this.value = value;
12-
}
13-
14-
Object getValue() {
15-
return value;
16-
}
17-
}
9+
record DecodedValue(Object value) {}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ <T> T decode(long offset, Class<T> cls) throws IOException {
6969
}
7070

7171
this.buffer.position(offset);
72-
return cls.cast(decode(cls, null).getValue());
72+
return cls.cast(decode(cls, null).value());
7373
}
7474

7575
private <T> DecodedValue decode(CacheKey<T> key) throws IOException {
@@ -300,7 +300,7 @@ private <T, V> List<V> decodeArray(
300300
}
301301

302302
for (int i = 0; i < size; i++) {
303-
Object e = this.decode(elementClass, null).getValue();
303+
Object e = this.decode(elementClass, null).value();
304304
array.add(elementClass.cast(e));
305305
}
306306

@@ -360,8 +360,8 @@ private <T, V> Map<String, V> decodeMapIntoMap(
360360
}
361361

362362
for (int i = 0; i < size; i++) {
363-
String key = (String) this.decode(String.class, null).getValue();
364-
Object value = this.decode(valueClass, null).getValue();
363+
String key = (String) this.decode(String.class, null).value();
364+
Object value = this.decode(valueClass, null).value();
365365
try {
366366
map.put(key, valueClass.cast(value));
367367
} catch (ClassCastException e) {
@@ -412,7 +412,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
412412

413413
Object[] parameters = new Object[parameterTypes.length];
414414
for (int i = 0; i < size; i++) {
415-
String key = (String) this.decode(String.class, null).getValue();
415+
String key = (String) this.decode(String.class, null).value();
416416

417417
Integer parameterIndex = parameterIndexes.get(key);
418418
if (parameterIndex == null) {
@@ -424,7 +424,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
424424
parameters[parameterIndex] = this.decode(
425425
parameterTypes[parameterIndex],
426426
parameterGenericTypes[parameterIndex]
427-
).getValue();
427+
).value();
428428
}
429429

430430
try {

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

Lines changed: 39 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -7,171 +7,62 @@
77

88
/**
99
* {@code Metadata} holds data associated with the database itself.
10+
*
11+
* @param binaryFormatMajorVersion The major version number for the database's
12+
* binary format.
13+
* @param binaryFormatMinorVersion The minor version number for the database's
14+
* binary format.
15+
* @param buildEpoch The date of the database build.
16+
* @param databaseType A string that indicates the structure of each
17+
* data record associated with an IP address.
18+
* The actual definition of these structures is
19+
* left up to the database creator.
20+
* @param languages List of languages supported by the database.
21+
* @param description Map from language code to description in that
22+
* language.
23+
* @param ipVersion Whether the database contains IPv4 or IPv6
24+
* address data. The only possible values are 4
25+
* and 6.
26+
* @param nodeCount The number of nodes in the search tree.
27+
* @param recordSize The number of bits in a record in the search
28+
* tree. Note that each node consists of two
29+
* records.
1030
*/
11-
public final class Metadata {
12-
private final int binaryFormatMajorVersion;
13-
private final int binaryFormatMinorVersion;
14-
15-
private final BigInteger buildEpoch;
16-
17-
private final String databaseType;
18-
19-
private final Map<String, String> description;
20-
21-
private final int ipVersion;
22-
23-
private final List<String> languages;
24-
25-
private final int nodeByteSize;
26-
27-
private final long nodeCount;
28-
29-
private final int recordSize;
30-
31-
private final long searchTreeSize;
32-
31+
public record Metadata(
32+
@MaxMindDbParameter(name = "binary_format_major_version") int binaryFormatMajorVersion,
33+
@MaxMindDbParameter(name = "binary_format_minor_version") int binaryFormatMinorVersion,
34+
@MaxMindDbParameter(name = "build_epoch") BigInteger buildEpoch,
35+
@MaxMindDbParameter(name = "database_type") String databaseType,
36+
@MaxMindDbParameter(name = "languages") List<String> languages,
37+
@MaxMindDbParameter(name = "description") Map<String, String> description,
38+
@MaxMindDbParameter(name = "ip_version") int ipVersion,
39+
@MaxMindDbParameter(name = "node_count") long nodeCount,
40+
@MaxMindDbParameter(name = "record_size") int recordSize
41+
) {
3342
/**
34-
* Constructs a {@code Metadata} object.
35-
*
36-
* @param binaryFormatMajorVersion The major version number for the database's
37-
* binary format.
38-
* @param binaryFormatMinorVersion The minor version number for the database's
39-
* binary format.
40-
* @param buildEpoch The date of the database build.
41-
* @param databaseType A string that indicates the structure of each
42-
* data record associated with an IP address.
43-
* The actual definition of these structures is
44-
* left up to the database creator.
45-
* @param languages List of languages supported by the database.
46-
* @param description Map from language code to description in that
47-
* language.
48-
* @param ipVersion Whether the database contains IPv4 or IPv6
49-
* address data. The only possible values are 4
50-
* and 6.
51-
* @param nodeCount The number of nodes in the search tree.
52-
* @param recordSize The number of bits in a record in the search
53-
* tree. Note that each node consists of two
54-
* records.
43+
* Compact constructor for the Metadata record.
5544
*/
5645
@MaxMindDbConstructor
57-
public Metadata(
58-
@MaxMindDbParameter(name = "binary_format_major_version") int binaryFormatMajorVersion,
59-
@MaxMindDbParameter(name = "binary_format_minor_version") int binaryFormatMinorVersion,
60-
@MaxMindDbParameter(name = "build_epoch") BigInteger buildEpoch,
61-
@MaxMindDbParameter(name = "database_type") String databaseType,
62-
@MaxMindDbParameter(name = "languages") List<String> languages,
63-
@MaxMindDbParameter(name = "description") Map<String, String> description,
64-
@MaxMindDbParameter(name = "ip_version") int ipVersion,
65-
@MaxMindDbParameter(name = "node_count") long nodeCount,
66-
@MaxMindDbParameter(name = "record_size") int recordSize) {
67-
this.binaryFormatMajorVersion = binaryFormatMajorVersion;
68-
this.binaryFormatMinorVersion = binaryFormatMinorVersion;
69-
this.buildEpoch = buildEpoch;
70-
this.databaseType = databaseType;
71-
this.languages = languages;
72-
this.description = description;
73-
this.ipVersion = ipVersion;
74-
this.nodeCount = nodeCount;
75-
this.recordSize = recordSize;
76-
77-
this.nodeByteSize = this.recordSize / 4;
78-
this.searchTreeSize = this.nodeCount * this.nodeByteSize;
79-
}
80-
81-
/**
82-
* @return the major version number for the database's binary format.
83-
*/
84-
public int getBinaryFormatMajorVersion() {
85-
return this.binaryFormatMajorVersion;
86-
}
87-
88-
/**
89-
* @return the minor version number for the database's binary format.
90-
*/
91-
public int getBinaryFormatMinorVersion() {
92-
return this.binaryFormatMinorVersion;
93-
}
46+
public Metadata {}
9447

9548
/**
9649
* @return the date of the database build.
9750
*/
98-
public Date getBuildDate() {
99-
return new Date(this.buildEpoch.longValue() * 1000);
100-
}
101-
102-
/**
103-
* @return a string that indicates the structure of each data record
104-
* associated with an IP address. The actual definition of these
105-
* structures is left up to the database creator.
106-
*/
107-
public String getDatabaseType() {
108-
return this.databaseType;
109-
}
110-
111-
/**
112-
* @return map from language code to description in that language.
113-
*/
114-
public Map<String, String> getDescription() {
115-
return this.description;
116-
}
117-
118-
/**
119-
* @return whether the database contains IPv4 or IPv6 address data. The only
120-
* possible values are 4 and 6.
121-
*/
122-
public int getIpVersion() {
123-
return this.ipVersion;
124-
}
125-
126-
/**
127-
* @return list of languages supported by the database.
128-
*/
129-
public List<String> getLanguages() {
130-
return this.languages;
51+
public Date buildDate() {
52+
return new Date(buildEpoch.longValue() * 1000);
13153
}
13254

13355
/**
13456
* @return the nodeByteSize
13557
*/
136-
int getNodeByteSize() {
137-
return this.nodeByteSize;
138-
}
139-
140-
/**
141-
* @return the number of nodes in the search tree.
142-
*/
143-
long getNodeCount() {
144-
return this.nodeCount;
145-
}
146-
147-
/**
148-
* @return the number of bits in a record in the search tree. Note that each
149-
* node consists of two records.
150-
*/
151-
int getRecordSize() {
152-
return this.recordSize;
58+
int nodeByteSize() {
59+
return recordSize / 4;
15360
}
15461

15562
/**
15663
* @return the searchTreeSize
15764
*/
158-
long getSearchTreeSize() {
159-
return this.searchTreeSize;
160-
}
161-
162-
/*
163-
* (non-Javadoc)
164-
*
165-
* @see java.lang.Object#toString()
166-
*/
167-
@Override
168-
public String toString() {
169-
return "Metadata [binaryFormatMajorVersion="
170-
+ this.binaryFormatMajorVersion + ", binaryFormatMinorVersion="
171-
+ this.binaryFormatMinorVersion + ", buildEpoch="
172-
+ this.buildEpoch + ", databaseType=" + this.databaseType
173-
+ ", description=" + this.description + ", ipVersion="
174-
+ this.ipVersion + ", nodeCount=" + this.nodeCount
175-
+ ", recordSize=" + this.recordSize + "]";
65+
long searchTreeSize() {
66+
return nodeCount * nodeByteSize();
17667
}
17768
}

0 commit comments

Comments
 (0)