-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
295 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
); | ||
} | ||
|
||
} |