Skip to content

Commit

Permalink
use Map.of
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza committed Oct 2, 2024
1 parent 9dbe04f commit e4a373c
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;

public abstract class AbstractDaprKeyValueAdapter implements KeyValueAdapter {
private static final Map<String, String> CONTENT_TYPE_META = Collections.singletonMap(
private static final Map<String, String> CONTENT_TYPE_META = Map.of(
"contentType", "application/json");

private final DaprClient daprClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) {
}

private void execUsingBinding(String sql) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

daprClient.invokeBinding(bindingName, "exec", null, meta).block();
}

private <T> T queryUsingBinding(String sql, TypeRef<T> typeRef) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) {
}

private void execUsingBinding(String sql) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

daprClient.invokeBinding(bindingName, "exec", null, meta).block();
}

private <T> T queryUsingBinding(String sql, TypeRef<T> typeRef) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block();
}
Expand Down
10 changes: 9 additions & 1 deletion dapr-spring/dapr-spring-messaging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
<artifactId>dapr-spring-messaging</artifactId>
<name>dapr-spring-messaging</name>
<description>Dapr Spring Messaging</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk</artifactId>
<version>1.13.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<packaging>jar</packaging>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.dapr.client.domain.Metadata;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.Map;

public class DaprMessagingTemplate<T> implements DaprMessagingOperations<T> {

Expand Down Expand Up @@ -49,7 +49,7 @@ private Mono<Void> doSendAsync(String topic, T message) {
return daprClient.publishEvent(pubsubName,
topic,
message,
Collections.singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS));
Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS));
}

private static class SendMessageBuilderImpl<T> implements SendMessageBuilder<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.dapr.client.domain.Metadata;
import io.dapr.client.domain.PublishEventRequest;

import java.util.Map;
import java.util.UUID;

import static java.util.Collections.singletonMap;
Expand Down Expand Up @@ -66,7 +67,7 @@ public static void main(String[] args) throws Exception {
client.publishEvent(
new PublishEventRequest(PUBSUB_NAME, TOPIC_NAME, cloudEvent)
.setContentType(CloudEvent.CONTENT_TYPE)
.setMetadata(singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block();
.setMetadata(Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block();
System.out.println("Published cloud event with message: " + cloudEvent.getData());

try {
Expand Down
14 changes: 6 additions & 8 deletions sdk-tests/src/test/java/io/dapr/it/DaprPorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -40,12 +38,12 @@ private DaprPorts(Integer appPort, Integer httpPort, Integer grpcPort) {
this.grpcPort = grpcPort;
this.httpPort = httpPort;
this.appPort = appPort;
this.overrides = Collections.unmodifiableMap(new HashMap<>(){{
put(Properties.GRPC_PORT, grpcPort.toString());
put(Properties.HTTP_PORT, httpPort.toString());
put(Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort);
put(Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort);
}});
this.overrides = Map.of(
Properties.GRPC_PORT, grpcPort.toString(),
Properties.HTTP_PORT, httpPort.toString(),
Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort,
Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort
);
}

public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static io.dapr.it.Retry.callWithRetry;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -115,14 +116,14 @@ public void inputOutputBinding() throws Exception {

System.out.println("sending first message");
client.invokeBinding(
bidingName, "create", myClass, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block();
bidingName, "create", myClass, Map.of("MyMetadata", "MyValue"), Void.class).block();

// This is an example of sending a plain string. The input binding will receive
// cat
final String m = "cat";
System.out.println("sending " + m);
client.invokeBinding(
bidingName, "create", m, Collections.singletonMap("MyMetadata", "MyValue"), Void.class).block();
bidingName, "create", m, Map.of("MyMetadata", "MyValue"), Void.class).block();

// Metadata is not used by Kafka component, so it is not possible to validate.
callWithRetry(() -> {
Expand Down
5 changes: 3 additions & 2 deletions sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -534,7 +535,7 @@ public void testPubSubTTLMetadata() throws Exception {
PUBSUB_NAME,
TTL_TOPIC_NAME,
message,
Collections.singletonMap(Metadata.TTL_IN_SECONDS, "1")).block();
Map.of(Metadata.TTL_IN_SECONDS, "1")).block();
System.out.println(String.format("Published message: '%s' to topic '%s' pubsub_name '%s'", message, TOPIC_NAME, PUBSUB_NAME));
}
}
Expand Down Expand Up @@ -652,7 +653,7 @@ public void testLongValues() throws Exception {
PUBSUB_NAME,
LONG_TOPIC_NAME,
value,
Collections.singletonMap(Metadata.TTL_IN_SECONDS, "30")).block();
Map.of(Metadata.TTL_IN_SECONDS, "30")).block();

try {
Thread.sleep((long) (1000 * Math.random()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class DaprKeyValueRepositoryIT {
"host=postgres-repository user=postgres password=password port=5432 connect_timeout=10 database=dapr_db_repository";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class MySQLDaprKeyValueTemplateIT {
private static final String BINDING_DSN = "mysql:password@tcp(mysql:3306)/dapr_db";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("url", BINDING_DSN);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("url", BINDING_DSN);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down Expand Up @@ -114,7 +114,7 @@ private static Map<String, String> createStateStoreProperties() {
*/
@AfterEach
public void tearDown() {
var meta = Collections.singletonMap("sql", "delete from state");
var meta = Map.of("sql", "delete from state");

daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PostgreSQLDaprKeyValueTemplateIT {
"host=postgres user=postgres password=password port=5432 connect_timeout=10 database=dapr_db";
private static final Map<String, String> STATE_STORE_PROPERTIES = createStateStoreProperties();

private static final Map<String, String> BINDING_PROPERTIES = Collections.singletonMap("connectionString", CONNECTION_STRING);
private static final Map<String, String> BINDING_PROPERTIES = Map.of("connectionString", CONNECTION_STRING);

private static final Network DAPR_NETWORK = Network.newNetwork();

Expand Down Expand Up @@ -96,7 +96,7 @@ private static Map<String, String> createStateStoreProperties() {

@BeforeEach
public void setUp() {
var meta = Collections.singletonMap("sql", "delete from state");
var meta = Map.of("sql", "delete from state");

daprClient.invokeBinding(BINDING_NAME, "exec", null, meta).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -149,7 +148,7 @@ public void testPubSub() throws Exception {

try (DaprClient client = (builder).build()) {
String message = "message content";
Map<String, String> metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
Map<String, String> metadata = Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
client.publishEvent(PUBSUB_NAME, PUBSUB_TOPIC_NAME, message, metadata).block();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -59,7 +60,7 @@ public class DaprWorkflowsIT {
.withAppName("workflow-dapr-app")
.withNetwork(DAPR_NETWORK)
.withComponent(new Component("kvstore", "state.in-memory", "v1",
Collections.singletonMap("actorStateStore", "true")))
Map.of("actorStateStore", "true")))
.withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap()))
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/main/java/io/dapr/client/DaprHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public class DaprHttp implements AutoCloseable {
/**
* Context entries allowed to be in HTTP Headers.
*/
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate")));
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS = Set.of("grpc-trace-bin", "traceparent", "tracestate");

/**
* Object mapper to parse DaprError with or without details.
Expand Down
5 changes: 2 additions & 3 deletions sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BulkPublishEntry(String entryId, T event, String contentType) {
this.entryId = entryId;
this.event = event;
this.contentType = contentType;
this.metadata = Collections.unmodifiableMap(new HashMap<>());
this.metadata = Map.of();
}

/**
Expand All @@ -70,8 +70,7 @@ public BulkPublishEntry(String entryId, T event, String contentType, Map<String,
this.entryId = entryId;
this.event = event;
this.contentType = contentType;
this.metadata = metadata == null ? Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(metadata);
this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata);
}

public String getEntryId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ public BulkPublishRequest(String pubsubName, String topic, List<BulkPublishEntry
Map<String, String> metadata) {
this.pubsubName = pubsubName;
this.topic = topic;
this.entries = entries == null ? Collections.unmodifiableList(new ArrayList<>()) :
Collections.unmodifiableList(entries);
this.metadata = metadata == null ? Collections.unmodifiableMap(new HashMap<>()) :
Collections.unmodifiableMap(metadata);
this.entries = entries == null ? List.of() : Collections.unmodifiableList(entries);
this.metadata = metadata == null ? Map.of() : Collections.unmodifiableMap(metadata);
}

public String getPubsubName() {
Expand Down
6 changes: 2 additions & 4 deletions sdk/src/main/java/io/dapr/config/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -194,8 +192,8 @@ public Properties() {
* @param overridesInput to override static properties
*/
public Properties(Map<?, String> overridesInput) {
this.overrides = overridesInput == null ? Collections.emptyMap() :
Collections.unmodifiableMap(overridesInput.entrySet().stream()
this.overrides = overridesInput == null ? Map.of() :
Map.copyOf(overridesInput.entrySet().stream()
.filter(e -> e.getKey() != null)
.filter(e -> e.getValue() != null)
.collect(Collectors.toMap(
Expand Down
26 changes: 12 additions & 14 deletions sdk/src/main/java/io/dapr/exceptions/DaprErrorDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ public class DaprErrorDetails {
static final DaprErrorDetails EMPTY_INSTANCE = new DaprErrorDetails((Status) null);

private static final Map<Class<? extends Message>, ErrorDetailType> SUPPORTED_ERROR_TYPES =
Collections.unmodifiableMap(new HashMap<>() {
{
put(com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO);
put(com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO);
put(com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO);
put(com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE);
put(com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE);
put(com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST);
put(com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO);
put(com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO);
put(com.google.rpc.Help.class, ErrorDetailType.HELP);
put(com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE);
}
});
Map.of(
com.google.rpc.ErrorInfo.class, ErrorDetailType.ERROR_INFO,
com.google.rpc.RetryInfo.class, ErrorDetailType.RETRY_INFO,
com.google.rpc.DebugInfo.class, ErrorDetailType.DEBUG_INFO,
com.google.rpc.QuotaFailure.class, ErrorDetailType.QUOTA_FAILURE,
com.google.rpc.PreconditionFailure.class, ErrorDetailType.PRECONDITION_FAILURE,
com.google.rpc.BadRequest.class, ErrorDetailType.BAD_REQUEST,
com.google.rpc.RequestInfo.class, ErrorDetailType.REQUEST_INFO,
com.google.rpc.ResourceInfo.class, ErrorDetailType.RESOURCE_INFO,
com.google.rpc.Help.class, ErrorDetailType.HELP,
com.google.rpc.LocalizedMessage.class, ErrorDetailType.LOCALIZED_MESSAGE
);

private static final Map<String, Class<? extends Message>> ERROR_TYPES_FQN_REVERSE_LOOKUP =
SUPPORTED_ERROR_TYPES.keySet().stream().collect(Collectors.toMap(
Expand Down
16 changes: 8 additions & 8 deletions sdk/src/test/java/io/dapr/utils/NetworkUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -22,11 +21,12 @@ public class NetworkUtilsTest {

@BeforeEach
public void setUp() {
propertiesOverride = new HashMap<>(){{
put(Properties.GRPC_PORT.getName(), Integer.toString(defaultGrpcPort));
put(Properties.SIDECAR_IP.getName(), defaultSidecarIP);
put(Properties.GRPC_ENDPOINT.getName(), "");
}};
// Must be mutable for some test scenarios here.
propertiesOverride = new HashMap<>(Map.of(
Properties.GRPC_PORT.getName(), Integer.toString(defaultGrpcPort),
Properties.SIDECAR_IP.getName(), defaultSidecarIP,
Properties.GRPC_ENDPOINT.getName(), ""
));
}

@AfterEach
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void testGrpcEndpointParsingScenario(
String expectedEndpoint,
boolean expectSecure
) {
var override = Collections.singletonMap(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue);
var override = Map.of(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue);
var settings = NetworkUtils.GrpcEndpointSettings.parse(new Properties(override));

Assertions.assertEquals(expectedEndpoint, settings.endpoint);
Expand All @@ -153,7 +153,7 @@ private static void testGrpcEndpointParsingScenario(

private static void testGrpcEndpointParsingErrorScenario(String grpcEndpointEnvValue) {
try {
var override = Collections.singletonMap(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue);
var override = Map.of(Properties.GRPC_ENDPOINT.getName(), grpcEndpointEnvValue);
NetworkUtils.GrpcEndpointSettings.parse(new Properties(override));
Assert.fail();
} catch (IllegalArgumentException e) {
Expand Down

0 comments on commit e4a373c

Please sign in to comment.