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

#1549: Add ctor to map.Merged #1550

Merged
merged 5 commits into from Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 17 additions & 6 deletions src/main/java/org/cactoos/map/Merged.java
Expand Up @@ -24,8 +24,9 @@
package org.cactoos.map;

import java.util.Map;
import java.util.stream.Collectors;
import org.cactoos.list.ListOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;

/**
* This class can be used to merge a few {@link Map}.
Expand All @@ -42,12 +43,22 @@ public final class Merged<K, V> extends MapEnvelope<K, V> {
*/
@SafeVarargs
public Merged(final Map<? extends K, ? extends V>... maps) {
this(new IterableOf<>(maps));
}

/**
* Ctor.
* @param maps Iterable of {@link Map}s to merge.
*/
public Merged(final Iterable<? extends Map<? extends K, ? extends V>> maps) {
super(
new MapOf<>(
new ListOf<>(maps)
.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toList())
new Joined<>(
new Mapped<>(
Map::entrySet,
maps
)
)
)
);
}
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/org/cactoos/map/MergedTest.java
Expand Up @@ -23,6 +23,8 @@
*/
package org.cactoos.map;

import org.cactoos.iterable.IterableOf;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -31,21 +33,30 @@
* Test case for {@link Merged}.
*
* @since 1.0
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class MergedTest {

@Test
@SuppressWarnings("unchecked")
public void behavesAsMap() {
new Assertion<>(
"Must behave as a map",
Copy link
Contributor

Choose a reason for hiding this comment

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

@andreoss I suggest this message : Must behave as a map when created from iterable

new Merged<Integer, Integer>(
new MapOf<Integer, Integer>(
new MapEntry<>(0, -1),
new MapEntry<>(1, 1)
new Merged<>(
new MapOf<>(
new MapEntry<>("a", 1)
),
new MapOf<>(
new MapEntry<>("b", 2)
)
),
new BehavesAsMap<>(1, 1)
new AllOf<>(
new IterableOf<>(
new BehavesAsMap<>("a", 1),
new BehavesAsMap<>("b", 2)
)
)
).affirm();
}

Expand Down