Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 20, 2018
2 parents cc17485 + 7299f8b commit 1b881f8
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/main/java/org/cactoos/scalar/Folded.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@
* ).value() // returns 10L
* </pre>
*
* <p>Here is how you can use it to
* find one of items according to the specified {@link BiFunc}:</p>
*
* <pre>
* final String apple = new Folded&lt;&gt;(
* (first, last) -&gt; first,
* new IterableOf&lt;Scalar&lt;String&gt;&gt;(
* () -&gt; "Apple",
* () -&gt; "Banana",
* () -&gt; "Orange"
* )
* ).value();
* final String orange = new Folded&lt;&gt;(
* (first, last) -&gt; last,
* new IterableOf&lt;Scalar&lt;String&gt;&gt;(
* () -&gt; "Apple",
* () -&gt; "Banana",
* () -&gt; "Orange"
* )
* ).value();
* </pre>
*
* <p>There is no thread-safety guarantee.
*
* <p>This class implements {@link Scalar}, which throws a checked
Expand All @@ -49,7 +71,7 @@
* @author Eduard Balovnev (bedward70@mail.ru)
* @version $Id$
* @param <T> Scalar type
* @since 0.9
* @since 0.29
*/
public final class Folded<T> implements Scalar<T> {

Expand Down
114 changes: 114 additions & 0 deletions src/main/java/org/cactoos/scalar/LowestOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.scalar;

import org.cactoos.Scalar;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;

/**
* Find the lowest item.
*
* <p>Here is how you can use it to
* find lowest of {@link Comparable} items:</p>
*
* <pre>
* final String lowest = new LowestOf&lt;String&gt;(
* () -&gt; "Banana", () -&gt; "Apple", () -&gt; "Orange"
* ).value();
* // -&gt; lowest == "Apple"
*
* final Character lowestChar = new LowestOf&lt;&gt;('B', 'U', 'G').value();
* // -&gt; lowestChar == 'B'
* </pre>
*
* <p>This class implements {@link Scalar}, which throws a checked
* {@link Exception}. This may not be convenient in many cases. To make
* it more convenient and get rid of the checked exception you can
* use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @author Eduard Balovnev (bedward70@mail.ru)
* @version $Id$
* @param <T> Scalar type
* @see UncheckedScalar
* @see IoCheckedScalar
* @since 0.29
*/
public final class LowestOf<T extends Comparable<T>> implements Scalar<T> {

/**
* Result.
*/
private final Scalar<T> result;

/**
* Ctor.
* @param items The comparable items
*/
@SafeVarargs
public LowestOf(final T... items) {
this(
new Mapped<>(
item -> () -> item,
items
)
);
}

/**
* Ctor.
* @param scalars The scalars
*/
@SafeVarargs
public LowestOf(final Scalar<T>... scalars) {
this(new IterableOf<>(scalars));
}

/**
* Ctor.
* @param iterable The items
*/
public LowestOf(final Iterable<Scalar<T>> iterable) {
this.result = new Folded<>(
(first, second) -> {
final T value;
if (first.compareTo(second) < 0) {
value = first;
} else {
value = second;
}
return value;
},
iterable
);
}

@Override
public T value() throws Exception {
return this.result.value();
}
}
5 changes: 2 additions & 3 deletions src/test/java/org/cactoos/scalar/FoldedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @author Eduard Balovnev (bedward70@mail.ru)
* @version $Id$
* @since 1.0
* @since 0.29
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class FoldedTest {
Expand All @@ -46,8 +46,7 @@ public void failsForEmptyIterable() throws Exception {
new Folded<>(
(first, last) -> first,
Collections.emptyList()
)
.value();
).value();
}

@Test
Expand Down
Loading

0 comments on commit 1b881f8

Please sign in to comment.