Skip to content

Commit 3cd16f0

Browse files
committed
- replaces manual null defensive programming by facilitating method
1 parent 61520f2 commit 3cd16f0

13 files changed

Lines changed: 41 additions & 59 deletions

File tree

‎src/main/java/com/microsoft/graph/authentication/TokenCredentialAuthProvider.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URL;
77
import java.util.Arrays;
88
import java.util.List;
9+
import java.util.Objects;
910
import java.util.concurrent.CompletableFuture;
1011

1112
import javax.annotation.Nonnull;
@@ -36,15 +37,12 @@ public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredentia
3637
* @param scopes Specified desired scopes of the Auth Provider
3738
*/
3839
public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull final TokenCredential tokenCredential) {
39-
if(tokenCredential == null) {
40-
throw new IllegalArgumentException("tokenCredential parameter cannot be null.");
41-
}
4240
if(scopes == null || scopes.isEmpty()) {
4341
throw new IllegalArgumentException("scopes parameter cannot be null or empty");
4442
}
4543
this.context = new TokenRequestContext();
4644
this.context.setScopes(scopes);
47-
this.tokenCredential = tokenCredential;
45+
this.tokenCredential = Objects.requireNonNull(tokenCredential, "tokenCredential parameter cannot be null.");
4846
}
4947

5048
/**
@@ -54,9 +52,7 @@ public TokenCredentialAuthProvider(@Nonnull final List<String> scopes, @Nonnull
5452
*/
5553
@Nonnull
5654
public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull final URL requestUrl) {
57-
if(requestUrl == null)
58-
throw new IllegalArgumentException("requestUrl parameter cannot be null");
59-
else if(shouldAuthenticateRequestWithUrl(requestUrl))
55+
if(shouldAuthenticateRequestWithUrl(Objects.requireNonNull(requestUrl, "requestUrl parameter cannot be null")))
6056
return this.tokenCredential
6157
.getToken(this.context)
6258
.toFuture()

‎src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.LinkedHashMap;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.Objects;
89
import java.util.concurrent.CompletableFuture;
910
import java.util.concurrent.ThreadLocalRandom;
1011
import java.util.regex.Matcher;
@@ -60,10 +61,7 @@ public MSBatchRequestContent(@Nonnull final ILogger logger, @Nonnull final MSBat
6061
if (batchRequestStepsArray.length > MAX_NUMBER_OF_REQUESTS)
6162
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
6263

63-
if(logger == null)
64-
throw new IllegalArgumentException("logger cannot be null");
65-
66-
this.logger = logger;
64+
this.logger = Objects.requireNonNull(logger, "logger cannot be null");
6765

6866
this.batchRequestStepsHashMap = new LinkedHashMap<>();
6967
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
@@ -78,8 +76,7 @@ public MSBatchRequestContent(@Nonnull final ILogger logger, @Nonnull final MSBat
7876
* given
7977
*/
8078
public boolean addBatchRequestStep(@Nonnull final MSBatchRequestStep batchRequestStep) {
81-
if(batchRequestStep == null)
82-
throw new IllegalArgumentException("batchRequestStep parameter cannot be null");
79+
Objects.requireNonNull(batchRequestStep, "batchRequestStep parameter cannot be null");
8380
if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()) ||
8481
batchRequestStepsHashMap.size() >= MAX_NUMBER_OF_REQUESTS)
8582
return false;
@@ -95,8 +92,7 @@ public boolean addBatchRequestStep(@Nonnull final MSBatchRequestStep batchReques
9592
*/
9693
@Nonnull
9794
public String addBatchRequestStep(@Nonnull final Request request, @Nullable final String... arrayOfDependsOnIds) {
98-
if(request == null)
99-
throw new IllegalArgumentException("request parameter cannot be null");
95+
Objects.requireNonNull(request, "request parameter cannot be null");
10096
String requestId;
10197
do {
10298
requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE));
@@ -168,9 +164,7 @@ public MSBatchResponseContent execute(@Nonnull final IBaseClient client) {
168164
*/
169165
@Nonnull
170166
public CompletableFuture<MSBatchResponseContent> executeAsync(@Nonnull final IBaseClient client) {
171-
if(client == null) {
172-
throw new IllegalArgumentException("client parameter cannot be null");
173-
}
167+
Objects.requireNonNull(client, "client parameter cannot be null");
174168
final JsonObject content = getBatchRequestContentAsJson();
175169
return client.customRequest("/$batch")
176170
.buildRequest()

‎src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.HashSet;
5+
import java.util.Objects;
56

67
import javax.annotation.Nullable;
78
import javax.annotation.Nonnull;
@@ -23,15 +24,12 @@ public class MSBatchRequestStep {
2324
* @param arrayOfDependsOnIds the ids of steps this step depends on
2425
*/
2526
public MSBatchRequestStep(@Nonnull final String requestId, @Nonnull final Request request, @Nullable final String... arrayOfDependsOnIds) {
26-
if(requestId == null)
27-
throw new IllegalArgumentException("Request Id cannot be null.");
28-
if(requestId.length() == 0)
27+
Objects.requireNonNull(requestId, "Request Id cannot be null.");
28+
if(requestId.isEmpty())
2929
throw new IllegalArgumentException("Request Id cannot be empty.");
30-
if(request == null)
31-
throw new IllegalArgumentException("Request cannot be null.");
3230

3331
this.requestId = requestId;
34-
this.request = request;
32+
this.request = Objects.requireNonNull(request, "Request cannot be null.");
3533
this.dependsOnIds = new HashSet<>(Arrays.asList(arrayOfDependsOnIds));
3634
}
3735

‎src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.LinkedHashMap;
55
import java.util.Iterator;
66
import java.util.Map;
7+
import java.util.Objects;
78

89
import javax.annotation.Nullable;
910
import javax.annotation.Nonnull;
@@ -48,9 +49,7 @@ public MSBatchResponseContent(@Nullable final Response batchResponse) {
4849
* @param logger logger to use for telemetry
4950
*/
5051
public MSBatchResponseContent(@Nonnull final ILogger logger, @Nullable final Response batchResponse) {
51-
if(logger == null)
52-
throw new IllegalArgumentException("logger parameter cannot be null");
53-
this.logger = logger;
52+
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");
5453
update(batchResponse);
5554
this.message = batchResponse.message();
5655
this.protocol = batchResponse.protocol();
@@ -173,10 +172,7 @@ public Iterator<Map.Entry<String, Response>> getResponsesIterator() {
173172
* @param batchResponse the response from the service.
174173
*/
175174
public void update(@Nonnull final Response batchResponse) {
176-
if (batchResponse == null)
177-
throw new IllegalArgumentException("Batch Response cannot be null");
178-
179-
final Map<String, Request> requestMap = createBatchRequestsHashMap(batchResponse);
175+
final Map<String, Request> requestMap = createBatchRequestsHashMap(Objects.requireNonNull(batchResponse, "Batch Response cannot be null"));
180176
if (requestMap != null)
181177
batchRequestsHashMap.putAll(requestMap);
182178

‎src/main/java/com/microsoft/graph/core/BaseClient.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import com.microsoft.graph.serializer.ISerializer;
3434

3535
import javax.annotation.Nullable;
36+
37+
import java.util.Objects;
38+
3639
import javax.annotation.Nonnull;
3740

3841
import okhttp3.OkHttpClient;
@@ -236,9 +239,7 @@ public Builder<httpClientType> authenticationProvider(@Nonnull final IAuthentica
236239
*/
237240
@Nonnull
238241
protected <ClientType extends BaseClient> ClientType buildClient(@Nonnull ClientType instance) throws ClientException {
239-
if(instance == null) {
240-
throw new IllegalArgumentException("The instance cannot be null");
241-
}
242+
Objects.requireNonNull(instance, "The instance cannot be null");
242243
instance.setHttpProvider(this.getHttpProvider());
243244
instance.setLogger(this.getLogger());
244245
instance.setSerializer(this.getSerializer());

‎src/main/java/com/microsoft/graph/httpcore/RetryHandler.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.microsoft.graph.httpcore;
22

33
import java.io.IOException;
4+
import java.util.Objects;
45

56
import javax.annotation.Nullable;
67
import javax.annotation.Nonnull;
@@ -84,9 +85,7 @@ public RetryHandler(@Nullable final RetryOptions retryOption) {
8485
* @param logger logger to use for telemetry
8586
*/
8687
public RetryHandler(@Nonnull final ILogger logger, @Nullable final RetryOptions retryOption) {
87-
if(logger == null)
88-
throw new IllegalArgumentException("logger parameter cannot be null");
89-
this.logger = logger;
88+
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");
9089
this.mRetryOption = retryOption;
9190
if(this.mRetryOption == null) {
9291
this.mRetryOption = new RetryOptions();

‎src/main/java/com/microsoft/graph/options/Option.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package com.microsoft.graph.options;
2424

25+
import java.util.Objects;
26+
2527
import javax.annotation.Nonnull;
2628

2729
/**
@@ -46,8 +48,9 @@ public class Option {
4648
* @param value the value of the option
4749
*/
4850
protected Option(@Nonnull final String name, @Nonnull final Object value) {
49-
if(name == null || name == "") {
50-
throw new IllegalArgumentException("name should not be null or empty");
51+
Objects.requireNonNull(name, "name should not be null");
52+
if(name.isEmpty()) {
53+
throw new IllegalArgumentException("name should not empty");
5154
}
5255
this.name = name;
5356
this.value = value;

‎src/test/java/com/microsoft/graph/authentication/TokenCredentialAuthProviderTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public void providerDoesntAddTokenOnInvalidHostName() throws MalformedURLExcepti
4747
}
4848
@Test
4949
public void providerDefensiveProgramming() {
50-
assertThrows("should throw on null credentials", IllegalArgumentException.class, () -> { new TokenCredentialAuthProvider(null); });
50+
assertThrows("should throw on null credentials", NullPointerException.class, () -> { new TokenCredentialAuthProvider(null); });
5151
assertThrows("should throw on null scopes", IllegalArgumentException.class, () -> { new TokenCredentialAuthProvider(null, mock(TokenCredential.class)); });
5252
assertThrows("should throw on empty scopes", IllegalArgumentException.class, () -> { new TokenCredentialAuthProvider(new ArrayList<String>(), mock(TokenCredential.class)); });
5353
final TokenCredentialAuthProvider provider = new TokenCredentialAuthProvider(mock(TokenCredential.class));
54-
assertThrows("should throw on null url", IllegalArgumentException.class, () -> { provider.getAuthorizationTokenAsync(null); });
54+
assertThrows("should throw on null url", NullPointerException.class, () -> { provider.getAuthorizationTokenAsync(null); });
5555
}
5656
}

‎src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ public String answer(InvocationOnMock invocation) throws Throwable {
114114
new MSBatchRequestContent(mock(ILogger.class), null, null, null, null, null, null, null, null, null, null, null, null, null,
115115
null, null, null, null, null, null, null, null, null);
116116
});
117-
assertThrows("the logger cannot be null", IllegalArgumentException.class, () -> {
117+
assertThrows("the logger cannot be null", NullPointerException.class, () -> {
118118
new MSBatchRequestContent((ILogger) null, (MSBatchRequestStep)null);
119119
});
120120

121121
new MSBatchRequestContent(mockStep, null); // addind a null step doesn't throw
122-
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
122+
assertThrows("should throw argument exception", NullPointerException.class, () -> {
123123
new MSBatchRequestContent().addBatchRequestStep(null);
124124
});
125-
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
125+
assertThrows("should throw argument exception", NullPointerException.class, () -> {
126126
new MSBatchRequestContent().addBatchRequestStep((Request) null);
127127
});
128128

‎src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public void testMSBatchRequestStepCreation() {
2222

2323
@Test
2424
public void defensiveProgrammingTests() {
25-
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
25+
assertThrows("should throw argument exception", NullPointerException.class, () -> {
2626
new MSBatchRequestStep(null, null);
2727
});
28-
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
28+
assertThrows("should throw argument exception", NullPointerException.class, () -> {
2929
new MSBatchRequestStep("id", null);
3030
});
3131
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {

0 commit comments

Comments
 (0)