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

(#1335) Introduce NumberEnvelope and move number in their package #1587

Merged
merged 3 commits into from
Jun 12, 2021
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ The MIT License (MIT)
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/Fallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.MinOf;
import org.cactoos.scalar.InheritanceLevel;
import org.cactoos.scalar.MinOf;

/**
* Fallback from a {@link Throwable}.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/TailOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.cactoos.Input;
import org.cactoos.scalar.MinOf;
import org.cactoos.number.MinOf;
import org.cactoos.text.FormattedText;

/**
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.Joined;
import org.cactoos.text.UncheckedText;
Expand Down Expand Up @@ -123,10 +122,7 @@ public int hashCode() {
return new Unchecked<>(
new Folded<>(
42,
(hash, entry) -> new SumOfInt(
() -> 37 * hash,
entry::hashCode
).value(),
(hash, entry) -> 37 * hash + entry.hashCode(),
this
)
).value();
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/org/cactoos/number/AvgOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.number;

import java.math.BigDecimal;
import java.util.Iterator;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

/**
* Average of numbers.
*
* <p>Here is how you can use it to find the mathematical average of numbers:
* </p>
*
* <pre>
* int avg = new AvgOf(1, 2, 3, 4).intValue();
* long avg = new AvgOf(1L, 2L, 3L).longValue();
* int avg = new AvgOf(numbers).intValue();
* </pre>
*
* <p>There is no thread-safety guarantee.
*
* @since 1.0.0
*/
public final class AvgOf extends NumberEnvelope {

/**
* Serialization marker.
*/
private static final long serialVersionUID = -5952222476772718552L;

/**
* Ctor.
* @param src Numbers
*/
public AvgOf(final Number... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
*/
public AvgOf(final Iterable<? extends Number> src) {
super(
new NumberOfScalars(
new Ternary<>(
new ScalarOf<>(src::iterator),
(Iterator<? extends Number> it) -> it.hasNext(),
it -> {
BigDecimal total = BigDecimal.ZERO;
long qty = 0;
for (final Number value: new IterableOf<>(it)) {
qty = qty + 1;
total = total.add(
new BigDecimal(value.toString())
);
}
return total.divide(BigDecimal.valueOf(qty));
},
it -> BigDecimal.ZERO
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,35 @@
* 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.Mapped;
package org.cactoos.number;

/**
* Make a scalar which is sum of scalar's values.
*
* <p>This class implements {@link Scalar}, which throws a checked
* {@link Exception}. Despite that this class does NOT throw a checked
* exception.</p>
* {@link Number} as {@link Comparable}.
*
* <p>There is no thread-safety guarantee.
* <p>Note this class is for internal usage only
* <p>
* There is no thread-safety guarantee.
*
* @since 0.30
* @since 1.0.0
*/
final class SumOfScalar extends ScalarEnvelope<Number> {
public final class ComparableNumber extends NumberEnvelope
implements Comparable<Number> {

/**
* Serialization marker.
*/
private static final long serialVersionUID = -2598821437507165938L;

/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*
* @param nbr Number
*/
@SafeVarargs
SumOfScalar(final Scalar<? extends Number>... src) {
super(() -> new SumOf(new Mapped<>(Scalar::value, src)));
public ComparableNumber(final Number nbr) {
super(nbr);
}

@Override
public int compareTo(final Number nbr) {
return Double.compare(this.doubleValue(), nbr.doubleValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,52 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.scalar;
package org.cactoos.number;

import java.math.BigDecimal;
import java.math.MathContext;
import org.cactoos.Scalar;

/**
* Division result of two numbers.
*
* <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 the {@link Unchecked} decorator. Or you may use
* {@link IoChecked} to wrap it in an IOException.</p>
*
* <p>There is no thread-safety guarantee.
*
* @since 0.49.2
* @since 1.0.0
*/
public final class DivisionOf extends NumberEnvelope {

private static final long serialVersionUID = -5276601257067346442L;
/**
* Serialization marker.
*/
private static final long serialVersionUID = -7835189568890281261L;

/**
* Ctor.
* @param dividend The dividend
* @param divisor The divisor
*/
public DivisionOf(final BigDecimal dividend, final BigDecimal divisor) {
this(() -> dividend.divide(divisor));
}

/**
* Ctor.
* @param dividend The dividend
* @param divisor The divisor
*/
public DivisionOf(final Number dividend, final Number divisor) {
super(() -> BigDecimal.valueOf(dividend.doubleValue()).divide(
BigDecimal.valueOf(divisor.doubleValue()),
MathContext.DECIMAL128
).doubleValue()
this(
() -> new BigDecimal(dividend.toString()).divide(
new BigDecimal(divisor.toString())
)
);
}

/**
* Ctor.
* @param result The result
*/
private DivisionOf(final Scalar<BigDecimal> result) {
super(new NumberOfScalars(result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.scalar;
package org.cactoos.number;

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

/**
* Find the greater among items.
Expand All @@ -37,87 +37,51 @@
* <pre>
* int max = new MaxOf(1, 2, 3, 4).intValue();
* long max = new MaxOf(1L, 2L, 3L).longValue();
* int max = new MaxOf(numbers.toArray(new Integer[numbers.size()])).intValue();
* int max = new MaxOf(numbers).intValue();
* </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 the
* {@link Unchecked} decorator. Or you may use {@link IoChecked} to wrap it in
* an IOException.
* </p>
* <p>There is no thread-safety guarantee.
*
* <p>
* There is no thread-safety guarantee.
* @see Unchecked
* @see IoChecked
* @since 0.24
* @since 1.0.0
*/
public final class MaxOf extends NumberEnvelope {

/**
* Serialization marker.
*/
private static final long serialVersionUID = -6057839494957475355L;

/**
* Ctor.
* @param src Numbers
*/
public MaxOf(final Integer... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src Numbers
*/
public MaxOf(final Long... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src Numbers
*/
public MaxOf(final Double... src) {
this(new IterableOf<>(src));
}
private static final long serialVersionUID = 8337955195592696602L;

/**
* Ctor.
* @param src Numbers
*/
public MaxOf(final Float... src) {
public MaxOf(final Number... src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
*/
public MaxOf(final Iterable<Number> src) {
public MaxOf(final Iterable<? extends Number> src) {
super(
new Folded<>(
Long.MIN_VALUE,
Math::max,
new Mapped<>(Number::longValue, src)
),
new Folded<>(
Integer.MIN_VALUE,
Math::max,
new Mapped<>(Number::intValue, src)
),
new Folded<>(
-Float.MAX_VALUE,
Math::max,
new Mapped<>(Number::floatValue, src)
),
new Folded<>(
-Double.MAX_VALUE,
Math::max,
new Mapped<>(Number::doubleValue, src)
new NumberOfScalars(
new Reduced<Long>(
Math::max,
new Mapped<>((Number n) -> n::longValue, src)
),
new Reduced<Integer>(
Math::max,
new Mapped<>((Number n) -> n::intValue, src)
),
new Reduced<Float>(
Math::max,
new Mapped<>((Number n) -> n::floatValue, src)
),
new Reduced<Double>(
Math::max,
new Mapped<>((Number n) -> n::doubleValue, src)
)
)
);
}
Expand Down
Loading