Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Context::putAllMap #3102

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 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,20 @@ default Context putAll(ContextView other) {
return newContext;
}

default Context putAllMap(Map<?, ?> from) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add javadoc shortly.

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
13 changes: 12 additions & 1 deletion reactor-core/src/main/java/reactor/util/context/ContextN.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2015-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 Down Expand Up @@ -184,6 +184,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
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)));
}
}
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}");
}
}
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}");
}
}
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}");
}
}
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}");
}
}