Skip to content

Commit

Permalink
Revamped Max and Min
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Jun 28, 2017
1 parent 38919dc commit 5b8de67
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,56 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.list;

import java.util.Iterator;
import org.cactoos.Scalar;

/**
* Math.
* Find the greater among items.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.6
* @param <T> Scalar type
* @since 0.9
*/
package org.cactoos.math;
public final class Max<T extends Comparable<T>> implements Scalar<T> {

/**
* Items.
*/
private final Iterable<Scalar<T>> items;

/**
* Ctor.
* @param items The items
*/
@SafeVarargs
public Max(final Scalar<T>... items) {
this(new ArrayAsIterable<>(items));
}

/**
* Ctor.
* @param items The items
*/
public Max(final Iterable<Scalar<T>> items) {
this.items = items;
}

@Override
public T asValue() throws Exception {
final Iterator<Scalar<T>> iter = this.items.iterator();
T max = iter.next().asValue();
while (iter.hasNext()) {
final T next = iter.next().asValue();
if (next.compareTo(max) > 0) {
max = next;
}
}
return max;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,56 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.list;

import java.util.Iterator;
import org.cactoos.Scalar;

/**
* Math, tests.
* Find the smaller among items.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.6
* @param <T> Scalar type
* @since 0.9
*/
package org.cactoos.math;
public final class Min<T extends Comparable<T>> implements Scalar<T> {

/**
* Items.
*/
private final Iterable<Scalar<T>> items;

/**
* Ctor.
* @param items The items
*/
@SafeVarargs
public Min(final Scalar<T>... items) {
this(new ArrayAsIterable<>(items));
}

/**
* Ctor.
* @param items The items
*/
public Min(final Iterable<Scalar<T>> items) {
this.items = items;
}

@Override
public T asValue() throws Exception {
final Iterator<Scalar<T>> iter = this.items.iterator();
T min = iter.next().asValue();
while (iter.hasNext()) {
final T next = iter.next().asValue();
if (next.compareTo(min) < 0) {
min = next;
}
}
return min;
}

}
109 changes: 0 additions & 109 deletions src/main/java/org/cactoos/math/Max.java

This file was deleted.

109 changes: 0 additions & 109 deletions src/main/java/org/cactoos/math/Min.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.math;
package org.cactoos.list;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand All @@ -32,28 +32,33 @@
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.7
* @since 0.9
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class MaxTest {

@Test
public void maxAmongOneTest() throws Exception {
final int num = 10;
MatcherAssert.assertThat(
"Can't find the greater among one number",
// @checkstyle MagicNumber (2 lines)
new Max(10).asValue(),
Matchers.equalTo(10)
"Can't find the greater among one",
new Max<Integer>(() -> new Integer(num)).asValue(),
Matchers.equalTo(num)
);
}

@Test
public void maxAmongManyTest() throws Exception {
final int num = 10;
MatcherAssert.assertThat(
"Can't find the greater among many numbers",
// @checkstyle MagicNumber (2 lines)
new Max(10, 5, 7, 2, 100).asValue(),
Matchers.equalTo(100)
"Can't find the greater among many",
new Max<Integer>(
() -> new Integer(num),
() -> new Integer(0),
() -> new Integer(-1),
() -> new Integer(2)
).asValue(),
Matchers.equalTo(num)
);
}
}
Loading

0 comments on commit 5b8de67

Please sign in to comment.