Skip to content

Commit

Permalink
fix!: Qodana suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Feb 8, 2024
1 parent 9530643 commit 027dcc3
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/brenoepics/at4j/AzureApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ CompletableFuture<Optional<Collection<Language>>> getAvailableLanguages(
*
* @return RateLimitManager - The ratelimit manager.
*/
RateLimitManager getRatelimitManager();
<T, T3, T4> RateLimitManager<T, T3, T4> getRatelimitManager();

/**
* Gets the HttpClient.
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* This class is an implementation of the AzureApi interface. It provides methods to interact with
* Azure's translation API.
*/
public class AzureApiImpl implements AzureApi {
public class AzureApiImpl<T> implements AzureApi {

/** The Http Client for this instance. */
private final HttpClient httpClient;
Expand All @@ -42,7 +42,7 @@ public class AzureApiImpl implements AzureApi {
private final ObjectMapper objectMapper = new ObjectMapper();

/** The ratelimit manager for this resource. */
private final RateLimitManager ratelimitManager = new RateLimitManager(this);
private final RateLimitManager<T, ?, ?> ratelimitManager = new RateLimitManager<>(this);

/** The thread pool which is used internally. */
private final ThreadPoolImpl threadPool = new ThreadPoolImpl();
Expand Down Expand Up @@ -89,8 +89,7 @@ public CompletableFuture<Optional<TranslationResponse>> translate(TranslateParam
return CompletableFuture.completedFuture(Optional.empty());
}

RestRequest<Optional<TranslationResponse>> request =
new RestRequest<>(this, RestMethod.POST, RestEndpoint.TRANSLATE);
RestRequest request = new RestRequest(this, RestMethod.POST, RestEndpoint.TRANSLATE);
request.setBody(params.getBody());
request.addQueryParameters(params.getQueryParameters());

Expand All @@ -107,8 +106,7 @@ public CompletableFuture<Optional<DetectResponse>> detectLanguage(DetectLanguage
return CompletableFuture.completedFuture(Optional.empty());
}

RestRequest<Optional<DetectResponse>> request =
new RestRequest<>(this, RestMethod.POST, RestEndpoint.DETECT);
RestRequest request = new RestRequest(this, RestMethod.POST, RestEndpoint.DETECT);
request.setBody(params.getBody());

return request.execute(params::handleResponse);
Expand All @@ -117,8 +115,7 @@ public CompletableFuture<Optional<DetectResponse>> detectLanguage(DetectLanguage
@Override
public CompletableFuture<Optional<Collection<Language>>> getAvailableLanguages(
AvailableLanguagesParams params) {
RestRequest<Optional<Collection<Language>>> request =
new RestRequest<>(this, RestMethod.GET, RestEndpoint.LANGUAGES, false);
RestRequest request = new RestRequest(this, RestMethod.GET, RestEndpoint.LANGUAGES, false);
request.addQueryParameter("scope", params.getScope());

if (params.getSourceLanguage() != null) {
Expand Down Expand Up @@ -158,8 +155,9 @@ public ObjectMapper getObjectMapper() {
*
* @return RateLimitManager - The used RateLimitManager.
*/
@SuppressWarnings("unchecked")
@Override
public RateLimitManager getRatelimitManager() {
public RateLimitManager<T, ?, ?> getRatelimitManager() {
return ratelimitManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class RateLimitBucket<T, T4, T3> {

private final ConcurrentLinkedQueue<RestRequest<T>> requestQueue = new ConcurrentLinkedQueue<>();
private final ConcurrentLinkedQueue<RestRequest> requestQueue = new ConcurrentLinkedQueue<>();

private final RestEndpoint endpoint;

Expand All @@ -31,7 +31,7 @@ public RateLimitBucket(RestEndpoint endpoint) {
*
* @param request The request to add.
*/
public void addRequestToQueue(RestRequest<T> request) {
public void addRequestToQueue(RestRequest request) {
requestQueue.add(request);
}

Expand All @@ -45,7 +45,7 @@ public void pollRequestFromQueue() {
*
* @return The peeked request.
*/
public RestRequest<T> peekRequestFromQueue() {
public RestRequest peekRequestFromQueue() {
return requestQueue.peek();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public RateLimitManager(AzureApi api) {
*
* @param request The request to queue.
*/
public void queueRequest(RestRequest<T> request) {
public void queueRequest(RestRequest request) {
Optional<RateLimitBucket<T, T4, T3>> searchBucket = searchBucket(request);

if (searchBucket.isEmpty()) {
Expand All @@ -72,13 +72,13 @@ public void queueRequest(RestRequest<T> request) {
* @param bucket The bucket to submit the request to.
*/
private void submitRequest(RateLimitBucket<T, T4, T3> bucket) {
RestRequest<T> currentRequest = bucket.peekRequestFromQueue();
RestRequestResult<T> result = null;
RestRequest currentRequest = bucket.peekRequestFromQueue();
RestRequestResult result = null;

long responseTimestamp = System.currentTimeMillis();

while (currentRequest != null) {
RestRequestHandler<T> newResult =
RestRequestHandler newResult =
handleCurrentRequest(result, currentRequest, bucket, responseTimestamp);

result = newResult.getResult();
Expand All @@ -96,9 +96,9 @@ private void submitRequest(RateLimitBucket<T, T4, T3> bucket) {
* @param responseTimestamp The timestamp directly after the response finished.
* @return The result of the current request.
*/
RestRequestHandler<T> handleCurrentRequest(
RestRequestResult<T> result,
RestRequest<T> currentRequest,
RestRequestHandler handleCurrentRequest(
RestRequestResult result,
RestRequest currentRequest,
RateLimitBucket<T, T4, T3> bucket,
long responseTimestamp) {

Expand Down Expand Up @@ -131,7 +131,7 @@ RestRequestHandler<T> handleCurrentRequest(
}
}

return new RestRequestHandler<>(result, currentRequest, responseTimestamp);
return new RestRequestHandler(result, currentRequest, responseTimestamp);
}

/**
Expand Down Expand Up @@ -164,10 +164,10 @@ void waitUntilSpaceGetsAvailable(RateLimitBucket<T, T4, T3> bucket) {
* @param bucket The bucket to retry the request for.
* @return The request that was retried.
*/
RestRequest<T> retryRequest(RateLimitBucket<T, T4, T3> bucket) {
RestRequest retryRequest(RateLimitBucket<T, T4, T3> bucket) {
synchronized (buckets) {
bucket.pollRequestFromQueue();
RestRequest<T> request = bucket.peekRequestFromQueue();
RestRequest request = bucket.peekRequestFromQueue();
if (request == null) {
buckets.remove(bucket);
}
Expand All @@ -182,8 +182,7 @@ RestRequest<T> retryRequest(RateLimitBucket<T, T4, T3> bucket) {
* @param t The exception to map.
* @return The mapped exception.
*/
@SuppressWarnings("unchecked")
private RestRequestResult<T> mapAzureException(Throwable t) {
private RestRequestResult mapAzureException(Throwable t) {
return ((AzureException) t)
.getResponse()
.map(RestRequestResponseInfoImpl.class::cast)
Expand All @@ -197,7 +196,7 @@ private RestRequestResult<T> mapAzureException(Throwable t) {
* @param request The request.
* @return The bucket that fits to the request.
*/
Optional<RateLimitBucket<T, T4, T3>> searchBucket(RestRequest<T> request) {
Optional<RateLimitBucket<T, T4, T3>> searchBucket(RestRequest request) {
synchronized (buckets) {
RateLimitBucket<T, T4, T3> bucket = getMatchingBucket(request);

Expand All @@ -218,7 +217,7 @@ Optional<RateLimitBucket<T, T4, T3>> searchBucket(RestRequest<T> request) {
* @param request The request.
* @return The bucket that matches the request.
*/
RateLimitBucket<T, T4, T3> getMatchingBucket(RestRequest<?> request) {
RateLimitBucket<T, T4, T3> getMatchingBucket(RestRequest request) {
synchronized (buckets) {
return buckets.stream()
.filter(b -> b.endpointMatches(request.getEndpoint()))
Expand All @@ -236,8 +235,8 @@ RateLimitBucket<T, T4, T3> getMatchingBucket(RestRequest<?> request) {
* @param responseTimestamp The timestamp directly after the response finished.
*/
void handleResponse(
RestRequest<T> request,
RestRequestResult<T> result,
RestRequest request,
RestRequestResult result,
RateLimitBucket<T, T4, T3> bucket,
long responseTimestamp) {
try {
Expand Down Expand Up @@ -296,8 +295,8 @@ private void handleCloudFlare(HttpHeaders headers, RateLimitBucket<T, T4, T3> bu
* @param headers The headers of the response.
*/
private void handleRateLimit(
CompletableFuture<RestRequestResult<T>> request,
RestRequestResult<T> result,
CompletableFuture<RestRequestResult> request,
RestRequestResult result,
RateLimitBucket<T, T4, T3> bucket,
HttpHeaders headers) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Optional;

/** This class represents the parameters for available languages. */
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class AvailableLanguagesParams {
// List of scopes for the languages
private List<LanguageScope> scope = List.of(LanguageScope.TRANSLATION);
Expand Down Expand Up @@ -64,7 +65,7 @@ public AvailableLanguagesParams setSourceLanguage(String sourceLanguage) {
* @param response The response to handle.
* @return An optional containing the collection of languages if the response was successful.
*/
public Optional<Collection<Language>> handleResponse(RestRequestResult<Optional<Collection<Language>>> response) {
public Optional<Collection<Language>> handleResponse(RestRequestResult response) {
if (response.getJsonBody().isNull() || !response.getJsonBody().has("translation"))
return Optional.empty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* This class represents the parameters for a language detection request. It contains a single
* field, 'text', which is the text to be analyzed for language detection.
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class DetectLanguageParams {

// The text to be analyzed for language detection
Expand Down Expand Up @@ -88,7 +89,7 @@ public ArrayNode getBody() {
* 'language'
*/
public Optional<DetectResponse> handleResponse(
RestRequestResult<Optional<DetectResponse>> response) {
RestRequestResult response) {
if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty())
return Optional.empty();
JsonNode jsonBody = response.getJsonBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* profanity, text type, alignment, sentence length, source language, target languages, and a
* fallback language.
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class TranslateParams {
// The text to be translated
private LinkedHashMap<Integer, String> toTranslate;
Expand Down Expand Up @@ -346,7 +347,7 @@ public JsonNode getBody() {
* 'translations'.
*/
public Optional<TranslationResponse> handleResponse(
RestRequestResult<Optional<TranslationResponse>> response) {
RestRequestResult response) {
if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty())
return Optional.empty();
JsonNode jsonBody = response.getJsonBody();
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.apache.logging.log4j.Logger;

/** This class is used to wrap a rest request. */
public class RestRequest<T> {
public class RestRequest {

/** The (logger) of this class. */
private static final Logger logger = LoggerUtil.getLogger(RestRequest.class);
Expand All @@ -32,7 +32,7 @@ public class RestRequest<T> {
private final Map<String, String> headers = new HashMap<>();
private volatile String body = null;

private final CompletableFuture<RestRequestResult<T>> result = new CompletableFuture<>();
private final CompletableFuture<RestRequestResult> result = new CompletableFuture<>();

/** The origin of the rest request. */
private final Exception origin;
Expand Down Expand Up @@ -167,7 +167,7 @@ public Map<String, String> getHeaders() {
* @param body The body of the request.
* @return The current instance to chain call methods.
*/
public RestRequest<T> setBody(JsonNode body) {
public RestRequest setBody(JsonNode body) {
return setBody(body.toString());
}

Expand All @@ -177,7 +177,7 @@ public RestRequest<T> setBody(JsonNode body) {
* @param body The body of the request.
* @return The current instance to chain call methods.
*/
public RestRequest<T> setBody(String body) {
public RestRequest setBody(String body) {
this.body = body;
return this;
}
Expand All @@ -197,8 +197,7 @@ public void includeAuthorizationHeader(boolean includeAuthorizationHeader) {
* @param function A function which processes the rest response to the requested object.
* @return A future which will contain the output of the function.
*/
@SuppressWarnings("unchecked")
public CompletableFuture<T> execute(Function<RestRequestResult<T>, T> function) {
public <T> CompletableFuture<T> execute(Function<RestRequestResult, T> function) {
api.getRatelimitManager().queueRequest(this);
CompletableFuture<T> future = new CompletableFuture<>();
result.whenComplete(
Expand All @@ -221,7 +220,7 @@ public CompletableFuture<T> execute(Function<RestRequestResult<T>, T> function)
*
* @return Gets the result of this request.
*/
public CompletableFuture<RestRequestResult<T>> getResult() {
public CompletableFuture<RestRequestResult> getResult() {
return result;
}

Expand Down Expand Up @@ -252,7 +251,7 @@ public RestRequestInfo asRestRequestInformation() {
* @throws AzureException Thrown in case of an error while requesting azure.
* @throws IOException Thrown if an error occurs while reading the response.
*/
public RestRequestResult<T> executeBlocking()
public RestRequestResult executeBlocking()
throws AzureException, IOException, URISyntaxException {
URI fullUrl = endpoint.getHttpUrl(api.getBaseURL(), queryParameters);
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder().uri(fullUrl);
Expand All @@ -276,14 +275,14 @@ public RestRequestResult<T> executeBlocking()
getApi()
.getHttpClient()
.sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
RestRequestResult<T> responseResult = handleResponse(fullUrl, response.join());
RestRequestResult responseResult = handleResponse(fullUrl, response.join());
result.complete(responseResult);
return responseResult;
}

private RestRequestResult<T> handleResponse(URI fullUrl, HttpResponse<String> response)
private RestRequestResult handleResponse(URI fullUrl, HttpResponse<String> response)
throws IOException, AzureException {
RestRequestResult<T> requestResult = new RestRequestResult<>(this, response);
RestRequestResult requestResult = new RestRequestResult(this, response);
String bodyString = requestResult.getStringBody().orElse("empty");
logger.debug(
"Sent {} request to {} and received status code {} with body {}",
Expand All @@ -299,11 +298,11 @@ private RestRequestResult<T> handleResponse(URI fullUrl, HttpResponse<String> re
return requestResult;
}

private RestRequestResult<T> handleError(int resultCode, RestRequestResult<T> result)
private RestRequestResult handleError(int resultCode, RestRequestResult result)
throws AzureException {
RestRequestInfo requestInformation = asRestRequestInformation();
RestRequestResponseInfo responseInformation =
new RestRequestResponseInfoImpl<>(requestInformation, result);
new RestRequestResponseInfoImpl(requestInformation, result);
Optional<RestRequestHttpResponseCode> responseCodeOptional =
RestRequestHttpResponseCode.fromCode(resultCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* current request being processed, and the timestamp of the response.
*
*/
public class RestRequestHandler<T> {
public class RestRequestHandler {
// The result of the REST request
private final RestRequestResult<T> result;
private final RestRequestResult result;
// The current request being processed
private final RestRequest<T> currentRequest;
private final RestRequest currentRequest;
// The timestamp of the response
private final long responseTimestamp;

Expand All @@ -22,7 +22,7 @@ public class RestRequestHandler<T> {
* @param responseTimestamp the timestamp of the response
*/
public RestRequestHandler(
RestRequestResult<T> result, RestRequest<T> currentRequest, long responseTimestamp) {
RestRequestResult result, RestRequest currentRequest, long responseTimestamp) {
this.result = result;
this.currentRequest = currentRequest;
this.responseTimestamp = responseTimestamp;
Expand All @@ -33,7 +33,7 @@ public RestRequestHandler(
*
* @return the result of the REST request
*/
public RestRequestResult<T> getResult() {
public RestRequestResult getResult() {
return result;
}

Expand All @@ -42,7 +42,7 @@ public RestRequestResult<T> getResult() {
*
* @return the current request being processed
*/
public RestRequest<T> getCurrentRequest() {
public RestRequest getCurrentRequest() {
return currentRequest;
}

Expand Down
Loading

0 comments on commit 027dcc3

Please sign in to comment.