forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscriptions utility class and rx.subscriptions package
- Loading branch information
1 parent
4e2b666
commit d8d305e
Showing
11 changed files
with
110 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
rxjava-core/src/main/java/rx/subscriptions/BooleanSubscription.java
This file contains 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,26 @@ | ||
package rx.subscriptions; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import rx.Observable; | ||
import rx.Subscription; | ||
|
||
/** | ||
* Subscription that can be checked for status such as in a loop inside an {@link Observable} to exit the loop if unsubscribed. | ||
* | ||
* @see Rx.Net equivalent BooleanDisposable at http://msdn.microsoft.com/en-us/library/system.reactive.disposables.booleandisposable(v=vs.103).aspx | ||
*/ | ||
public class BooleanSubscription implements Subscription { | ||
|
||
private final AtomicBoolean unsubscribed = new AtomicBoolean(false); | ||
|
||
public boolean isUnsubscribed() { | ||
return unsubscribed.get(); | ||
} | ||
|
||
@Override | ||
public void unsubscribe() { | ||
unsubscribed.set(false); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
rxjava-core/src/main/java/rx/subscriptions/Subscriptions.java
This file contains 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,58 @@ | ||
package rx.subscriptions; | ||
|
||
import rx.Subscription; | ||
import rx.util.functions.Action0; | ||
import rx.util.functions.FuncN; | ||
import rx.util.functions.Functions; | ||
|
||
public class Subscriptions { | ||
/** | ||
* A {@link Subscription} that does nothing. | ||
* | ||
* @return {@link Subscription} | ||
*/ | ||
public static Subscription empty() { | ||
return new EmptySubscription(); | ||
} | ||
|
||
/** | ||
* A {@link Subscription} implemented via a Func | ||
* | ||
* @return {@link Subscription} | ||
*/ | ||
public static Subscription createSubscription(final Action0 unsubscribe) { | ||
return new Subscription() { | ||
|
||
@Override | ||
public void unsubscribe() { | ||
unsubscribe.call(); | ||
} | ||
|
||
}; | ||
} | ||
|
||
/** | ||
* A {@link Subscription} implemented via an anonymous function (such as closures from other languages). | ||
* | ||
* @return {@link Subscription} | ||
*/ | ||
public static Subscription createSubscription(final Object unsubscribe) { | ||
final FuncN<?> f = Functions.from(unsubscribe); | ||
return new Subscription() { | ||
|
||
@Override | ||
public void unsubscribe() { | ||
f.call(); | ||
} | ||
|
||
}; | ||
} | ||
|
||
/** | ||
* A {@link Subscription} that does nothing when its unsubscribe method is called. | ||
*/ | ||
private static class EmptySubscription implements Subscription { | ||
public void unsubscribe() { | ||
} | ||
} | ||
} |