Skip to content

Commit

Permalink
(#1513) Cleanup types and simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Dec 24, 2020
1 parent f065edd commit 1fb4a36
Show file tree
Hide file tree
Showing 23 changed files with 142 additions and 188 deletions.
10 changes: 0 additions & 10 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.iterable;

import java.util.Iterator;
import java.util.List;
import org.cactoos.Scalar;
import org.cactoos.func.FallbackFrom;
import org.cactoos.iterator.IteratorOf;
Expand All @@ -46,7 +45,6 @@
* @since 0.12
* @checkstyle ClassDataAbstractionCouplingCheck (550 lines)
*/
@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization")
public final class IterableOf<X> implements Iterable<X> {

/**
Expand All @@ -63,14 +61,6 @@ public IterableOf(final X... items) {
this(() -> new IteratorOf<>(items));
}

/**
* Ctor.
* @param list The list
*/
public IterableOf(final List<? extends X> list) {
this(list::iterator);
}

/**
* Ctor.
* @param list The list
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/org/cactoos/iterable/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,15 @@ public Joined(final Iterable<? extends T>... items) {
this(new IterableOf<>(items));
}

/**
* Ctor.
* @param items Items to concatenate
* @since 0.21
*/
public Joined(final Iterator<Iterable<? extends T>> items) {
this(new IterableOf<>(items));
}

/**
* Ctor.
* @param item First item
* @param items Iterable
* @since 0.32
*/
@SuppressWarnings("unchecked")
public Joined(final T item, final Iterable<T> items) {
super(new Joined<>(new IterableOf<>(item), items));
public Joined(final T item, final Iterable<? extends T> items) {
this(new IterableOf<>(new IterableOf<>(item), items));
}

/**
Expand All @@ -72,10 +63,9 @@ public Joined(final Iterable<Iterable<? extends T>> items) {
super(
new IterableOf<>(
() -> new org.cactoos.iterator.Joined<>(
new Mapped<>(Iterable::iterator, items)
new Mapped<Iterator<? extends T>>(Iterable::iterator, items)
)
)
);
}

}
17 changes: 5 additions & 12 deletions src/main/java/org/cactoos/iterable/Sticky.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
package org.cactoos.iterable;

import java.util.Collection;
import java.util.LinkedList;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.Mapped;

/**
Expand All @@ -50,19 +49,13 @@ public Sticky(final X... src) {
* Ctor.
* @param iterable The iterable
*/
public Sticky(final Iterable<X> iterable) {
public Sticky(final Iterable<? extends X> iterable) {
super(
new IterableOf<>(
new IterableOf<X>(
new Mapped<>(
Iterable::iterator,
new org.cactoos.scalar.Sticky<Iterable<X>>(
() -> {
final Collection<X> temp = new LinkedList<>();
for (final X item : iterable) {
temp.add(item);
}
return temp;
}
new org.cactoos.scalar.Sticky<>(
() -> new ListOf<>(iterable)
)
)
)
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/org/cactoos/iterator/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.Collections;
import java.util.Iterator;
import org.cactoos.list.ListOf;
import org.cactoos.iterable.IterableOf;

/**
* A few Iterators joined together.
Expand All @@ -40,27 +40,38 @@ public final class Joined<T> implements Iterator<T> {
/**
* Iterators.
*/
private final Iterator<Iterator<T>> iters;
private final Iterator<Iterator<? extends T>> iters;

/**
* Current traversal iterator.
*/
private Iterator<T> current;
private Iterator<? extends T> current;

/**
* Ctor.
* @param items Items to concatenate
*/
@SafeVarargs
public Joined(final Iterator<T>... items) {
this(new ListOf<>(items));
public Joined(final Iterator<? extends T>... items) {
this(new IterableOf<>(items));
}

/**
* Ctor.
* @param item First item
* @param items Iterable
* @since 0.49
*/
@SuppressWarnings("unchecked")
public Joined(final T item, final Iterator<? extends T> items) {
this(new IterableOf<>(new IteratorOf<>(item), items));
}

/**
* Ctor.
* @param items Items to concatenate
*/
public Joined(final Iterable<Iterator<T>> items) {
public Joined(final Iterable<Iterator<? extends T>> items) {
this.iters = items.iterator();
this.current = Collections.emptyIterator();
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/cactoos/map/MapOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,11 @@ public <Z> MapOf(
* @param list List of the entries
* @since 0.12
*/
@SuppressWarnings("unchecked")
public MapOf(
final Map<? extends X, ? extends Y> src,
final Iterable<Map.Entry<? extends X, ? extends Y>> list
) {
this(
new Joined<>(
src.entrySet(), list
)
);
this(new Joined<Map.Entry<? extends X, ? extends Y>>(src.entrySet(), list));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/scalar/MultiplicationOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public MultiplicationOf(final Iterable<? extends Number> src) {
return new Folded<>(
BigDecimal.ONE,
(mtn, value) -> mtn.multiply(value, MathContext.DECIMAL128),
new Mapped<>(
new Mapped<BigDecimal>(
number -> BigDecimal.valueOf(number.doubleValue()),
src
)
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/cactoos/scalar/PropertiesOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ public PropertiesOf(final Map.Entry<?, ?>... entries) {
public PropertiesOf(final Iterable<Map.Entry<?, ?>> entries) {
this(
new MapOf<>(
new Mapped<>(
input -> new MapEntry<>(
input.getKey().toString(), input.getValue().toString()
),
entries
)
input -> new MapEntry<>(
input.getKey().toString(), input.getValue().toString()
),
entries
)
);
}
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/org/cactoos/scalar/SumOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
*
* @since 0.9
*/
@SuppressWarnings(
{
"PMD.CallSuperInConstructor",
"PMD.OnlyOneConstructorShouldDoInitialization",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
}
)
public final class SumOf extends NumberEnvelope {

/**
Expand Down Expand Up @@ -108,7 +101,7 @@ public SumOf(final Iterable<? extends Number> src) {
super(() -> new Folded<>(
BigDecimal.ZERO,
(sum, value) -> sum.add(value, MathContext.DECIMAL128),
new Mapped<>(
new Mapped<BigDecimal>(
number -> BigDecimal.valueOf(number.doubleValue()),
src
)).value().doubleValue()
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/org/cactoos/scalar/SumOfDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,14 @@
*
* @since 0.30
*/
public final class SumOfDouble implements Scalar<Double> {

/**
* Varargs of Scalar to sum up values from.
*/
private final Scalar<Double>[] scalars;

public final class SumOfDouble extends ScalarEnvelope<Double> {
/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*/
@SafeVarargs
public SumOfDouble(final Scalar<Double>... src) {
this.scalars = src;
}

@Override
public Double value() {
return new SumOfScalar(this.scalars).value().doubleValue();
super(new Mapped<>(Number::doubleValue, new SumOfScalar(src)));
}
}
15 changes: 2 additions & 13 deletions src/main/java/org/cactoos/scalar/SumOfFloat.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,14 @@
*
* @since 0.30
*/
public final class SumOfFloat implements Scalar<Float> {

/**
* Varargs of Scalar to sum up values from.
*/
private final Scalar<Float>[] scalars;

public final class SumOfFloat extends ScalarEnvelope<Float> {
/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*/
@SafeVarargs
public SumOfFloat(final Scalar<Float>... src) {
this.scalars = src;
}

@Override
public Float value() {
return new SumOfScalar(this.scalars).value().floatValue();
super(new Mapped<>(Number::floatValue, new SumOfScalar(src)));
}
}
15 changes: 2 additions & 13 deletions src/main/java/org/cactoos/scalar/SumOfInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,14 @@
*
* @since 0.30
*/
public final class SumOfInt implements Scalar<Integer> {

/**
* Varargs of Scalar to sum up values from.
*/
private final Scalar<Integer>[] scalars;

public final class SumOfInt extends ScalarEnvelope<Integer> {
/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*/
@SafeVarargs
public SumOfInt(final Scalar<Integer>... src) {
this.scalars = src;
}

@Override
public Integer value() {
return new SumOfScalar(this.scalars).value().intValue();
super(new Mapped<>(Number::intValue, new SumOfScalar(src)));
}
}
15 changes: 2 additions & 13 deletions src/main/java/org/cactoos/scalar/SumOfLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,14 @@
*
* @since 0.30
*/
public final class SumOfLong implements Scalar<Long> {

/**
* Varargs of Scalar to sum up values from.
*/
private final Scalar<Long>[] scalars;

public final class SumOfLong extends ScalarEnvelope<Long> {
/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*/
@SafeVarargs
public SumOfLong(final Scalar<Long>... src) {
this.scalars = src;
}

@Override
public Long value() {
return new SumOfScalar(this.scalars).value().longValue();
super(new Mapped<>(Number::longValue, new SumOfScalar(src)));
}
}
27 changes: 3 additions & 24 deletions src/main/java/org/cactoos/scalar/SumOfScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
*/
package org.cactoos.scalar;

import java.util.stream.Collectors;
import org.cactoos.Scalar;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
import org.cactoos.iterable.Mapped;

/**
* Make a scalar which is sum of scalar's values.
Expand All @@ -40,33 +38,14 @@
*
* @since 0.30
*/
final class SumOfScalar implements Scalar<SumOf> {

/**
* Varargs of Scalar to sum up values from.
*/
private final Scalar<? extends Number>[] scalars;

final class SumOfScalar extends ScalarEnvelope<Number> {
/**
* Ctor.
* @param src Varargs of Scalar to sum up values from
* @since 0.30
*/
@SafeVarargs
SumOfScalar(final Scalar<? extends Number>... src) {
this.scalars = src;
}

@Override
public SumOf value() {
return new SumOf(
new IterableOf<>(
new ListOf<>(this.scalars)
.stream()
.map(
scalar -> new Unchecked<>(scalar).value()
).collect(Collectors.toList())
)
);
super(() -> new SumOf(new Mapped<>(Scalar::value, src)));
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/cactoos/func/ChainedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void withoutIterable() throws Exception {
new Mapped<>(
new Chained<>(
input -> input.concat("1"),
input -> input.concat("2")
(String input) -> input.concat("2")
),
new IterableOf<>("public", "final", "class")
)
Expand Down
Loading

1 comment on commit 1fb4a36

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 1fb4a36 Dec 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1287-1c7c8289 disappeared from src/main/java/org/cactoos/text/Lowered.java, that's why I closed #1462. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.