Skip to content

Commit

Permalink
(yegor256#1533) More variance on some generics
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Jan 16, 2021
1 parent 865f249 commit 1fed966
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/scalar/Flattened.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Flattened<X> extends ScalarEnvelope<X> {
* Ctor.
* @param sclr The func
*/
public Flattened(final Scalar<Scalar<X>> sclr) {
public Flattened(final Scalar<? extends Scalar<? extends X>> sclr) {
super(() -> sclr.value().value());
}
}
26 changes: 13 additions & 13 deletions src/main/java/org/cactoos/scalar/Folded.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class Folded<X, T> implements Scalar<X> {
/**
* Original iterable.
*/
private final Iterable<T> iterable;
private final Iterable<? extends T> iterable;

/**
* Input.
Expand All @@ -49,31 +49,31 @@ public final class Folded<X, T> implements Scalar<X> {
/**
* Func.
*/
private final BiFunc<X, T, X> func;
private final BiFunc<? super X, ? super T, ? extends X> func;

/**
* Ctor.
* @param ipt Input
* @param fnc Func original
* @param list List of items
* @param list Array of items
*/
public Folded(final X ipt, final BiFunc<X, T, X> fnc,
final Iterable<T> list) {
this.iterable = list;
this.input = ipt;
this.func = fnc;
@SafeVarargs
public Folded(final X ipt, final BiFunc<? super X, ? super T, ? extends X> fnc,
final T... list) {
this(ipt, fnc, new IterableOf<>(list));
}

/**
* Ctor.
* @param ipt Input
* @param fnc Func original
* @param list Array of items
* @param list List of items
*/
@SafeVarargs
public Folded(final X ipt, final BiFunc<X, T, X> fnc,
final T... list) {
this(ipt, fnc, new IterableOf<>(list));
public Folded(final X ipt, final BiFunc<? super X, ? super T, ? extends X> fnc,
final Iterable<? extends T> list) {
this.iterable = list;
this.input = ipt;
this.func = fnc;
}

@Override
Expand Down
40 changes: 16 additions & 24 deletions src/main/java/org/cactoos/scalar/HighestOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@
* @see IoChecked
* @since 0.29
*/
public final class HighestOf<T extends Comparable<T>> implements Scalar<T> {

/**
* Result.
*/
private final Scalar<T> result;
public final class HighestOf<T extends Comparable<? super T>> extends ScalarEnvelope<T> {

/**
* Ctor.
Expand All @@ -82,31 +77,28 @@ public HighestOf(final T... items) {
* @param scalars The scalars
*/
@SafeVarargs
public HighestOf(final Scalar<T>... scalars) {
public HighestOf(final Scalar<? extends T>... scalars) {
this(new IterableOf<>(scalars));
}

/**
* Ctor.
* @param iterable The items
*/
public HighestOf(final Iterable<Scalar<T>> iterable) {
this.result = new Reduced<>(
(first, second) -> {
final T value;
if (first.compareTo(second) > 0) {
value = first;
} else {
value = second;
}
return value;
},
iterable
public HighestOf(final Iterable<? extends Scalar<? extends T>> iterable) {
super(
new Reduced<>(
(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();
}
}
41 changes: 16 additions & 25 deletions src/main/java/org/cactoos/scalar/LowestOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@
* @see IoChecked
* @since 0.29
*/
public final class LowestOf<T extends Comparable<T>> implements Scalar<T> {

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

public final class LowestOf<T extends Comparable<? super T>> extends ScalarEnvelope<T> {
/**
* Ctor.
* @param items The comparable items
Expand All @@ -82,31 +76,28 @@ public LowestOf(final T... items) {
* @param scalars The scalars
*/
@SafeVarargs
public LowestOf(final Scalar<T>... scalars) {
public LowestOf(final Scalar<? extends T>... scalars) {
this(new IterableOf<>(scalars));
}

/**
* Ctor.
* @param iterable The items
*/
public LowestOf(final Iterable<Scalar<T>> iterable) {
this.result = new Reduced<>(
(first, second) -> {
final T value;
if (first.compareTo(second) < 0) {
value = first;
} else {
value = second;
}
return value;
},
iterable
public LowestOf(final Iterable<? extends Scalar<? extends T>> iterable) {
super(
new Reduced<>(
(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();
}
}
26 changes: 13 additions & 13 deletions src/main/java/org/cactoos/scalar/Reduced.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,42 +77,42 @@ public final class Reduced<T> implements Scalar<T> {
/**
* Items.
*/
private final Iterable<Scalar<T>> items;
private final Iterable<? extends Scalar<? extends T>> items;

/**
* Folding function.
*/
private final BiFunc<T, T, T> function;
private final BiFunc<? super T, ? super T, ? extends T> function;

/**
* Ctor.
* @param reduce Reducing function
* @param scalars The scalars
* @param values Values to be wrapped as scalars
*/
@SafeVarargs
public Reduced(
final BiFunc<T, T, T> reduce,
final Iterable<Scalar<T>> scalars
final BiFunc<? super T, ? super T, ? extends T> reduce,
final T... values
) {
this.items = scalars;
this.function = reduce;
this(reduce, new Mapped<>(Constant::new, values));
}

/**
* Ctor.
* @param reduce Reducing function
* @param values Values to be wrapped as scalars
* @param scalars The scalars
*/
@SafeVarargs
public Reduced(
final BiFunc<T, T, T> reduce,
final T... values
final BiFunc<? super T, ? super T, ? extends T> reduce,
final Iterable<? extends Scalar<? extends T>> scalars
) {
this(reduce, new Mapped<>(Constant::new, values));
this.items = scalars;
this.function = reduce;
}

@Override
public T value() throws Exception {
final Iterator<Scalar<T>> iter = this.items.iterator();
final Iterator<? extends Scalar<? extends T>> iter = this.items.iterator();
if (!iter.hasNext()) {
throw new NoSuchElementException(
"Can't find first element in an empty iterable"
Expand Down

0 comments on commit 1fed966

Please sign in to comment.