Skip to content

Commit 21f9e5b

Browse files
authored
RATIS-2180. Use Objects.requireNonNull instead of Preconditions.assertNotNull (#1256)
1 parent 7f85d46 commit 21f9e5b

File tree

12 files changed

+24
-20
lines changed

12 files changed

+24
-20
lines changed

ratis-common/src/main/java/org/apache/ratis/protocol/RaftClientMessage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
package org.apache.ratis.protocol;
1919

2020
import org.apache.ratis.util.JavaUtils;
21-
import org.apache.ratis.util.Preconditions;
21+
22+
import java.util.Objects;
2223

2324
public abstract class RaftClientMessage implements RaftRpcMessage {
2425
private final ClientId clientId;
@@ -27,9 +28,9 @@ public abstract class RaftClientMessage implements RaftRpcMessage {
2728
private final long callId;
2829

2930
RaftClientMessage(ClientId clientId, RaftPeerId serverId, RaftGroupId groupId, long callId) {
30-
this.clientId = Preconditions.assertNotNull(clientId, "clientId");
31-
this.serverId = Preconditions.assertNotNull(serverId, "serverId");
32-
this.groupId = Preconditions.assertNotNull(groupId, "groupId");
31+
this.clientId = Objects.requireNonNull(clientId, "clientId == null");
32+
this.serverId = Objects.requireNonNull(serverId, "serverId == null");
33+
this.groupId = Objects.requireNonNull(groupId, "groupId == null");
3334
this.callId = callId;
3435
}
3536

ratis-common/src/main/java/org/apache/ratis/protocol/RaftId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ID randomId() {
8585
private final Supplier<String> uuidString;
8686

8787
RaftId(UUID uuid) {
88-
this.uuid = Preconditions.assertNotNull(uuid, "uuid");
88+
this.uuid = Objects.requireNonNull(uuid, "uuid == null");
8989
this.uuidBytes = JavaUtils.memoize(() -> toByteString(uuid));
9090
this.uuidString = JavaUtils.memoize(() -> createUuidString(uuid));
9191
Preconditions.assertTrue(ZERO_UUID == uuid || !uuid.equals(ZERO_UUID),

ratis-common/src/main/java/org/apache/ratis/retry/ExponentialBackoffRetry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
package org.apache.ratis.retry;
1919

20-
import org.apache.ratis.util.Preconditions;
2120
import org.apache.ratis.util.TimeDuration;
2221

22+
import java.util.Objects;
2323
import java.util.concurrent.ThreadLocalRandom;
2424

2525
/**
@@ -56,7 +56,7 @@ public Builder setMaxSleepTime(TimeDuration maxSleepTime) {
5656
}
5757

5858
public ExponentialBackoffRetry build() {
59-
Preconditions.assertNotNull(baseSleepTime, "baseSleepTime");
59+
Objects.requireNonNull(baseSleepTime, "baseSleepTime == null");
6060
return new ExponentialBackoffRetry(baseSleepTime, maxSleepTime,
6161
maxAttempts);
6262
}

ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static <T> T doPrivileged(Supplier<T> action, Supplier<String> name) {
148148
* otherwise, return system property value.
149149
*/
150150
static String getSystemProperty(final String key) {
151-
Preconditions.assertNotNull(key, "key");
151+
Objects.requireNonNull(key, "key == null");
152152
Preconditions.assertTrue(!key.isEmpty(), "key is empty.");
153153
return doPrivileged(() -> System.getProperty(key), () -> "get system property " + key);
154154
}
@@ -166,9 +166,9 @@ static String getEnv(String variable) {
166166
* When there is a {@link SecurityException}, this becomes a NOOP.
167167
*/
168168
static void setSystemProperty(String key, String value) {
169-
Preconditions.assertNotNull(key, "key");
169+
Objects.requireNonNull(key, "key == null");
170170
Preconditions.assertTrue(!key.isEmpty(), "key is empty.");
171-
Preconditions.assertNotNull(value, "value");
171+
Objects.requireNonNull(value, "value == null");
172172
doPrivileged(() -> System.setProperty(key, value), () -> "set system property " + key + " to " + value);
173173
}
174174

ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Objects;
2526
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.concurrent.atomic.AtomicReference;
2728
import java.util.function.Consumer;
@@ -202,7 +203,7 @@ public synchronized boolean release() {
202203
}
203204

204205
if (released) {
205-
Preconditions.assertNotNull(removeMethod, () -> "Not yet retained (removeMethod == null): " + valueClass);
206+
Objects.requireNonNull(removeMethod, () -> "Not yet retained (removeMethod == null): " + valueClass);
206207
removeMethod.run();
207208
}
208209
return released;

ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ synchronized int process(Event event) {
177177
public GrpcLogAppender(RaftServer.Division server, LeaderState leaderState, FollowerInfo f) {
178178
super(server, leaderState, f);
179179

180-
Preconditions.assertNotNull(getServerRpc(), "getServerRpc()");
180+
Objects.requireNonNull(getServerRpc(), "getServerRpc() == null");
181181

182182
final RaftProperties properties = server.getRaftServer().getProperties();
183183
this.maxPendingRequestsNum = GrpcConfigKeys.Server.leaderOutstandingAppendsMax(properties);

ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ CompletableFuture<RaftClientReply> takeSnapshotAsync(SnapshotManagementRequest r
12951295
LOG.info("{}: takeSnapshotAsync {}", getMemberId(), request);
12961296
assertLifeCycleState(LifeCycle.States.RUNNING);
12971297
assertGroup(getMemberId(), request);
1298-
Preconditions.assertNotNull(request.getCreate(), "create");
1298+
Objects.requireNonNull(request.getCreate(), "create == null");
12991299

13001300
final long creationGap = request.getCreate().getCreationGap();
13011301
long minGapValue = creationGap > 0? creationGap : RaftServerConfigKeys.Snapshot.creationGap(proxy.getProperties());
@@ -1914,6 +1914,7 @@ CompletableFuture<Message> applyLogToStateMachine(ReferenceCountedObject<LogEntr
19141914
break;
19151915
case STATEMACHINELOGENTRY:
19161916
TransactionContext trx = getTransactionContext(next, true);
1917+
Objects.requireNonNull(trx, "trx == null");
19171918
final ClientInvocationId invocationId = ClientInvocationId.valueOf(next.getStateMachineLogEntry());
19181919
writeIndexCache.add(invocationId.getClientId(), ((TransactionContextImpl) trx).getLogIndexFuture());
19191920
((TransactionContextImpl) trx).setDelegatedRef(nextRef);

ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.NavigableMap;
4848
import java.util.TreeMap;
49+
import java.util.Objects;
4950
import java.util.concurrent.TimeUnit;
5051

5152
/** Server utilities for internal use. */
@@ -170,8 +171,8 @@ public static RaftServerProxy newRaftServer(
170171
ThreadGroup threadGroup, RaftProperties properties, Parameters parameters) throws IOException {
171172
RaftServer.LOG.debug("newRaftServer: {}, {}", id, group);
172173
if (group != null && !group.getPeers().isEmpty()) {
173-
Preconditions.assertNotNull(id, "RaftPeerId %s is not in RaftGroup %s", id, group);
174-
Preconditions.assertNotNull(group.getPeer(id), "RaftPeerId %s is not in RaftGroup %s", id, group);
174+
Objects.requireNonNull(id, () -> "RaftPeerId " + id + " is not in RaftGroup " + group);
175+
Objects.requireNonNull(group.getPeer(id), () -> "RaftPeerId " + id + " is not in RaftGroup " + group);
175176
}
176177
final RaftServerProxy proxy = newRaftServer(id, stateMachineRegistry, threadGroup, properties, parameters);
177178
proxy.initGroups(group, option);

ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ TermIndex getLastTermIndex() {
628628
void appendEntry(LogSegment.Op op, ReferenceCountedObject<LogEntryProto> entry) {
629629
// SegmentedRaftLog does the segment creation/rolling work. Here we just
630630
// simply append the entry into the open segment.
631-
Preconditions.assertNotNull(openSegment, "openSegment");
631+
Objects.requireNonNull(openSegment, "openSegment == null");
632632
openSegment.appendToOpenSegment(op, entry);
633633
}
634634

ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static RaftPeerId waitAndKillLeader(MiniRaftCluster cluster) throws InterruptedE
152152

153153
static void waitFor(Supplier<Boolean> check, int checkEveryMillis,
154154
int waitForMillis) throws TimeoutException, InterruptedException {
155-
Preconditions.assertNotNull(check, "check");
155+
Objects.requireNonNull(check, "check == null");
156156
Preconditions.assertTrue(waitForMillis >= checkEveryMillis,
157157
() -> "waitFor: " + waitForMillis + " < checkEvery: " + checkEveryMillis);
158158

0 commit comments

Comments
 (0)