Skip to content

Commit 6fbcf51

Browse files
committed
Use var for variables
I realize this is contentious among some Java programmers, but given that most people modifying this code will be familiar with this pattern from other languages (Go, C#, etc.), this seems like an improvement.
1 parent 58ff7a4 commit 6fbcf51

File tree

13 files changed

+305
-305
lines changed

13 files changed

+305
-305
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ final class BufferHolder {
3535
this.buffer = new SingleBuffer(buffer);
3636
} else {
3737
// Allocate chunks, read, and make read-only
38-
int fullChunks = (int) (size / chunkSize);
39-
int remainder = (int) (size % chunkSize);
40-
int totalChunks = fullChunks + (remainder > 0 ? 1 : 0);
41-
ByteBuffer[] buffers = new ByteBuffer[totalChunks];
38+
var fullChunks = (int) (size / chunkSize);
39+
var remainder = (int) (size % chunkSize);
40+
var totalChunks = fullChunks + (remainder > 0 ? 1 : 0);
41+
var buffers = new ByteBuffer[totalChunks];
4242

4343
for (int i = 0; i < fullChunks; i++) {
4444
buffers[i] = ByteBuffer.allocate(chunkSize);
@@ -47,9 +47,9 @@ final class BufferHolder {
4747
buffers[totalChunks - 1] = ByteBuffer.allocate(remainder);
4848
}
4949

50-
long totalRead = 0;
51-
for (ByteBuffer buffer : buffers) {
52-
int read = channel.read(buffer);
50+
var totalRead = 0L;
51+
for (var buffer : buffers) {
52+
var read = channel.read(buffer);
5353
if (read == -1) {
5454
break;
5555
}
@@ -90,23 +90,23 @@ final class BufferHolder {
9090
if (null == stream) {
9191
throw new NullPointerException("Unable to use a NULL InputStream");
9292
}
93-
List<ByteBuffer> chunks = new ArrayList<>();
94-
long total = 0;
95-
byte[] tmp = new byte[chunkSize];
93+
var chunks = new ArrayList<ByteBuffer>();
94+
var total = 0L;
95+
var tmp = new byte[chunkSize];
9696
int read;
9797

9898
while (-1 != (read = stream.read(tmp))) {
99-
ByteBuffer chunk = ByteBuffer.allocate(read);
99+
var chunk = ByteBuffer.allocate(read);
100100
chunk.put(tmp, 0, read);
101101
chunk.flip();
102102
chunks.add(chunk);
103103
total += read;
104104
}
105105

106106
if (total <= chunkSize) {
107-
byte[] data = new byte[(int) total];
108-
int pos = 0;
109-
for (ByteBuffer chunk : chunks) {
107+
var data = new byte[(int) total];
108+
var pos = 0;
109+
for (var chunk : chunks) {
110110
System.arraycopy(chunk.array(), 0, data, pos, chunk.capacity());
111111
pos += chunk.capacity();
112112
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public CHMCache(int capacity) {
3737

3838
@Override
3939
public DecodedValue get(CacheKey<?> key, Loader loader) throws IOException {
40-
DecodedValue value = cache.get(key);
40+
var value = cache.get(key);
4141
if (value == null) {
4242
value = loader.load(key);
4343
if (!cacheFull) {

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

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,26 @@ private <T> DecodedValue decode(CacheKey<T> key) throws IOException {
8787

8888
private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType)
8989
throws IOException {
90-
int ctrlByte = 0xFF & this.buffer.get();
90+
var ctrlByte = 0xFF & this.buffer.get();
9191

92-
Type type = Type.fromControlByte(ctrlByte);
92+
var type = Type.fromControlByte(ctrlByte);
9393

9494
// Pointers are a special case, we don't read the next 'size' bytes, we
9595
// use the size to determine the length of the pointer and then follow
9696
// it.
9797
if (type.equals(Type.POINTER)) {
98-
int pointerSize = ((ctrlByte >>> 3) & 0x3) + 1;
99-
int base = pointerSize == 4 ? (byte) 0 : (byte) (ctrlByte & 0x7);
100-
int packed = this.decodeInteger(base, pointerSize);
101-
long pointer = packed + this.pointerBase + POINTER_VALUE_OFFSETS[pointerSize];
98+
var pointerSize = ((ctrlByte >>> 3) & 0x3) + 1;
99+
var base = pointerSize == 4 ? (byte) 0 : (byte) (ctrlByte & 0x7);
100+
var packed = this.decodeInteger(base, pointerSize);
101+
var pointer = packed + this.pointerBase + POINTER_VALUE_OFFSETS[pointerSize];
102102

103103
return decodePointer(pointer, cls, genericType);
104104
}
105105

106106
if (type.equals(Type.EXTENDED)) {
107-
int nextByte = this.buffer.get();
107+
var nextByte = this.buffer.get();
108108

109-
int typeNum = nextByte + 7;
109+
var typeNum = nextByte + 7;
110110

111111
if (typeNum < 8) {
112112
throw new InvalidDatabaseException(
@@ -132,11 +132,11 @@ private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType
132132

133133
DecodedValue decodePointer(long pointer, Class<?> cls, java.lang.reflect.Type genericType)
134134
throws IOException {
135-
long targetOffset = pointer;
136-
long position = buffer.position();
135+
var targetOffset = pointer;
136+
var position = buffer.position();
137137

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

141141
buffer.position(position);
142142
return o;
@@ -154,7 +154,7 @@ private <T> Object decodeByType(
154154
case ARRAY:
155155
Class<?> elementClass = Object.class;
156156
if (genericType instanceof ParameterizedType ptype) {
157-
java.lang.reflect.Type[] actualTypes = ptype.getActualTypeArguments();
157+
var actualTypes = ptype.getActualTypeArguments();
158158
if (actualTypes.length == 1) {
159159
elementClass = (Class<?>) actualTypes[0];
160160
}
@@ -186,9 +186,9 @@ private <T> Object decodeByType(
186186
}
187187

188188
private String decodeString(long size) throws CharacterCodingException {
189-
long oldLimit = buffer.limit();
189+
var oldLimit = buffer.limit();
190190
buffer.limit(buffer.position() + size);
191-
String s = buffer.decode(utfDecoder);
191+
var s = buffer.decode(utfDecoder);
192192
buffer.limit(oldLimit);
193193
return s;
194194
}
@@ -234,7 +234,7 @@ static int decodeInteger(Buffer buffer, int base, int size) {
234234
}
235235

236236
private BigInteger decodeBigInteger(int size) {
237-
byte[] bytes = this.getByteArray(size);
237+
var bytes = this.getByteArray(size);
238238
return new BigInteger(1, bytes);
239239
}
240240

@@ -287,10 +287,10 @@ private <T, V> List<V> decodeArray(
287287
throw new DeserializationException(
288288
"No constructor found for the List: " + e.getMessage(), e);
289289
}
290-
Object[] parameters = {size};
290+
var parameters = new Object[]{size};
291291
try {
292292
@SuppressWarnings("unchecked")
293-
List<V> array2 = (List<V>) constructor.newInstance(parameters);
293+
var array2 = (List<V>) constructor.newInstance(parameters);
294294
array = array2;
295295
} catch (InstantiationException
296296
| IllegalAccessException
@@ -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).value();
303+
var e = this.decode(elementClass, null).value();
304304
array.add(elementClass.cast(e));
305305
}
306306

@@ -315,9 +315,9 @@ private <T> Object decodeMap(
315315
if (Map.class.isAssignableFrom(cls) || cls.equals(Object.class)) {
316316
Class<?> valueClass = Object.class;
317317
if (genericType instanceof ParameterizedType ptype) {
318-
java.lang.reflect.Type[] actualTypes = ptype.getActualTypeArguments();
318+
var actualTypes = ptype.getActualTypeArguments();
319319
if (actualTypes.length == 2) {
320-
Class<?> keyClass = (Class<?>) actualTypes[0];
320+
var keyClass = (Class<?>) actualTypes[0];
321321
if (!keyClass.equals(String.class)) {
322322
throw new DeserializationException("Map keys must be strings.");
323323
}
@@ -347,10 +347,10 @@ private <T, V> Map<String, V> decodeMapIntoMap(
347347
throw new DeserializationException(
348348
"No constructor found for the Map: " + e.getMessage(), e);
349349
}
350-
Object[] parameters = {size};
350+
var parameters = new Object[]{size};
351351
try {
352352
@SuppressWarnings("unchecked")
353-
Map<String, V> map2 = (Map<String, V>) constructor.newInstance(parameters);
353+
var map2 = (Map<String, V>) constructor.newInstance(parameters);
354354
map = map2;
355355
} catch (InstantiationException
356356
| IllegalAccessException
@@ -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).value();
364-
Object value = this.decode(valueClass, null).value();
363+
var key = (String) this.decode(String.class, null).value();
364+
var value = this.decode(valueClass, null).value();
365365
try {
366366
map.put(key, valueClass.cast(value));
367367
} catch (ClassCastException e) {
@@ -375,7 +375,7 @@ private <T, V> Map<String, V> decodeMapIntoMap(
375375

376376
private <T> Object decodeMapIntoObject(int size, Class<T> cls)
377377
throws IOException {
378-
CachedConstructor<T> cachedConstructor = getCachedConstructor(cls);
378+
var cachedConstructor = getCachedConstructor(cls);
379379
Constructor<T> constructor;
380380
Class<?>[] parameterTypes;
381381
java.lang.reflect.Type[] parameterGenericTypes;
@@ -388,9 +388,9 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
388388
parameterGenericTypes = constructor.getGenericParameterTypes();
389389

390390
parameterIndexes = new HashMap<>();
391-
Annotation[][] annotations = constructor.getParameterAnnotations();
391+
var annotations = constructor.getParameterAnnotations();
392392
for (int i = 0; i < constructor.getParameterCount(); i++) {
393-
String parameterName = getParameterName(cls, i, annotations[i]);
393+
var parameterName = getParameterName(cls, i, annotations[i]);
394394
parameterIndexes.put(parameterName, i);
395395
}
396396

@@ -410,13 +410,13 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
410410
parameterIndexes = cachedConstructor.parameterIndexes();
411411
}
412412

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

417-
Integer parameterIndex = parameterIndexes.get(key);
417+
var parameterIndex = parameterIndexes.get(key);
418418
if (parameterIndex == null) {
419-
long offset = this.nextValueOffset(this.buffer.position(), 1);
419+
var offset = this.nextValueOffset(this.buffer.position(), 1);
420420
this.buffer.position(offset);
421421
continue;
422422
}
@@ -434,9 +434,9 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
434434
| InvocationTargetException e) {
435435
throw new DeserializationException("Error creating object: " + e.getMessage(), e);
436436
} catch (IllegalArgumentException e) {
437-
StringBuilder sbErrors = new StringBuilder();
438-
for (String key : parameterIndexes.keySet()) {
439-
int index = parameterIndexes.get(key);
437+
var sbErrors = new StringBuilder();
438+
for (var key : parameterIndexes.keySet()) {
439+
var index = parameterIndexes.get(key);
440440
if (parameters[index] != null
441441
&& !parameters[index].getClass().isAssignableFrom(parameterTypes[index])) {
442442
sbErrors.append(" argument type mismatch in " + key + " MMDB Type: "
@@ -458,8 +458,8 @@ private <T> CachedConstructor<T> getCachedConstructor(Class<T> cls) {
458458

459459
private static <T> Constructor<T> findConstructor(Class<T> cls)
460460
throws ConstructorNotFoundException {
461-
Constructor<?>[] constructors = cls.getConstructors();
462-
for (Constructor<?> constructor : constructors) {
461+
var constructors = cls.getConstructors();
462+
for (var constructor : constructors) {
463463
if (constructor.getAnnotation(MaxMindDbConstructor.class) == null) {
464464
continue;
465465
}
@@ -477,11 +477,11 @@ private static <T> String getParameterName(
477477
int index,
478478
Annotation[] annotations
479479
) throws ParameterNotFoundException {
480-
for (Annotation annotation : annotations) {
480+
for (var annotation : annotations) {
481481
if (!annotation.annotationType().equals(MaxMindDbParameter.class)) {
482482
continue;
483483
}
484-
MaxMindDbParameter paramAnnotation = (MaxMindDbParameter) annotation;
484+
var paramAnnotation = (MaxMindDbParameter) annotation;
485485
return paramAnnotation.name();
486486
}
487487
throw new ParameterNotFoundException(
@@ -495,15 +495,15 @@ private long nextValueOffset(long offset, int numberToSkip)
495495
return offset;
496496
}
497497

498-
CtrlData ctrlData = this.getCtrlData(offset);
499-
int ctrlByte = ctrlData.ctrlByte();
500-
int size = ctrlData.size();
498+
var ctrlData = this.getCtrlData(offset);
499+
var ctrlByte = ctrlData.ctrlByte();
500+
var size = ctrlData.size();
501501
offset = ctrlData.offset();
502502

503-
Type type = ctrlData.type();
503+
var type = ctrlData.type();
504504
switch (type) {
505505
case POINTER:
506-
int pointerSize = ((ctrlByte >>> 3) & 0x3) + 1;
506+
var pointerSize = ((ctrlByte >>> 3) & 0x3) + 1;
507507
offset += pointerSize;
508508
break;
509509
case MAP:
@@ -531,15 +531,15 @@ private CtrlData getCtrlData(long offset)
531531
}
532532

533533
this.buffer.position(offset);
534-
int ctrlByte = 0xFF & this.buffer.get();
534+
var ctrlByte = 0xFF & this.buffer.get();
535535
offset++;
536536

537-
Type type = Type.fromControlByte(ctrlByte);
537+
var type = Type.fromControlByte(ctrlByte);
538538

539539
if (type.equals(Type.EXTENDED)) {
540-
int nextByte = this.buffer.get();
540+
var nextByte = this.buffer.get();
541541

542-
int typeNum = nextByte + 7;
542+
var typeNum = nextByte + 7;
543543

544544
if (typeNum < 8) {
545545
throw new InvalidDatabaseException(
@@ -552,9 +552,9 @@ private CtrlData getCtrlData(long offset)
552552
offset++;
553553
}
554554

555-
int size = ctrlByte & 0x1f;
555+
var size = ctrlByte & 0x1f;
556556
if (size >= 29) {
557-
int bytesToRead = size - 28;
557+
var bytesToRead = size - 28;
558558
offset += bytesToRead;
559559
size = switch (size) {
560560
case 29 -> 29 + (0xFF & buffer.get());
@@ -571,7 +571,7 @@ private byte[] getByteArray(int length) {
571571
}
572572

573573
private static byte[] getByteArray(Buffer buffer, int length) {
574-
byte[] bytes = new byte[length];
574+
var bytes = new byte[length];
575575
buffer.get(bytes);
576576
return bytes;
577577
}

0 commit comments

Comments
 (0)