Skip to content

Commit

Permalink
#415 Sorted collection
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 28, 2017
1 parent f580d69 commit b0fb379
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 8 deletions.
172 changes: 172 additions & 0 deletions src/main/java/org/cactoos/collection/Sorted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.collection;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.SyncScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Sorted collection.
*
* <p>There is no thread-safety guarantee.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <T> Element type
* @since 0.19
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class Sorted<T extends Comparable<? super T>> implements
Collection<T> {

/**
* Sorted one.
*/
private final UncheckedScalar<Collection<T>> scalar;

/**
* Ctor.
* @param src The underlying collection
*/
@SafeVarargs
public Sorted(final T... src) {
this(new ListOf<>(src));
}

/**
* Ctor.
* @param src The underlying collection
*/
public Sorted(final Iterable<T> src) {
this(Comparator.naturalOrder(), new ListOf<>(src));
}

/**
* Ctor.
* @param src The underlying collection
* @param cmp The comparator
*/
@SafeVarargs
public Sorted(final Comparator<T> cmp, final T... src) {
this(cmp, new ListOf<>(src));
}

/**
* Ctor.
* @param src The underlying collection
* @param cmp The comparator
*/
public Sorted(final Comparator<T> cmp, final Collection<T> src) {
this.scalar = new UncheckedScalar<>(
new SyncScalar<>(
new StickyScalar<>(
() -> {
final List<T> items = new LinkedList<>();
items.addAll(src);
items.sort(cmp);
return items;
}
)
)
);
}

@Override
public String toString() {
return this.scalar.value().toString();
}

@Override
public int size() {
return this.scalar.value().size();
}

@Override
public boolean isEmpty() {
return this.scalar.value().isEmpty();
}

@Override
public boolean contains(final Object item) {
return this.scalar.value().contains(item);
}

@Override
public Iterator<T> iterator() {
return this.scalar.value().iterator();
}

@Override
public Object[] toArray() {
return this.scalar.value().toArray();
}

@Override
@SuppressWarnings("PMD.UseVarargs")
public <X> X[] toArray(final X[] array) {
return this.scalar.value().toArray(array);
}

@Override
public boolean add(final T item) {
throw new UnsupportedOperationException("#add()");
}

@Override
public boolean remove(final Object item) {
throw new UnsupportedOperationException("#remove()");
}

@Override
public boolean containsAll(final Collection<?> list) {
return this.scalar.value().containsAll(list);
}

@Override
public boolean addAll(final Collection<? extends T> list) {
throw new UnsupportedOperationException("#addAll()");
}

@Override
public boolean removeAll(final Collection<?> list) {
throw new UnsupportedOperationException("#removeAll()");
}

@Override
public boolean retainAll(final Collection<?> list) {
throw new UnsupportedOperationException("#retainAll()");
}

@Override
public void clear() {
throw new UnsupportedOperationException("#clear()");
}
}
19 changes: 11 additions & 8 deletions src/main/java/org/cactoos/iterator/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedList;
import java.util.List;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.SyncScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
Expand Down Expand Up @@ -64,15 +65,17 @@ public Sorted(final Iterator<T> items) {
*/
public Sorted(final Comparator<T> comparator, final Iterator<T> iterator) {
this.scalar = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final List<T> items = new LinkedList<>();
while (iterator.hasNext()) {
items.add(iterator.next());
new SyncScalar<>(
new StickyScalar<>(
() -> {
final List<T> items = new LinkedList<>();
while (iterator.hasNext()) {
items.add(iterator.next());
}
items.sort(comparator);
return items.iterator();
}
items.sort(comparator);
return items.iterator();
}
)
)
);
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/cactoos/collection/SortedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.collection;

import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test Case for {@link Sorted}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.19
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class SortedTest {

@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void sortsCollection() throws Exception {
MatcherAssert.assertThat(
"Can't sort elements in iterator",
new Sorted<>(
new ListOf<>(
"one", "two", "three", "four"
)
),
Matchers.contains(
"four", "one", "three", "two"
)
);
}

}
56 changes: 56 additions & 0 deletions src/test/java/org/cactoos/iterator/SortedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.iterator;

import org.cactoos.iterable.IterableOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test Case for {@link Sorted}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.19
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class SortedTest {

@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void sortsIterable() throws Exception {
MatcherAssert.assertThat(
"Can't sort elements in iterator",
() -> new Sorted<>(
new IterableOf<>(
"one", "two", "three", "four"
).iterator()
),
Matchers.contains(
"four", "one", "three", "two"
)
);
}

}

0 comments on commit b0fb379

Please sign in to comment.