Skip to content

Commit d18321c

Browse files
authored
rafactor(java): improve oneOf types DX (#1990)
1 parent 2d91773 commit d18321c

31 files changed

+390
-417
lines changed

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/AlgoliaAgent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedHashSet;
44
import java.util.List;
55
import java.util.Set;
6-
import org.jetbrains.annotations.NotNull;
6+
import javax.annotation.Nonnull;
77

88
public final class AlgoliaAgent {
99

@@ -17,7 +17,7 @@ public AlgoliaAgent(String clientVersion) {
1717
this.addSegment(new Segment("JVM", System.getProperty("java.version")));
1818
}
1919

20-
public AlgoliaAgent addSegment(@NotNull Segment seg) {
20+
public AlgoliaAgent addSegment(@Nonnull Segment seg) {
2121
String segment = seg.toString();
2222
if (!segments.contains(segment)) {
2323
segments.add(segment);
@@ -26,14 +26,14 @@ public AlgoliaAgent addSegment(@NotNull Segment seg) {
2626
return this;
2727
}
2828

29-
public AlgoliaAgent addSegments(@NotNull List<Segment> segments) {
29+
public AlgoliaAgent addSegments(@Nonnull List<Segment> segments) {
3030
for (Segment segment : segments) {
3131
addSegment(segment);
3232
}
3333
return this;
3434
}
3535

36-
public AlgoliaAgent removeSegment(@NotNull Segment seg) {
36+
public AlgoliaAgent removeSegment(@Nonnull Segment seg) {
3737
segments.remove(seg.toString());
3838
return this;
3939
}

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/ClientConfig.java

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

33
import java.time.Duration;
44
import java.util.Map;
5-
import org.jetbrains.annotations.NotNull;
5+
import javax.annotation.Nonnull;
66

77
public interface ClientConfig {
8-
public @NotNull LogLevel getLogLevel();
8+
public @Nonnull LogLevel getLogLevel();
99

1010
public Logger getLogger();
1111

12-
public @NotNull Duration getConnectTimeout();
12+
public @Nonnull Duration getConnectTimeout();
1313

14-
public @NotNull Duration getWriteTimeout();
14+
public @Nonnull Duration getWriteTimeout();
1515

16-
public @NotNull Duration getReadTimeout();
16+
public @Nonnull Duration getReadTimeout();
1717

18-
public @NotNull Map<String, String> getDefaultHeaders();
18+
public @Nonnull Map<String, String> getDefaultHeaders();
1919

20-
public @NotNull CompressionType getCompressionType();
20+
public @Nonnull CompressionType getCompressionType();
2121
}

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/ClientOptions.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.ExecutorService;
1212
import java.util.function.Consumer;
13-
import org.jetbrains.annotations.NotNull;
13+
import javax.annotation.Nonnull;
1414

1515
public final class ClientOptions implements ClientConfig {
1616

@@ -53,7 +53,7 @@ public ClientOptions() {
5353
this.executor = builder.executor != null ? builder.executor : ExecutorUtils.newThreadPool();
5454
}
5555

56-
@NotNull
56+
@Nonnull
5757
public List<AlgoliaAgent.Segment> getAlgoliaAgentSegments() {
5858
return algoliaAgentSegments;
5959
}
@@ -62,32 +62,32 @@ public List<Host> getHosts() {
6262
return hosts;
6363
}
6464

65-
@NotNull
65+
@Nonnull
6666
public LogLevel getLogLevel() {
6767
return logLevel;
6868
}
6969

70-
@NotNull
70+
@Nonnull
7171
public Duration getConnectTimeout() {
7272
return connectTimeout;
7373
}
7474

75-
@NotNull
75+
@Nonnull
7676
public Duration getWriteTimeout() {
7777
return writeTimeout;
7878
}
7979

80-
@NotNull
80+
@Nonnull
8181
public Duration getReadTimeout() {
8282
return readTimeout;
8383
}
8484

85-
@NotNull
85+
@Nonnull
8686
public Map<String, String> getDefaultHeaders() {
8787
return defaultHeaders;
8888
}
8989

90-
@NotNull
90+
@Nonnull
9191
public CompressionType getCompressionType() {
9292
return compressionType;
9393
}
@@ -96,7 +96,7 @@ public Requester getCustomRequester() {
9696
return customRequester;
9797
}
9898

99-
@NotNull
99+
@Nonnull
100100
public Logger getLogger() {
101101
return logger;
102102
}

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/Host.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.algolia.config;
22

33
import java.util.Set;
4-
import org.jetbrains.annotations.NotNull;
4+
import javax.annotation.Nonnull;
55

66
public final class Host {
77

@@ -11,7 +11,7 @@ public final class Host {
1111

1212
private final String scheme;
1313

14-
public Host(@NotNull String url, @NotNull Set<CallType> callType) {
14+
public Host(@Nonnull String url, @Nonnull Set<CallType> callType) {
1515
this(url, callType, "https");
1616
}
1717

@@ -21,17 +21,17 @@ public Host(String url, Set<CallType> callType, String scheme) {
2121
this.scheme = scheme;
2222
}
2323

24-
@NotNull
24+
@Nonnull
2525
public String getUrl() {
2626
return url;
2727
}
2828

29-
@NotNull
29+
@Nonnull
3030
public Set<CallType> getCallTypes() {
3131
return callTypes;
3232
}
3333

34-
@NotNull
34+
@Nonnull
3535
public String getScheme() {
3636
return scheme;
3737
}

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/HttpRequester.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
import java.util.List;
1515
import java.util.concurrent.atomic.AtomicBoolean;
1616
import java.util.function.Consumer;
17+
import javax.annotation.Nonnull;
1718
import okhttp3.*;
1819
import okhttp3.internal.http.HttpMethod;
1920
import okio.BufferedSink;
20-
import org.jetbrains.annotations.NotNull;
21-
import org.jetbrains.annotations.Nullable;
2221

2322
/**
2423
* HttpRequester is responsible for making HTTP requests using the OkHttp client. It provides a
@@ -62,7 +61,7 @@ public <T> T execute(HttpRequest httpRequest, RequestOptions requestOptions, Typ
6261
}
6362

6463
/** Core method to execute an HTTP request and handle the response. */
65-
private <T> T execute(@NotNull HttpRequest httpRequest, RequestOptions requestOptions, JavaType returnType) {
64+
private <T> T execute(@Nonnull HttpRequest httpRequest, RequestOptions requestOptions, JavaType returnType) {
6665
if (isClosed.get()) {
6766
throw new IllegalStateException("HttpRequester is closed");
6867
}
@@ -99,8 +98,8 @@ private <T> T execute(@NotNull HttpRequest httpRequest, RequestOptions requestOp
9998
}
10099

101100
/** Constructs the URL for the HTTP request. */
102-
@NotNull
103-
private static HttpUrl createHttpUrl(@NotNull HttpRequest request, RequestOptions requestOptions) {
101+
@Nonnull
102+
private static HttpUrl createHttpUrl(@Nonnull HttpRequest request, RequestOptions requestOptions) {
104103
HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
105104
.scheme("https")
106105
.host("algolia.com") // will be overridden by the retry strategy
@@ -126,25 +125,24 @@ private RequestBody createRequestBody(HttpRequest httpRequest) {
126125
}
127126

128127
/** Serializes the request body into JSON format. */
129-
@NotNull
128+
@Nonnull
130129
private RequestBody buildRequestBody(Object requestBody) {
131130
return new RequestBody() {
132-
@Nullable
133131
@Override
134132
public MediaType contentType() {
135133
return JSON_MEDIA_TYPE;
136134
}
137135

138136
@Override
139-
public void writeTo(@NotNull BufferedSink bufferedSink) {
137+
public void writeTo(@Nonnull BufferedSink bufferedSink) {
140138
serializer.serialize(bufferedSink.outputStream(), requestBody);
141139
}
142140
};
143141
}
144142

145143
/** Constructs the headers for the HTTP request. */
146-
@NotNull
147-
private Headers createHeaders(@NotNull HttpRequest request, RequestOptions requestOptions) {
144+
@Nonnull
145+
private Headers createHeaders(@Nonnull HttpRequest request, RequestOptions requestOptions) {
148146
Headers.Builder builder = new Headers.Builder();
149147
request.getHeaders().forEach(builder::add);
150148
if (requestOptions != null) {
@@ -154,7 +152,7 @@ private Headers createHeaders(@NotNull HttpRequest request, RequestOptions reque
154152
}
155153

156154
/** Returns a suitable OkHttpClient instance based on the provided request options. */
157-
@NotNull
155+
@Nonnull
158156
private OkHttpClient getOkHttpClient(RequestOptions requestOptions) {
159157
// Return the default client if no request options are provided.
160158
if (requestOptions == null) return httpClient;

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/JsonSerializer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.InputStream;
1111
import java.io.OutputStream;
1212
import java.util.function.Consumer;
13-
import org.jetbrains.annotations.NotNull;
13+
import javax.annotation.Nonnull;
1414

1515
/**
1616
* Utility class for JSON serialization and deserialization using Jackson. It provides functionality
@@ -29,7 +29,7 @@ public static Builder builder() {
2929
*
3030
* @param mapper The Jackson ObjectMapper to be used for JSON operations.
3131
*/
32-
JsonSerializer(@NotNull ObjectMapper mapper) {
32+
JsonSerializer(@Nonnull ObjectMapper mapper) {
3333
this.mapper = mapper;
3434
}
3535

@@ -39,7 +39,7 @@ public static Builder builder() {
3939
* @param stream output steam.
4040
* @param object The Java object to serialize.
4141
*/
42-
public void serialize(OutputStream stream, @NotNull Object object) {
42+
public void serialize(OutputStream stream, @Nonnull Object object) {
4343
try {
4444
mapper.writeValue(stream, object);
4545
} catch (IOException e) {
@@ -63,7 +63,7 @@ public <T> T deserialize(InputStream stream, JavaType returnType) {
6363
* @param innerType The parameterized type.
6464
* @return A JavaType representation of the parameterized class.
6565
*/
66-
public JavaType getJavaType(@NotNull Class<?> returnType, @NotNull Class<?> innerType) {
66+
public JavaType getJavaType(@Nonnull Class<?> returnType, @Nonnull Class<?> innerType) {
6767
return mapper.getTypeFactory().constructParametricType(returnType, innerType);
6868
}
6969

@@ -73,7 +73,7 @@ public JavaType getJavaType(@NotNull Class<?> returnType, @NotNull Class<?> inne
7373
* @param returnType The main class type.
7474
* @return A JavaType representation of the parameterized class.
7575
*/
76-
public JavaType getJavaType(@NotNull TypeReference<?> returnType) {
76+
public JavaType getJavaType(@Nonnull TypeReference<?> returnType) {
7777
return mapper.getTypeFactory().constructType(returnType);
7878
}
7979

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.algolia.internal.interceptors;
22

33
import java.io.IOException;
4+
import javax.annotation.Nonnull;
45
import okhttp3.Headers;
56
import okhttp3.Interceptor;
67
import okhttp3.Response;
7-
import org.jetbrains.annotations.NotNull;
88

99
public final class AuthInterceptor implements Interceptor {
1010

@@ -19,7 +19,7 @@ public AuthInterceptor(String applicationId, String apiKey) {
1919
this.apiKey = apiKey;
2020
}
2121

22-
@NotNull
22+
@Nonnull
2323
@Override
2424
public Response intercept(Chain chain) throws IOException {
2525
okhttp3.Request originalRequest = chain.request();

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/interceptors/GzipRequestInterceptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.algolia.internal.interceptors;
22

33
import java.io.IOException;
4+
import javax.annotation.Nonnull;
45
import okhttp3.*;
56
import okio.BufferedSink;
67
import okio.GzipSink;
78
import okio.Okio;
8-
import org.jetbrains.annotations.NotNull;
99

1010
/** This interceptor compresses the HTTP request body. */
1111
public final class GzipRequestInterceptor implements Interceptor {
1212

13-
@NotNull
13+
@Nonnull
1414
@Override
1515
public Response intercept(Interceptor.Chain chain) throws IOException {
1616
Request originalRequest = chain.request();
@@ -39,7 +39,7 @@ public long contentLength() {
3939
}
4040

4141
@Override
42-
public void writeTo(@NotNull BufferedSink sink) throws IOException {
42+
public void writeTo(@Nonnull BufferedSink sink) throws IOException {
4343
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
4444
body.writeTo(gzipSink);
4545
gzipSink.close();

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/interceptors/HeaderInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import java.io.IOException;
44
import java.util.Collections;
55
import java.util.Map;
6+
import javax.annotation.Nonnull;
67
import okhttp3.Headers;
78
import okhttp3.Interceptor;
89
import okhttp3.Response;
9-
import org.jetbrains.annotations.NotNull;
1010

1111
public final class HeaderInterceptor implements Interceptor {
1212

@@ -16,7 +16,7 @@ public HeaderInterceptor(Map<String, String> headers) {
1616
this.headers = Collections.unmodifiableMap(headers);
1717
}
1818

19-
@NotNull
19+
@Nonnull
2020
@Override
2121
public Response intercept(Chain chain) throws IOException {
2222
okhttp3.Request request = chain.request();

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/interceptors/LogInterceptor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import com.algolia.config.LogLevel;
44
import com.algolia.config.Logger;
55
import java.io.IOException;
6+
import javax.annotation.Nonnull;
67
import okhttp3.Interceptor;
78
import okhttp3.Response;
89
import okhttp3.logging.HttpLoggingInterceptor;
9-
import org.jetbrains.annotations.NotNull;
1010

1111
/**
1212
* An interceptor that facilitates HTTP logging based on the provided logging level. This class
@@ -28,11 +28,11 @@ public LogInterceptor(Logger logger, LogLevel logLevel) {
2828
this.logger = new HttpLoggingInterceptor(logr).setLevel(level);
2929
}
3030

31-
public HttpLoggingInterceptor.Logger toLogger(@NotNull Logger logger) {
31+
public HttpLoggingInterceptor.Logger toLogger(@Nonnull Logger logger) {
3232
return logger::log;
3333
}
3434

35-
public HttpLoggingInterceptor.Level toLevel(@NotNull LogLevel logLevel) {
35+
public HttpLoggingInterceptor.Level toLevel(@Nonnull LogLevel logLevel) {
3636
switch (logLevel) {
3737
case NONE:
3838
return HttpLoggingInterceptor.Level.NONE;
@@ -47,9 +47,9 @@ public HttpLoggingInterceptor.Level toLevel(@NotNull LogLevel logLevel) {
4747
}
4848
}
4949

50-
@NotNull
50+
@Nonnull
5151
@Override
52-
public Response intercept(@NotNull Chain chain) throws IOException {
52+
public Response intercept(@Nonnull Chain chain) throws IOException {
5353
return logger.intercept(chain);
5454
}
5555
}

0 commit comments

Comments
 (0)