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 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
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 Maps to merge.
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 to write as description : Iterable of {@link Map}s to merge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@baudoliver7 Fixed.

*/
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
23 changes: 23 additions & 0 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 @@ -35,6 +37,27 @@
*/
public final class MergedTest {

@Test
public void behavesAsMapCreatedFromIterable() {
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<>(
new MapOf<>(
new MapEntry<>("a", 1)
),
new MapOf<>(
new MapEntry<>("b", 2)
)
),
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 think you wanted to test Merged with Iterable constructor :) It should be like this :

new Merged<>(
    new IterableOf<>(
          new MapOf<>(
              new MapEntry<>("a", 1)
          ),
          new MapOf<>(
              new MapEntry<>("b", 2)
          )
    )
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@baudoliver7 Actually not, there's not need to test Iterable ctor because it is primary and getting hit anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

@andreoss That's right ! But it's because of the test name (behavesAsMapCreatedFromIterable) that I said that. However, you change it in your last commit.

new AllOf<>(
new IterableOf<>(
new BehavesAsMap<>("a", 1),
new BehavesAsMap<>("b", 2)
)
)
).affirm();
}

@Test
public void behavesAsMap() {
new Assertion<>(
Expand Down