-
Notifications
You must be signed in to change notification settings - Fork 165
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
#1445: Reorganise classes around Scalar and Callable #1494
Changes from 4 commits
091aefe
b99a3d4
0b637b6
af197d8
881364b
31971e9
d764d0b
96f391b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,22 +23,20 @@ | |
*/ | ||
package org.cactoos.scalar; | ||
|
||
import java.util.concurrent.Callable; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.func.FuncOf; | ||
import org.cactoos.func.Repeated; | ||
|
||
/** | ||
* Callable that runs repeatedly for a number of times. | ||
* Scalar that runs repeatedly for a number of times. | ||
* | ||
* @param <X> Type of output | ||
* @param <T> Type of output | ||
* @since 0.49.2 | ||
*/ | ||
public final class RepeatedCallable<X> implements Callable<X> { | ||
|
||
public final class Repeated<T> implements Scalar<T> { | ||
/** | ||
* Callable. | ||
* Scalar. | ||
*/ | ||
private final Callable<X> callable; | ||
private final Scalar<T> scalar; | ||
|
||
/** | ||
* Times to repeat. | ||
|
@@ -48,23 +46,23 @@ public final class RepeatedCallable<X> implements Callable<X> { | |
/** | ||
* Ctor. | ||
* | ||
* <p>If {@code max} is equal or less than zero {@link #call()} will return | ||
* <p>If {@code max} is equal or less than zero {@link #value()} will | ||
* return | ||
* an exception.</p> | ||
* | ||
* @param cllbl Callable to repeat. | ||
* @param sclr Scalar to repeat. | ||
* @param count How many times. | ||
*/ | ||
public RepeatedCallable(final Callable<X> cllbl, final int count) { | ||
this.callable = cllbl; | ||
public Repeated(final Scalar<T> sclr, final int count) { | ||
this.scalar = sclr; | ||
this.times = count; | ||
} | ||
|
||
@Override | ||
public X call() throws Exception { | ||
return new Repeated<>( | ||
new FuncOf<>(this.callable), | ||
public T value() throws Exception { | ||
return new org.cactoos.func.Repeated<>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabriciofx Can this be moved to the primary ctor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreoss done! |
||
new FuncOf<>(this.scalar), | ||
this.times | ||
).apply(true); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,32 +23,61 @@ | |
*/ | ||
package org.cactoos.scalar; | ||
|
||
import org.cactoos.Func; | ||
import org.cactoos.Proc; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.func.FuncOf; | ||
|
||
/** | ||
* ScalarOf. | ||
* | ||
* @param <X> Element type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabriciofx This type parameter should be on ctor level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreoss Can you explain it better? Shoul I add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabriciofx Exactly, it should be like that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreoss done, and thanks for the help! :) |
||
* @param <T> Element type | ||
* @since 0.4 | ||
*/ | ||
public final class ScalarOf<T> implements Scalar<T> { | ||
public final class ScalarOf<X, T> extends ScalarEnvelope<T> { | ||
/** | ||
* Ctor. | ||
* @param runnable Encapsulated proc | ||
* @param result Result to return | ||
* @since 0.32 | ||
*/ | ||
public ScalarOf(final Runnable runnable, final T result) { | ||
this( | ||
() -> { | ||
runnable.run(); | ||
return result; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* The scalar. | ||
* Ctor. | ||
* @param proc Encapsulated proc | ||
* @param ipt Input | ||
* @param result Result to return | ||
* @since 0.41 | ||
*/ | ||
private final Scalar<T> origin; | ||
public ScalarOf(final Proc<X> proc, final X ipt, final T result) { | ||
this(new FuncOf<>(proc, result), ipt); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param origin The scalar | ||
* @param fnc Encapsulated func | ||
* @param ipt Input | ||
* @since 0.41 | ||
*/ | ||
public ScalarOf(final Scalar<T> origin) { | ||
this.origin = origin; | ||
public ScalarOf(final Func<X, T> fnc, final X ipt) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabriciofx There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreoss done! |
||
this(() -> fnc.apply(ipt)); | ||
} | ||
|
||
@Override | ||
public T value() throws Exception { | ||
return this.origin.value(); | ||
/** | ||
* Ctor. | ||
* | ||
* @param scalar The scalar | ||
*/ | ||
public ScalarOf(final Scalar<T> scalar) { | ||
super(scalar); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabriciofx Not realated to the review. But what is the use case for such class?
It seems that it applies some side effects, which is bad for
Scalar
to haveFor example:
new Repeated<>(new Count<>(1), 5)).value() == 6 (all other values lost)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andreoss Sorry, but I didn't get the point. I did some code here to test and works fine (but I can't make
Count<T>
because, I can't increment something likeT
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabriciofx I mean, that with
Repeat
only the last result is returned, and all of other iteratatios are lost. Which implies mutable state.So
Repeated
may be seen as an encouragement to put mutable state in aScalar
, as in yourCount
example.