Skip to content

Commit

Permalink
Introducing Context::putAllMap (#3102)
Browse files Browse the repository at this point in the history
New method for easier write capability to `Context` when the user
is interacting with a generic `Map` instead of a `Context` and wants
all values to be added without creating an additional `ContextView` holder.
Resolves #3080
  • Loading branch information
chemicL committed Jul 5, 2022
1 parent bc40e94 commit a776230
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 44 deletions.
3 changes: 2 additions & 1 deletion reactor-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ task japicmp(type: JapicmpTask) {
// TODO after a .0 release, remove the reactor-core exclusions below if any
classExcludes = [ ]
methodExcludes = [
'reactor.util.context.ContextView#forEach(java.util.function.BiConsumer)'
'reactor.util.context.ContextView#forEach(java.util.function.BiConsumer)',
'reactor.util.context.Context#putAllMap(java.util.Map)'
]
}

Expand Down
29 changes: 24 additions & 5 deletions reactor-core/src/main/java/reactor/util/context/Context.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2017-2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,7 @@

package reactor.util.context;

import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

import reactor.util.annotation.Nullable;

Expand Down Expand Up @@ -283,6 +280,28 @@ default Context putAll(ContextView other) {
return newContext;
}

/**
* Create a new {@link Context} by merging the content of this context and a given
* {@link Map}. If the {@link Map} is empty, the same {@link Context} instance
* is returned.
*
* @param from the {@link Map} from which to include entries in the resulting {@link Context}.
* @return a new {@link Context} with a merge of the entries from this context and the given {@link Map}.
*/
default Context putAllMap(Map<?, ?> from) {
if (from.isEmpty()) {
return this;
}

ContextN combined = new ContextN(this.size() + from.size());
this.forEach(combined);
from.forEach(combined);
if (combined.size() <= 5) {
return Context.of((Map<?, ?>) combined);
}
return combined;
}

/**
* See {@link #putAll(ContextView)}.
*
Expand Down
11 changes: 11 additions & 0 deletions reactor-core/src/main/java/reactor/util/context/ContextN.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ public Context putAll(ContextView other) {
return newContext;
}

@Override
public Context putAllMap(Map<?, ?> from) {
if (from.isEmpty()) {
return this;
}

ContextN newContext = new ContextN(this);
from.forEach(newContext);
return newContext;
}

@Override
public String toString() {
return "ContextN" + super.toString();
Expand Down
34 changes: 32 additions & 2 deletions reactor-core/src/test/java/reactor/util/context/Context0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package reactor.util.context;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

public class Context0Test {
Expand Down Expand Up @@ -180,4 +180,34 @@ public void unsafePutAllIntoIsNoOp() {
.containsEntry(1, "SHOULD NOT BE REPLACED")
.hasSize(1);
}

@Test
void putAllMap() {
Map<Object, Object> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context3.class)
.hasToString("Context3{A=1, B=2, C=3}");
}

@Test
void putAllMapEmpty() {
Context put = c.putAllMap(Collections.emptyMap());
assertThat(put).isSameAs(c);
}

@Test
void putAllMapNullKey() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap(null, "oops")));
}

@Test
void putAllMapNullValue() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap("A", null)));
}
}
50 changes: 43 additions & 7 deletions reactor-core/src/test/java/reactor/util/context/Context1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@

package reactor.util.context;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static reactor.util.context.ContextTest.key;
Expand Down Expand Up @@ -245,4 +241,44 @@ public void unsafePutAllIntoShouldReplace() {
.hasSize(2);
}

@Test
void putAllMap() {
Map<Object, Object> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context4.class)
.hasToString("Context4{1=A, A=1, B=2, C=3}");
}

@Test
void putAllMapEmpty() {
Context put = c.putAllMap(Collections.emptyMap());
assertThat(put).isSameAs(c);
}

@Test
void putAllMapNullKey() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap(null, "oops")));
}

@Test
void putAllMapNullValue() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap("A", null)));
}

@Test
void putAllMapReplaces() {
Map<Object, Object> map = new HashMap<>();
map.put(c.key, "replaced");
map.put("A", 1);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context2.class)
.hasToString("Context2{1=replaced, A=1}");
}
}
44 changes: 42 additions & 2 deletions reactor-core/src/test/java/reactor/util/context/Context2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package reactor.util.context;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static reactor.util.context.ContextTest.key;
Expand Down Expand Up @@ -260,4 +260,44 @@ public void unsafePutAllIntoShouldReplace() {
.hasSize(3);
}

@Test
void putAllMap() {
Map<Object, Object> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context5.class)
.hasToString("Context5{1=A, 2=B, A=1, B=2, C=3}");
}

@Test
void putAllMapEmpty() {
Context put = c.putAllMap(Collections.emptyMap());
assertThat(put).isSameAs(c);
}

@Test
void putAllMapNullKey() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap(null, "oops")));
}

@Test
void putAllMapNullValue() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap("A", null)));
}

@Test
void putAllMapReplaces() {
Map<Object, Object> map = new HashMap<>();
map.put(c.key1, "replaced");
map.put("A", 1);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context3.class)
.hasToString("Context3{1=replaced, 2=B, A=1}");
}
}
45 changes: 43 additions & 2 deletions reactor-core/src/test/java/reactor/util/context/Context3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package reactor.util.context;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static reactor.util.context.ContextTest.key;
Expand Down Expand Up @@ -288,4 +288,45 @@ public void unsafePutAllIntoShouldReplace() {
.containsEntry("extra", "value")
.hasSize(4);
}

@Test
void putAllMap() {
Map<Object, Object> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(ContextN.class)
.hasToString("ContextN{1=A, 2=B, 3=C, A=1, B=2, C=3}");
}

@Test
void putAllMapEmpty() {
Context put = c.putAllMap(Collections.emptyMap());
assertThat(put).isSameAs(c);
}

@Test
void putAllMapNullKey() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap(null, "oops")));
}

@Test
void putAllMapNullValue() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap("A", null)));
}

@Test
void putAllMapReplaces() {
Map<Object, Object> map = new HashMap<>();
map.put(c.key1, "replaced");
map.put("A", 1);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context4.class)
.hasToString("Context4{1=replaced, 2=B, 3=C, A=1}");
}
}
45 changes: 42 additions & 3 deletions reactor-core/src/test/java/reactor/util/context/Context4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

package reactor.util.context;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static reactor.util.context.ContextTest.key;
import static reactor.util.context.ContextTest.keyValue;

Expand Down Expand Up @@ -450,4 +449,44 @@ public void unsafePutAllIntoShouldReplace() {
.hasSize(5);
}

@Test
void putAllMap() {
Map<Object, Object> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(ContextN.class)
.hasToString("ContextN{1=A, 2=B, 3=C, 4=D, A=1, B=2, C=3}");
}

@Test
void putAllMapEmpty() {
Context put = c.putAllMap(Collections.emptyMap());
assertThat(put).isSameAs(c);
}

@Test
void putAllMapNullKey() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap(null, "oops")));
}

@Test
void putAllMapNullValue() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> c.putAllMap(Collections.singletonMap("A", null)));
}

@Test
void putAllMapReplaces() {
Map<Object, Object> map = new HashMap<>();
map.put(c.key1, "replaced");
map.put("A", 1);
Context put = c.putAllMap(map);

assertThat(put).isInstanceOf(Context5.class)
.hasToString("Context5{1=replaced, 2=B, 3=C, 4=D, A=1}");
}
}

0 comments on commit a776230

Please sign in to comment.