1
0
mirror of https://github.com/google/nomulus synced 2026-01-08 15:21:46 +00:00

Bring codebase up to more recent Java standards (#2422)

This includes using the new switch format (though IntelliJ does not yet
understand patterns including default so those aren't used), multiline strings,
replacing some unnecessary type declarations with <>, converting some classes to
records, replacing some Guava predicates with native Java code, and some other
miscellaneous Code Inspection fixes.
This commit is contained in:
Ben McIlwain
2024-05-01 16:48:38 -04:00
committed by GitHub
parent 570618705e
commit 4b6ade0b14
154 changed files with 1454 additions and 1535 deletions

View File

@@ -441,7 +441,7 @@ public class CidrAddressBlock implements Iterable<InetAddress>, Serializable {
@Override
public Iterator<InetAddress> iterator() {
return new AbstractSequentialIterator<InetAddress>(ip) {
return new AbstractSequentialIterator<>(ip) {
@Override
protected InetAddress computeNext(InetAddress previous) {
if (InetAddresses.isMaximum(previous)) {
@@ -463,11 +463,10 @@ public class CidrAddressBlock implements Iterable<InetAddress>, Serializable {
@Override
public boolean equals(@Nullable Object o) {
if (!(o instanceof CidrAddressBlock)) {
if (!(o instanceof CidrAddressBlock cidr)) {
return false;
}
CidrAddressBlock cidr = (CidrAddressBlock) o;
return ip.equals(cidr.ip) && (netmask == cidr.netmask);
}

View File

@@ -68,8 +68,8 @@ public final class Concurrent {
if (threadCount == 1) {
return items.stream().map(funk).collect(toImmutableList());
}
ExecutorService executor = newFixedThreadPool(threadCount);
try {
;
try (ExecutorService executor = newFixedThreadPool(threadCount)) {
List<Future<B>> futures = new ArrayList<>();
for (final A item : items) {
futures.add(executor.submit(() -> funk.apply(item)));
@@ -83,8 +83,6 @@ public final class Concurrent {
}
}
return results.build();
} finally {
executor.shutdownNow();
}
}

View File

@@ -33,20 +33,10 @@ import javax.annotation.Nullable;
public final class DiffUtils {
/**
* A helper class to store the two sides of a diff. If both sides are Sets then they will be
* A helper record to store the two sides of a diff. If both sides are Sets then they will be
* diffed, otherwise the two objects are toStringed in Collection format "[a, b]".
*/
private static class DiffPair {
@Nullable
final Object a;
@Nullable
final Object b;
DiffPair(@Nullable Object a, @Nullable Object b) {
this.a = a;
this.b = b;
}
private record DiffPair(@Nullable Object a, @Nullable Object b) {
@Override
public String toString() {
@@ -133,10 +123,9 @@ public final class DiffUtils {
Object value = entry.getValue();
if (value instanceof Map) {
output = prettyPrintDiffedMap((Map<?, ?>) entry.getValue(), newPath);
} else if (value instanceof DiffPair
} else if (value instanceof DiffPair pair
&& ((DiffPair) value).a instanceof Set
&& ((DiffPair) value).b instanceof Set) {
DiffPair pair = ((DiffPair) value);
String prettyLineDiff = prettyPrintSetDiff((Set<?>) pair.a, (Set<?>) pair.b) + "\n";
output = newPath + (prettyLineDiff.startsWith("\n") ? ":" : ": ") + prettyLineDiff;
} else {

View File

@@ -68,8 +68,7 @@ public final class JdkLoggerConfig {
* in all expected use cases, so it should be okay to retain all of them for the life of a task.
*/
// TODO(dbeaumont): Reassess the risk of memory leaks here and decide what to do about it.
private static final Map<String, JdkLoggerConfig> strongRefMap =
new ConcurrentHashMap<String, JdkLoggerConfig>();
private static final Map<String, JdkLoggerConfig> strongRefMap = new ConcurrentHashMap<>();
/** Delegate logger. */
private final Logger logger;

View File

@@ -32,7 +32,7 @@ public class NullIgnoringCollectionBuilder<E, B extends ImmutableCollection.Buil
public static <E2, B2 extends ImmutableCollection.Builder<E2>>
NullIgnoringCollectionBuilder<E2, B2> create(B2 builder) {
return new NullIgnoringCollectionBuilder<E2, B2>(builder);
return new NullIgnoringCollectionBuilder<>(builder);
}
/** If 'elem' is not null, add it to the builder. */

View File

@@ -184,15 +184,11 @@ public final class PosixTarHeader {
/** Returns the {@link Type} of file. */
public Type getType() {
switch (header[156]) {
case '\0':
case '0':
return Type.REGULAR;
case '5':
return Type.DIRECTORY;
default:
return Type.UNSUPPORTED;
}
return switch (header[156]) {
case '\0', '0' -> Type.REGULAR;
case '5' -> Type.DIRECTORY;
default -> Type.UNSUPPORTED;
};
}
/**
@@ -434,14 +430,9 @@ public final class PosixTarHeader {
*/
public Builder setType(Type type) {
switch (type) {
case REGULAR:
header[156] = '0';
break;
case DIRECTORY:
header[156] = '5';
break;
default:
throw new UnsupportedOperationException();
case REGULAR -> header[156] = '0';
case DIRECTORY -> header[156] = '5';
default -> throw new UnsupportedOperationException();
}
return this;
}

View File

@@ -73,8 +73,7 @@ public final class SafeSerializationUtils {
return null;
}
try (ObjectInputStream is = new SafeObjectInputStream(new ByteArrayInputStream(bytes))) {
Serializable ret = (Serializable) is.readObject();
return ret;
return (Serializable) is.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new IllegalArgumentException("Failed to deserialize: " + Arrays.toString(bytes), e);
}

View File

@@ -76,14 +76,16 @@ class DiffUtilsTest {
assertThat(prettyPrintSetDiff(ImmutableSet.of(a, b), ImmutableSet.of(a, c)))
.isEqualTo(
"\n"
+ " ADDED:\n"
+ " {c}\n"
+ " REMOVED:\n"
+ " {b}\n"
+ " FINAL CONTENTS:\n"
+ " {a},\n"
+ " {c}");
"""
ADDED:
{c}
REMOVED:
{b}
FINAL CONTENTS:
{a},
{c}\
""");
}
private static class DummyObject {

View File

@@ -36,8 +36,10 @@ class HexDumperTest {
void testOneLine() {
String input = "hello world";
String output =
"[11 bytes total]\n"
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \n";
"""
[11 bytes total]
00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \s
""";
assertThat(input).hasLength(11);
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
}
@@ -45,19 +47,22 @@ class HexDumperTest {
@Test
void testMultiLine() {
String input =
""
+ "\n"
+ "Maids heard the goblins cry:\n"
+ "\"Come buy our orchard fruits,\n"
+ "\"Come buy, come buy:\n";
"""
Maids heard the goblins cry:
"Come buy our orchard fruits,
"Come buy, come buy:
""";
String output =
"[81 bytes total]\n"
+ "00000000 0a 4d 61 69 64 73 20 68 65 61 72 64 20 74 68 65 .Maids heard the\n"
+ "00000016 20 67 6f 62 6c 69 6e 73 20 63 72 79 3a 0a 22 43 goblins cry:.\"C\n"
+ "00000032 6f 6d 65 20 62 75 79 20 6f 75 72 20 6f 72 63 68 ome buy our orch\n"
+ "00000048 61 72 64 20 66 72 75 69 74 73 2c 0a 22 43 6f 6d ard fruits,.\"Com\n"
+ "00000064 65 20 62 75 79 2c 20 63 6f 6d 65 20 62 75 79 3a e buy, come buy:\n"
+ "00000080 0a . \n";
"""
[81 bytes total]
00000000 0a 4d 61 69 64 73 20 68 65 61 72 64 20 74 68 65 .Maids heard the
00000016 20 67 6f 62 6c 69 6e 73 20 63 72 79 3a 0a 22 43 goblins cry:."C
00000032 6f 6d 65 20 62 75 79 20 6f 75 72 20 6f 72 63 68 ome buy our orch
00000048 61 72 64 20 66 72 75 69 74 73 2c 0a 22 43 6f 6d ard fruits,."Com
00000064 65 20 62 75 79 2c 20 63 6f 6d 65 20 62 75 79 3a e buy, come buy:
00000080 0a . \s
""";
assertThat(input).hasLength(81);
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
}
@@ -66,8 +71,10 @@ class HexDumperTest {
void testFullLine() {
String input = "hello worldddddd";
String output =
"[16 bytes total]\n"
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 64 64 64 64 64 hello worldddddd\n";
"""
[16 bytes total]
00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 64 64 64 64 64 hello worldddddd
""";
assertThat(input).hasLength(16);
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
}
@@ -76,8 +83,10 @@ class HexDumperTest {
void testUnicode() {
String input = "(◕‿◕)";
String output =
"[11 bytes total]\n"
+ "00000000 28 e2 97 95 e2 80 bf e2 97 95 29 (.........) \n";
"""
[11 bytes total]
00000000 28 e2 97 95 e2 80 bf e2 97 95 29 (.........) \s
""";
assertThat(input).hasLength(5);
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
}
@@ -89,23 +98,25 @@ class HexDumperTest {
input[n] = (byte) n;
}
String output =
"[256 bytes total]\n"
+ "00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n"
+ "00000016 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................\n"
+ "00000032 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
+ "00000048 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
+ "00000064 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
+ "00000080 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
+ "00000096 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
+ "00000112 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
+ "00000128 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................\n"
+ "00000144 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................\n"
+ "00000160 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af ................\n"
+ "00000176 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf ................\n"
+ "00000192 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf ................\n"
+ "00000208 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df ................\n"
+ "00000224 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef ................\n"
+ "00000240 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ................\n";
"""
[256 bytes total]
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
00000016 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................
00000032 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f !"#$%&'()*+,-./
00000048 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?
00000064 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
00000080 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_
00000096 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno
00000112 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.
00000128 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................
00000144 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................
00000160 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af ................
00000176 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf ................
00000192 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf ................
00000208 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df ................
00000224 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef ................
00000240 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ................
""";
assertThat(HexDumper.dumpHex(input)).isEqualTo(output);
}
@@ -159,12 +170,14 @@ class HexDumperTest {
void testPerLineIsOne() {
String input = "hello";
String output =
"[5 bytes total]\n"
+ "00000000 68 h\n"
+ "00000001 65 e\n"
+ "00000002 6c l\n"
+ "00000003 6c l\n"
+ "00000004 6f o\n";
"""
[5 bytes total]
00000000 68 h
00000001 65 e
00000002 6c l
00000003 6c l
00000004 6f o
""";
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8), 1, 0)).isEqualTo(output);
}

View File

@@ -111,10 +111,9 @@ public class SafeObjectInputStreamTest {
if (this == o) {
return true;
}
if (!(o instanceof NomulusEntity)) {
if (!(o instanceof NomulusEntity that)) {
return false;
}
NomulusEntity that = (NomulusEntity) o;
return Objects.equal(value, that.value);
}