-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Operator: Generate #509
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
Closed
Closed
Operator: Generate #509
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
rxjava-core/src/main/java/rx/operators/OperationGenerate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package rx.operators; | ||
|
||
import java.util.Date; | ||
import java.util.concurrent.TimeUnit; | ||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Scheduler; | ||
import rx.Subscription; | ||
import rx.subscriptions.Subscriptions; | ||
import rx.util.TimeSpan; | ||
import rx.util.functions.Func1; | ||
import rx.util.functions.Func2; | ||
|
||
/** | ||
* Generates an observable sequence by iterating a state from an initial state | ||
* until the condition returns false. | ||
* <p> | ||
* Behaves like a generalized for loop. | ||
*/ | ||
public final class OperationGenerate { | ||
/** | ||
* Generates an observable sequence by iterating a state from an initial | ||
* state until the condition returns false. | ||
*/ | ||
public static <TState, R> OnSubscribeFunc<R> generate( | ||
final TState initialState, | ||
final Func1<TState, Boolean> condition, | ||
final Func1<TState, TState> iterate, | ||
final Func1<TState, R> resultSelector, | ||
final Scheduler scheduler) { | ||
return new OnSubscribeFunc<R>() { | ||
@Override | ||
public Subscription onSubscribe(final Observer<? super R> observer) { | ||
return scheduler.schedule(initialState, new Func2<Scheduler, TState, Subscription>() { | ||
@Override | ||
public Subscription call(Scheduler s, TState state) { | ||
boolean hasNext; | ||
try { | ||
hasNext = condition.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
if (hasNext) { | ||
R result; | ||
try { | ||
result = resultSelector.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
observer.onNext(result); | ||
|
||
TState nextState; | ||
try { | ||
nextState = iterate.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
return s.schedule(nextState, this); | ||
} | ||
observer.onCompleted(); | ||
return Subscriptions.empty(); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
/** | ||
* Generates an observable sequence by iterating a state, in relative timed fashion, | ||
* from an initial state until the condition fails. | ||
*/ | ||
public static <TState, R> OnSubscribeFunc<R> generate( | ||
final TState initialState, | ||
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. Bad indent. |
||
final Func1<TState, Boolean> condition, | ||
final Func1<TState, TState> iterate, | ||
final Func1<TState, R> resultSelector, | ||
final Func1<TState, TimeSpan> timeSelector, | ||
final Scheduler scheduler) { | ||
return new OnSubscribeFunc<R>() { | ||
@Override | ||
public Subscription onSubscribe(final Observer<? super R> observer) { | ||
TimeSpan first; | ||
try { | ||
first = timeSelector.call(initialState); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
return scheduler.schedule(initialState, | ||
new Func2<Scheduler, TState, Subscription>() { | ||
@Override | ||
public Subscription call(Scheduler s, TState state) { | ||
boolean hasNext; | ||
try { | ||
hasNext = condition.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
if (hasNext) { | ||
R result; | ||
try { | ||
result = resultSelector.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
observer.onNext(result); | ||
|
||
TState nextState; | ||
try { | ||
nextState = iterate.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
TimeSpan nextDate; | ||
try { | ||
nextDate = timeSelector.call(initialState); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
return s.schedule(nextState, this, nextDate.value(), nextDate.unit()); | ||
} | ||
observer.onCompleted(); | ||
return Subscriptions.empty(); | ||
} | ||
}, first.value(), first.unit()); | ||
} | ||
}; | ||
} | ||
/** | ||
* Generates an observable sequence by iterating a state, in absolute timed fashion, | ||
* from an initial state until the condition fails. | ||
*/ | ||
public static <TState, R> OnSubscribeFunc<R> generateAbsoluteTime( | ||
final TState initialState, | ||
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. Bad indent. |
||
final Func1<TState, Boolean> condition, | ||
final Func1<TState, TState> iterate, | ||
final Func1<TState, R> resultSelector, | ||
final Func1<TState, Date> timeSelector, | ||
final Scheduler scheduler) { | ||
return new OnSubscribeFunc<R>() { | ||
@Override | ||
public Subscription onSubscribe(final Observer<? super R> observer) { | ||
Date first; | ||
try { | ||
first = timeSelector.call(initialState); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
long delta = Math.max(0, first.getTime() - scheduler.now()); | ||
|
||
return scheduler.schedule(initialState, | ||
new Func2<Scheduler, TState, Subscription>() { | ||
@Override | ||
public Subscription call(Scheduler s, TState state) { | ||
boolean hasNext; | ||
try { | ||
hasNext = condition.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
if (hasNext) { | ||
R result; | ||
try { | ||
result = resultSelector.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
observer.onNext(result); | ||
|
||
TState nextState; | ||
try { | ||
nextState = iterate.call(state); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
Date nextDate; | ||
try { | ||
nextDate = timeSelector.call(initialState); | ||
} catch (Throwable t) { | ||
observer.onError(t); | ||
return Subscriptions.empty(); | ||
} | ||
|
||
long deltaNext = Math.max(0, nextDate.getTime() - s.now()); | ||
return s.schedule(nextState, this, deltaNext, TimeUnit.MILLISECONDS); | ||
} | ||
observer.onCompleted(); | ||
return Subscriptions.empty(); | ||
} | ||
}, delta, TimeUnit.MILLISECONDS); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it OK that creating a special timeSelector which always returns 0 to remove these duplicated codes?