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

(#1259) Sorted Set with custom comparator #1279

Merged
merged 4 commits into from
Feb 10, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ final Iterable<String> sorted = new Sorted<>(
);
```

To create a sorted set from existing vararg elements using comparator:
```java
final Set<String> sorted = new org.cactoos.set.Sorted<>(
(first, second) -> first.compareTo(second),
"abc", "bcd", "abc", "ccc", "acd"
);
```

To create a sorted set from existing iterable using comparator:
```java
final Set<String> sorted = new org.cactoos.set.Sorted<>(
(first, second) -> first.compareTo(second),
new IterableOf<>("abc", "bcd", "abc", "ccc", "acd")
);
```

## Funcs and Procs

This is a traditional `foreach` loop:
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/cactoos/set/SetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
*/
package org.cactoos.set;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.cactoos.collection.CollectionEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.Unchecked;

Expand All @@ -42,7 +40,7 @@
* @param <T> Set type
* @since 0.49.2
*/
public final class SetOf<T> extends CollectionEnvelope<T> implements Set<T> {
public final class SetOf<T> extends SetEnvelope<T> {

/**
* Ctor.
Expand All @@ -64,7 +62,7 @@ public SetOf(final Iterable<T> src) {
() -> {
final Set<T> tmp = new HashSet<>();
src.forEach(tmp::add);
return Collections.unmodifiableSet(tmp);
return tmp;
}
).value()
);
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/org/cactoos/set/Sorted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.set;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.Unchecked;

/**
* Sorted Iterable as {@link Set}.
*
* <p>This class should be used very carefully. You must understand that
* it will fetch the entire content of the encapsulated {@link Set} on each
* method call. It doesn't cache the data anyhow. </p>
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Set type
* @since 1.0.0
*/
public final class Sorted<T> extends SetEnvelope<T> {

/**
* Ctor.
* @param cmp Comparator
* @param array An array of some elements
*/
@SafeVarargs
public Sorted(final Comparator<T> cmp, final T... array) {
this(cmp, new IterableOf<>(array));
}

/**
* Ctor.
* @param cmp Comparator
* @param src An {@link Iterable}
*/
public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
super(
new Unchecked<>(
() -> {
final Set<T> set = new TreeSet<>(cmp);
src.forEach(set::add);
return set;
}
).value()
);
}
}
128 changes: 128 additions & 0 deletions src/test/java/org/cactoos/set/SortedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.set;

import java.util.Comparator;
import org.cactoos.Text;
import org.cactoos.list.ListOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Matcher;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test case for {@link Sorted}.
* @since 1.0.0
* @checkstyle MagicNumber (500 line)
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class SortedTest {

@Test
public void mustSortIntegerArrayAsSetInAscendingOrder() {
new Assertion<>(
"Must keep unique integer numbers sorted",
new Sorted<>(
Integer::compareTo,
2, 1, 3, 2, 1
),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(1),
new IsEqual<>(2),
new IsEqual<>(3)
)
)
).affirm();
}

@Test
public void mustSortIntegerIterableAsSetInDescendingOrder() {
new Assertion<>(
"Must keep unique integer numbers sorted in descending order",
new Sorted<>(
Comparator.reverseOrder(),
2, 1, 3, 2, 1
),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(3),
new IsEqual<>(2),
new IsEqual<>(1)
)
)
).affirm();
}

@Test
public void mustSortTextIterableAsSetUsingCustomCOmparator() {
new Assertion<>(
"Must keep unique integer numbers sorted in descending order",
new Sorted<>(
(first, second) -> {
final String left = new UncheckedText(first).asString();
final String right = new UncheckedText(second).asString();
return left.compareTo(right);
},
new TextOf("cd"),
new TextOf("ab"),
new TextOf("gh"),
new TextOf("ef")
),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Text>>(
new IsEqual<>(new TextOf("ab")),
new IsEqual<>(new TextOf("cd")),
new IsEqual<>(new TextOf("ef")),
new IsEqual<>(new TextOf("gh"))
)
)
).affirm();
}

@Test
public void mustNotBeEqualToSortedSet() {
new Assertion<>(
"Sorted set must not be equal to the tested collection",
new Sorted<>(
Integer::compareTo,
2, 1, 3, 2, 1
),
new IsNot<>(
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(1),
new IsEqual<>(3),
new IsEqual<>(2)
)
))
).affirm();
}
}