Skip to content

Commit

Permalink
rafactor(java): improve oneOf types DX (#1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam authored Sep 11, 2023
1 parent 2d91773 commit d18321c
Show file tree
Hide file tree
Showing 31 changed files with 390 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

public final class AlgoliaAgent {

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

public AlgoliaAgent addSegment(@NotNull Segment seg) {
public AlgoliaAgent addSegment(@Nonnull Segment seg) {
String segment = seg.toString();
if (!segments.contains(segment)) {
segments.add(segment);
Expand All @@ -26,14 +26,14 @@ public AlgoliaAgent addSegment(@NotNull Segment seg) {
return this;
}

public AlgoliaAgent addSegments(@NotNull List<Segment> segments) {
public AlgoliaAgent addSegments(@Nonnull List<Segment> segments) {
for (Segment segment : segments) {
addSegment(segment);
}
return this;
}

public AlgoliaAgent removeSegment(@NotNull Segment seg) {
public AlgoliaAgent removeSegment(@Nonnull Segment seg) {
segments.remove(seg.toString());
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import java.time.Duration;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

public interface ClientConfig {
public @NotNull LogLevel getLogLevel();
public @Nonnull LogLevel getLogLevel();

public Logger getLogger();

public @NotNull Duration getConnectTimeout();
public @Nonnull Duration getConnectTimeout();

public @NotNull Duration getWriteTimeout();
public @Nonnull Duration getWriteTimeout();

public @NotNull Duration getReadTimeout();
public @Nonnull Duration getReadTimeout();

public @NotNull Map<String, String> getDefaultHeaders();
public @Nonnull Map<String, String> getDefaultHeaders();

public @NotNull CompressionType getCompressionType();
public @Nonnull CompressionType getCompressionType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

public final class ClientOptions implements ClientConfig {

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

@NotNull
@Nonnull
public List<AlgoliaAgent.Segment> getAlgoliaAgentSegments() {
return algoliaAgentSegments;
}
Expand All @@ -62,32 +62,32 @@ public List<Host> getHosts() {
return hosts;
}

@NotNull
@Nonnull
public LogLevel getLogLevel() {
return logLevel;
}

@NotNull
@Nonnull
public Duration getConnectTimeout() {
return connectTimeout;
}

@NotNull
@Nonnull
public Duration getWriteTimeout() {
return writeTimeout;
}

@NotNull
@Nonnull
public Duration getReadTimeout() {
return readTimeout;
}

@NotNull
@Nonnull
public Map<String, String> getDefaultHeaders() {
return defaultHeaders;
}

@NotNull
@Nonnull
public CompressionType getCompressionType() {
return compressionType;
}
Expand All @@ -96,7 +96,7 @@ public Requester getCustomRequester() {
return customRequester;
}

@NotNull
@Nonnull
public Logger getLogger() {
return logger;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.algolia.config;

import java.util.Set;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

public final class Host {

Expand All @@ -11,7 +11,7 @@ public final class Host {

private final String scheme;

public Host(@NotNull String url, @NotNull Set<CallType> callType) {
public Host(@Nonnull String url, @Nonnull Set<CallType> callType) {
this(url, callType, "https");
}

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

@NotNull
@Nonnull
public String getUrl() {
return url;
}

@NotNull
@Nonnull
public Set<CallType> getCallTypes() {
return callTypes;
}

@NotNull
@Nonnull
public String getScheme() {
return scheme;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import okhttp3.*;
import okhttp3.internal.http.HttpMethod;
import okio.BufferedSink;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

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

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

/** Serializes the request body into JSON format. */
@NotNull
@Nonnull
private RequestBody buildRequestBody(Object requestBody) {
return new RequestBody() {
@Nullable
@Override
public MediaType contentType() {
return JSON_MEDIA_TYPE;
}

@Override
public void writeTo(@NotNull BufferedSink bufferedSink) {
public void writeTo(@Nonnull BufferedSink bufferedSink) {
serializer.serialize(bufferedSink.outputStream(), requestBody);
}
};
}

/** Constructs the headers for the HTTP request. */
@NotNull
private Headers createHeaders(@NotNull HttpRequest request, RequestOptions requestOptions) {
@Nonnull
private Headers createHeaders(@Nonnull HttpRequest request, RequestOptions requestOptions) {
Headers.Builder builder = new Headers.Builder();
request.getHeaders().forEach(builder::add);
if (requestOptions != null) {
Expand All @@ -154,7 +152,7 @@ private Headers createHeaders(@NotNull HttpRequest request, RequestOptions reque
}

/** Returns a suitable OkHttpClient instance based on the provided request options. */
@NotNull
@Nonnull
private OkHttpClient getOkHttpClient(RequestOptions requestOptions) {
// Return the default client if no request options are provided.
if (requestOptions == null) return httpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;

/**
* Utility class for JSON serialization and deserialization using Jackson. It provides functionality
Expand All @@ -29,7 +29,7 @@ public static Builder builder() {
*
* @param mapper The Jackson ObjectMapper to be used for JSON operations.
*/
JsonSerializer(@NotNull ObjectMapper mapper) {
JsonSerializer(@Nonnull ObjectMapper mapper) {
this.mapper = mapper;
}

Expand All @@ -39,7 +39,7 @@ public static Builder builder() {
* @param stream output steam.
* @param object The Java object to serialize.
*/
public void serialize(OutputStream stream, @NotNull Object object) {
public void serialize(OutputStream stream, @Nonnull Object object) {
try {
mapper.writeValue(stream, object);
} catch (IOException e) {
Expand All @@ -63,7 +63,7 @@ public <T> T deserialize(InputStream stream, JavaType returnType) {
* @param innerType The parameterized type.
* @return A JavaType representation of the parameterized class.
*/
public JavaType getJavaType(@NotNull Class<?> returnType, @NotNull Class<?> innerType) {
public JavaType getJavaType(@Nonnull Class<?> returnType, @Nonnull Class<?> innerType) {
return mapper.getTypeFactory().constructParametricType(returnType, innerType);
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.algolia.internal.interceptors;

import java.io.IOException;
import javax.annotation.Nonnull;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

public final class AuthInterceptor implements Interceptor {

Expand All @@ -19,7 +19,7 @@ public AuthInterceptor(String applicationId, String apiKey) {
this.apiKey = apiKey;
}

@NotNull
@Nonnull
@Override
public Response intercept(Chain chain) throws IOException {
okhttp3.Request originalRequest = chain.request();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.algolia.internal.interceptors;

import java.io.IOException;
import javax.annotation.Nonnull;
import okhttp3.*;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import org.jetbrains.annotations.NotNull;

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

@NotNull
@Nonnull
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request originalRequest = chain.request();
Expand Down Expand Up @@ -39,7 +39,7 @@ public long contentLength() {
}

@Override
public void writeTo(@NotNull BufferedSink sink) throws IOException {
public void writeTo(@Nonnull BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nonnull;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

public final class HeaderInterceptor implements Interceptor {

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

@NotNull
@Nonnull
@Override
public Response intercept(Chain chain) throws IOException {
okhttp3.Request request = chain.request();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.algolia.config.LogLevel;
import com.algolia.config.Logger;
import java.io.IOException;
import javax.annotation.Nonnull;
import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import org.jetbrains.annotations.NotNull;

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

public HttpLoggingInterceptor.Logger toLogger(@NotNull Logger logger) {
public HttpLoggingInterceptor.Logger toLogger(@Nonnull Logger logger) {
return logger::log;
}

public HttpLoggingInterceptor.Level toLevel(@NotNull LogLevel logLevel) {
public HttpLoggingInterceptor.Level toLevel(@Nonnull LogLevel logLevel) {
switch (logLevel) {
case NONE:
return HttpLoggingInterceptor.Level.NONE;
Expand All @@ -47,9 +47,9 @@ public HttpLoggingInterceptor.Level toLevel(@NotNull LogLevel logLevel) {
}
}

@NotNull
@Nonnull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
public Response intercept(@Nonnull Chain chain) throws IOException {
return logger.intercept(chain);
}
}
Loading

0 comments on commit d18321c

Please sign in to comment.