Skip to content

Commit

Permalink
Remove Google Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jan 17, 2024
1 parent 04d52a7 commit 76f354c
Showing 7 changed files with 32 additions and 42 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -105,11 +105,6 @@
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.0.0-jre</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Original file line number Diff line number Diff line change
@@ -2,17 +2,20 @@

import io.github.brenoepics.at4j.data.DetectedLanguage;
import io.github.brenoepics.at4j.data.Translation;

import java.util.Collection;
import javax.annotation.Nullable;

/**
* This class represents a response from a translation service. It contains the detected language of
* the input and a collection of translations.
*/
public class TranslationResponse {

// The detected language of the input text. It can be null if the language could not be detected.
@Nullable private DetectedLanguage detectedLanguage = null;
/** The detected language of the input text. It can be null if the language could not be detected.
* @see DetectedLanguage
*/

private DetectedLanguage detectedLanguage = null;

// A collection of translations for the input text.
private final Collection<Translation> translations;
@@ -24,7 +27,7 @@ public class TranslationResponse {
* @param translations A collection of translations for the input text.
*/
public TranslationResponse(
@Nullable DetectedLanguage detectedLanguage, Collection<Translation> translations) {
DetectedLanguage detectedLanguage, Collection<Translation> translations) {
this.detectedLanguage = detectedLanguage;
this.translations = translations;
}
@@ -44,7 +47,7 @@ public TranslationResponse(Collection<Translation> translations) {
*
* @param detectedLanguage The detected language of the input text.
*/
public void setDetectedLanguage(@Nullable DetectedLanguage detectedLanguage) {
public void setDetectedLanguage(DetectedLanguage detectedLanguage) {
this.detectedLanguage = detectedLanguage;
}

@@ -53,7 +56,6 @@ public void setDetectedLanguage(@Nullable DetectedLanguage detectedLanguage) {
*
* @return The detected language of the input text.
*/
@Nullable
public DetectedLanguage getDetectedLanguage() {
return detectedLanguage;
}
22 changes: 8 additions & 14 deletions src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java
Original file line number Diff line number Diff line change
@@ -6,14 +6,10 @@
import io.github.brenoepics.at4j.core.AzureApiImpl;
import io.github.brenoepics.at4j.core.exceptions.AzureException;
import io.github.brenoepics.at4j.util.logging.LoggerUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.function.Function;
@@ -33,7 +29,7 @@ public class RestRequest<T> {

private volatile boolean includeAuthorizationHeader = true;
private AtomicReferenceArray<String> urlParameters = new AtomicReferenceArray<>(new String[0]);
private final Multimap<String, String> queryParameters = ArrayListMultimap.create();
private final Map<String, Collection<String>> queryParameters = new HashMap<>();
private final Map<String, String> headers = new HashMap<>();
private volatile String body = null;

@@ -59,7 +55,7 @@ public RestRequest(AzureApi api, RestMethod method, RestEndpoint endpoint) {
this.api = (AzureApiImpl) api;
this.method = method;
this.endpoint = endpoint;
this.queryParameters.put("api-version", AT4J.AZURE_TRANSLATOR_API_VERSION);
addQueryParameter("api-version", AT4J.AZURE_TRANSLATOR_API_VERSION);

this.origin = new Exception("origin of RestRequest call");
}
@@ -96,7 +92,7 @@ public RestEndpoint getEndpoint() {
*
* @return The query parameters of this request.
*/
public Multimap<String, String> getQueryParameters() {
public Map<String, Collection<String>> getQueryParameters() {
return queryParameters;
}

@@ -162,7 +158,7 @@ public Exception getOrigin() {
* @return The current instance to chain call methods.
*/
public RestRequest<T> addQueryParameter(String key, String value) {
queryParameters.put(key, value);
queryParameters.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
return this;
}

@@ -313,13 +309,11 @@ public RestRequestInformation asRestRequestInformation() {
*/
public RestRequestResult<T> executeBlocking() throws AzureException, IOException {
Request.Builder requestBuilder = new Request.Builder();
String[] parameters = new String[urlParameters.length()];
for (int i = 0; i < urlParameters.length(); i++) {
parameters[i] = urlParameters.get(i);
}
String[] parameters = getUrlParameters();
HttpUrl.Builder httpUrlBuilder =
endpoint.getOkHttpUrl(api.getBaseURL(), parameters).newBuilder();
queryParameters.forEach(httpUrlBuilder::addQueryParameter);
queryParameters.forEach(
(key, values) -> values.forEach(value -> httpUrlBuilder.addQueryParameter(key, value)));
requestBuilder.url(httpUrlBuilder.build());
request(requestBuilder);

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.brenoepics.at4j.util.rest;

import io.github.brenoepics.at4j.AzureApi;
import com.google.common.collect.Multimap;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

@@ -28,7 +28,7 @@ public interface RestRequestInformation {
*
* @return The query parameters of the rest request.
*/
Multimap<String, String> getQueryParameters();
Map<String, Collection<String>> getQueryParameters();

/**
* Gets the headers of the rest request.
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package io.github.brenoepics.at4j.util.rest;

import io.github.brenoepics.at4j.AzureApi;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.*;

/** The implementation of {@link RestRequestInformation}. */
public class RestRequestInformationImpl implements RestRequestInformation {

private final AzureApi api;
private final URL url;
private final Multimap<String, String> queryParameters;
private final Map<String, Collection<String>> queryParameters;
private final Map<String, String> headers;
private final String body;

@@ -30,7 +25,7 @@ public class RestRequestInformationImpl implements RestRequestInformation {
public RestRequestInformationImpl(
AzureApi api,
URL url,
Multimap<String, String> queryParameter,
Map<String, Collection<String>> queryParameter,
Map<String, String> headers,
String body) {
this.api = api;
@@ -51,8 +46,8 @@ public URL getUrl() {
}

@Override
public Multimap<String, String> getQueryParameters() {
return Multimaps.unmodifiableMultimap(queryParameters);
public Map<String, Collection<String>> getQueryParameters() {
return Collections.unmodifiableMap(queryParameters);
}

@Override
Original file line number Diff line number Diff line change
@@ -2,16 +2,16 @@

import io.github.brenoepics.at4j.azure.BaseURL;
import io.github.brenoepics.at4j.core.AzureApiImpl;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import io.github.brenoepics.at4j.util.rest.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
@@ -25,7 +25,7 @@ class AzureExceptionTest<T> {
@BeforeEach
public void setup() throws MalformedURLException {
URL url = new URL("https://" + BaseURL.GLOBAL.getUrl());
Multimap<String, String> queryParameter = Multimaps.forMap(new HashMap<>());
Map<String, Collection<String>> queryParameter = new HashMap<>();
HashMap<String, String> headers = new HashMap<>();
String body = "Test body";
request = new RestRequestInformationImpl(api, url, queryParameter, headers, body);
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.util.Arrays;

class RestRequestTest<T> {

@Mock private AzureApiImpl<T> api;
@@ -27,8 +29,10 @@ void shouldAddQueryParameter() {
restRequest.addQueryParameter("to", "pt");
restRequest.addQueryParameter("to", "es");
restRequest.addQueryParameter("to", "fr");
Assertions.assertEquals(4, restRequest.getQueryParameters().size());
Assertions.assertTrue(restRequest.getQueryParameters().containsEntry("to", "pt"));
Assertions.assertTrue(restRequest.getQueryParameters().containsKey("to"));
Assertions.assertTrue(restRequest.getQueryParameters().get("to").containsAll(
Arrays.asList("pt", "es", "fr")
));
}

@Test

0 comments on commit 76f354c

Please sign in to comment.