Skip to content

Commit bd42688

Browse files
oschwaldclaude
andcommitted
Fix raw uses of parameterized classes
Replace raw types with properly parameterized generics: - ConcurrentHashMap<Class, CachedConstructor> → ConcurrentHashMap<Class<?>, CachedConstructor<?>> - CacheKey → CacheKey<?> - Map t → Map<String, Object> t Add type-safe helper method getCachedConstructor() to encapsulate necessary unchecked cast with clear documentation of safety invariant. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 7f30a90 commit bd42688

File tree

6 files changed

+19
-12
lines changed

6 files changed

+19
-12
lines changed

sample/Benchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void bench(Reader r, int count, int seed) throws IOException {
4545
for (int i = 0; i < count; i++) {
4646
random.nextBytes(address);
4747
InetAddress ip = InetAddress.getByAddress(address);
48-
Map t = r.get(ip, Map.class);
48+
Map<String, Object> t = r.get(ip, Map.class);
4949
if (TRACE) {
5050
if (i % 50000 == 0) {
5151
System.out.println(i + " " + ip);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CHMCache implements NodeCache {
1313
private static final int DEFAULT_CAPACITY = 4096;
1414

1515
private final int capacity;
16-
private final ConcurrentHashMap<CacheKey, DecodedValue> cache;
16+
private final ConcurrentHashMap<CacheKey<?>, DecodedValue> cache;
1717
private boolean cacheFull = false;
1818

1919
/**
@@ -36,7 +36,7 @@ public CHMCache(int capacity) {
3636
}
3737

3838
@Override
39-
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
39+
public DecodedValue get(CacheKey<?> key, Loader loader) throws IOException {
4040
DecodedValue value = cache.get(key);
4141
if (value == null) {
4242
value = loader.load(key);

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Decoder {
3636

3737
private final ByteBuffer buffer;
3838

39-
private final ConcurrentHashMap<Class, CachedConstructor> constructors;
39+
private final ConcurrentHashMap<Class<?>, CachedConstructor<?>> constructors;
4040

4141
Decoder(NodeCache cache, ByteBuffer buffer, long pointerBase) {
4242
this(
@@ -51,7 +51,7 @@ class Decoder {
5151
NodeCache cache,
5252
ByteBuffer buffer,
5353
long pointerBase,
54-
ConcurrentHashMap<Class, CachedConstructor> constructors
54+
ConcurrentHashMap<Class<?>, CachedConstructor<?>> constructors
5555
) {
5656
this.cache = cache;
5757
this.pointerBase = pointerBase;
@@ -135,7 +135,7 @@ DecodedValue decodePointer(long pointer, Class<?> cls, java.lang.reflect.Type ge
135135
int targetOffset = (int) pointer;
136136
int position = buffer.position();
137137

138-
CacheKey key = new CacheKey(targetOffset, cls, genericType);
138+
CacheKey<?> key = new CacheKey<>(targetOffset, cls, genericType);
139139
DecodedValue o = cache.get(key, cacheLoader);
140140

141141
buffer.position(position);
@@ -371,7 +371,7 @@ private <T, V> Map<String, V> decodeMapIntoMap(
371371

372372
private <T> Object decodeMapIntoObject(int size, Class<T> cls)
373373
throws IOException {
374-
CachedConstructor<T> cachedConstructor = this.constructors.get(cls);
374+
CachedConstructor<T> cachedConstructor = getCachedConstructor(cls);
375375
Constructor<T> constructor;
376376
Class<?>[] parameterTypes;
377377
java.lang.reflect.Type[] parameterGenericTypes;
@@ -392,7 +392,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
392392

393393
this.constructors.put(
394394
cls,
395-
new CachedConstructor(
395+
new CachedConstructor<>(
396396
constructor,
397397
parameterTypes,
398398
parameterGenericTypes,
@@ -445,6 +445,13 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
445445
}
446446
}
447447

448+
private <T> CachedConstructor<T> getCachedConstructor(Class<T> cls) {
449+
// This cast is safe because we only put CachedConstructor<T> for Class<T> as the key
450+
@SuppressWarnings("unchecked")
451+
CachedConstructor<T> result = (CachedConstructor<T>) this.constructors.get(cls);
452+
return result;
453+
}
454+
448455
private static <T> Constructor<T> findConstructor(Class<T> cls)
449456
throws ConstructorNotFoundException {
450457
Constructor<?>[] constructors = cls.getConstructors();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private NoCache() {
1313
}
1414

1515
@Override
16-
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
16+
public DecodedValue get(CacheKey<?> key, Loader loader) throws IOException {
1717
return loader.load(key);
1818
}
1919

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Loader {
1818
* @throws IOException
1919
* if there is an error loading the value
2020
*/
21-
DecodedValue load(CacheKey key) throws IOException;
21+
DecodedValue load(CacheKey<?> key) throws IOException;
2222
}
2323

2424
/**
@@ -33,6 +33,6 @@ interface Loader {
3333
* @throws IOException
3434
* if there is an error loading the value
3535
*/
36-
DecodedValue get(CacheKey key, Loader loader) throws IOException;
36+
DecodedValue get(CacheKey<?> key, Loader loader) throws IOException;
3737

3838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class Reader implements Closeable {
2626
private final Metadata metadata;
2727
private final AtomicReference<BufferHolder> bufferHolderReference;
2828
private final NodeCache cache;
29-
private final ConcurrentHashMap<Class, CachedConstructor> constructors;
29+
private final ConcurrentHashMap<Class<?>, CachedConstructor<?>> constructors;
3030

3131
/**
3232
* The file mode to use when opening a MaxMind DB.

0 commit comments

Comments
 (0)