├── .gitattributes ├── .gitignore ├── .travis.yml ├── HEADER ├── LICENSE ├── README.md ├── build.gradle ├── checkstyle.xml ├── gradle.properties ├── gradle ├── javadocStyleSheet.css ├── license.gradle ├── maven.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── jmh └── java │ └── hu │ └── akarnokd │ └── rxjava2 │ ├── EachTypeFlatMapPerf.java │ ├── InputWithIncrementingInteger.java │ ├── LatchedNbpObserver.java │ ├── LatchedObserver.java │ ├── LatchedSingleObserver.java │ ├── OperatorFlatMapPerf.java │ ├── OperatorMergePerf.java │ ├── RangePerf.java │ └── RxVsStreamPerf.java ├── main └── java │ └── hu │ └── akarnokd │ └── rxjava2 │ ├── BackpressureStrategy.java │ ├── Completable.java │ ├── NbpObservable.java │ ├── NbpObserver.java │ ├── Notification.java │ ├── Observable.java │ ├── Observer.java │ ├── Optional.java │ ├── Scheduler.java │ ├── Single.java │ ├── Try.java │ ├── annotations │ ├── BackpressureKind.java │ ├── BackpressureSupport.java │ ├── Beta.java │ ├── Experimental.java │ ├── SchedulerKind.java │ └── SchedulerSupport.java │ ├── disposables │ ├── BooleanDisposable.java │ ├── CompositeDisposable.java │ ├── Disposable.java │ ├── Disposables.java │ ├── MultipleAssignmentDisposable.java │ ├── RefCountDisposable.java │ └── SerialDisposable.java │ ├── exceptions │ ├── CompositeException.java │ ├── MissingBackpressureException.java │ ├── OnCompleteFailedException.java │ ├── OnErrorFailedException.java │ ├── OnErrorNotImplementedException.java │ └── UnsubscribeFailedException.java │ ├── functions │ ├── BiConsumer.java │ ├── BiFunction.java │ ├── BiPredicate.java │ ├── BooleanSupplier.java │ ├── Consumer.java │ ├── Function.java │ ├── Function3.java │ ├── Function4.java │ ├── Function5.java │ ├── Function6.java │ ├── Function7.java │ ├── Function8.java │ ├── Function9.java │ ├── IntFunction.java │ ├── LongConsumer.java │ ├── Predicate.java │ └── Supplier.java │ ├── internal │ ├── disposables │ │ ├── ArrayCompositeResource.java │ │ ├── CompositeResource.java │ │ ├── EmptyDisposable.java │ │ ├── ListCompositeResource.java │ │ ├── MultipleAssignmentResource.java │ │ ├── NbpFullArbiter.java │ │ ├── SerialResource.java │ │ └── SetCompositeResource.java │ ├── functions │ │ ├── Functions.java │ │ └── Objects.java │ ├── operators │ │ ├── BlockingOperatorLatest.java │ │ ├── BlockingOperatorMostRecent.java │ │ ├── BlockingOperatorNext.java │ │ ├── CachedObservable.java │ │ ├── ObservableScalarSource.java │ │ ├── OperatorAll.java │ │ ├── OperatorAny.java │ │ ├── OperatorBuffer.java │ │ ├── OperatorBufferBoundary.java │ │ ├── OperatorBufferBoundarySupplier.java │ │ ├── OperatorBufferExactBoundary.java │ │ ├── OperatorBufferTimed.java │ │ ├── OperatorCollect.java │ │ ├── OperatorConcatMap.java │ │ ├── OperatorCount.java │ │ ├── OperatorDebounce.java │ │ ├── OperatorDebounceTimed.java │ │ ├── OperatorDelay.java │ │ ├── OperatorDematerialize.java │ │ ├── OperatorDistinct.java │ │ ├── OperatorDoAfterTerminate.java │ │ ├── OperatorDoOnEach.java │ │ ├── OperatorElementAt.java │ │ ├── OperatorFilter.java │ │ ├── OperatorFlatMap.java │ │ ├── OperatorGroupBy.java │ │ ├── OperatorIgnoreElements.java │ │ ├── OperatorMap.java │ │ ├── OperatorMapNotification.java │ │ ├── OperatorMaterialize.java │ │ ├── OperatorObserveOn.java │ │ ├── OperatorOnBackpressureBuffer.java │ │ ├── OperatorOnBackpressureDrop.java │ │ ├── OperatorOnBackpressureLatest.java │ │ ├── OperatorOnErrorNext.java │ │ ├── OperatorOnErrorReturn.java │ │ ├── OperatorPublish.java │ │ ├── OperatorReplay.java │ │ ├── OperatorSamplePublisher.java │ │ ├── OperatorSampleTimed.java │ │ ├── OperatorScan.java │ │ ├── OperatorScanSeed.java │ │ ├── OperatorSingle.java │ │ ├── OperatorSkip.java │ │ ├── OperatorSkipLast.java │ │ ├── OperatorSkipLastTimed.java │ │ ├── OperatorSkipUntil.java │ │ ├── OperatorSkipWhile.java │ │ ├── OperatorSwitchIfEmpty.java │ │ ├── OperatorSwitchMap.java │ │ ├── OperatorTake.java │ │ ├── OperatorTakeLast.java │ │ ├── OperatorTakeLastOne.java │ │ ├── OperatorTakeLastTimed.java │ │ ├── OperatorTakeUntil.java │ │ ├── OperatorTakeUntilPredicate.java │ │ ├── OperatorTakeWhile.java │ │ ├── OperatorThrottleFirstTimed.java │ │ ├── OperatorTimeInterval.java │ │ ├── OperatorTimeout.java │ │ ├── OperatorTimeoutTimed.java │ │ ├── OperatorToList.java │ │ ├── OperatorUnsubscribeOn.java │ │ ├── OperatorWindow.java │ │ ├── OperatorWindowBoundary.java │ │ ├── OperatorWindowBoundarySelector.java │ │ ├── OperatorWindowBoundarySupplier.java │ │ ├── OperatorWindowTimed.java │ │ ├── OperatorWithLatestFrom.java │ │ ├── PublisherAmb.java │ │ ├── PublisherArraySource.java │ │ ├── PublisherAutoConnect.java │ │ ├── PublisherCombineLatest.java │ │ ├── PublisherDefer.java │ │ ├── PublisherDelaySubscriptionOther.java │ │ ├── PublisherEmptySource.java │ │ ├── PublisherErrorSource.java │ │ ├── PublisherFutureSource.java │ │ ├── PublisherGenerate.java │ │ ├── PublisherIntervalOnceSource.java │ │ ├── PublisherIntervalRangeSource.java │ │ ├── PublisherIntervalSource.java │ │ ├── PublisherIterableSource.java │ │ ├── PublisherLift.java │ │ ├── PublisherRangeSource.java │ │ ├── PublisherRedo.java │ │ ├── PublisherRefCount.java │ │ ├── PublisherRepeat.java │ │ ├── PublisherRepeatUntil.java │ │ ├── PublisherRetryBiPredicate.java │ │ ├── PublisherRetryPredicate.java │ │ ├── PublisherScalarAsyncSource.java │ │ ├── PublisherSequenceEqual.java │ │ ├── PublisherSubscribeOn.java │ │ ├── PublisherUsing.java │ │ ├── PublisherZip.java │ │ ├── PublisherZipIterable.java │ │ ├── SubscriberResourceWrapper.java │ │ ├── completable │ │ │ ├── CompletableOnSubscribeConcat.java │ │ │ ├── CompletableOnSubscribeConcatArray.java │ │ │ ├── CompletableOnSubscribeConcatIterable.java │ │ │ ├── CompletableOnSubscribeMerge.java │ │ │ ├── CompletableOnSubscribeMergeArray.java │ │ │ ├── CompletableOnSubscribeMergeDelayErrorArray.java │ │ │ ├── CompletableOnSubscribeMergeDelayErrorIterable.java │ │ │ ├── CompletableOnSubscribeMergeIterable.java │ │ │ └── CompletableOnSubscribeTimeout.java │ │ ├── nbp │ │ │ ├── NbpBlockingOperatorLatest.java │ │ │ ├── NbpBlockingOperatorMostRecent.java │ │ │ ├── NbpBlockingOperatorNext.java │ │ │ ├── NbpCachedObservable.java │ │ │ ├── NbpObservableScalarSource.java │ │ │ ├── NbpOnSubscribeAmb.java │ │ │ ├── NbpOnSubscribeArraySource.java │ │ │ ├── NbpOnSubscribeAutoConnect.java │ │ │ ├── NbpOnSubscribeCombineLatest.java │ │ │ ├── NbpOnSubscribeDefer.java │ │ │ ├── NbpOnSubscribeDelaySubscriptionOther.java │ │ │ ├── NbpOnSubscribeErrorSource.java │ │ │ ├── NbpOnSubscribeFutureSource.java │ │ │ ├── NbpOnSubscribeGenerate.java │ │ │ ├── NbpOnSubscribeIntervalRangeSource.java │ │ │ ├── NbpOnSubscribeIntervalSource.java │ │ │ ├── NbpOnSubscribeIterableSource.java │ │ │ ├── NbpOnSubscribeLift.java │ │ │ ├── NbpOnSubscribeRedo.java │ │ │ ├── NbpOnSubscribeRefCount.java │ │ │ ├── NbpOnSubscribeRepeat.java │ │ │ ├── NbpOnSubscribeRepeatUntil.java │ │ │ ├── NbpOnSubscribeRetryBiPredicate.java │ │ │ ├── NbpOnSubscribeRetryPredicate.java │ │ │ ├── NbpOnSubscribeScalarAsyncSource.java │ │ │ ├── NbpOnSubscribeSequenceEqual.java │ │ │ ├── NbpOnSubscribeSubscribeOn.java │ │ │ ├── NbpOnSubscribeTimerOnceSource.java │ │ │ ├── NbpOnSubscribeUsing.java │ │ │ ├── NbpOnSubscribeZip.java │ │ │ ├── NbpOnSubscribeZipIterable.java │ │ │ ├── NbpOperatorAll.java │ │ │ ├── NbpOperatorAny.java │ │ │ ├── NbpOperatorBuffer.java │ │ │ ├── NbpOperatorBufferBoundary.java │ │ │ ├── NbpOperatorBufferBoundarySupplier.java │ │ │ ├── NbpOperatorBufferExactBoundary.java │ │ │ ├── NbpOperatorBufferTimed.java │ │ │ ├── NbpOperatorCollect.java │ │ │ ├── NbpOperatorConcatMap.java │ │ │ ├── NbpOperatorCount.java │ │ │ ├── NbpOperatorDebounce.java │ │ │ ├── NbpOperatorDebounceTimed.java │ │ │ ├── NbpOperatorDelay.java │ │ │ ├── NbpOperatorDematerialize.java │ │ │ ├── NbpOperatorDistinct.java │ │ │ ├── NbpOperatorDoOnEach.java │ │ │ ├── NbpOperatorElementAt.java │ │ │ ├── NbpOperatorFilter.java │ │ │ ├── NbpOperatorFlatMap.java │ │ │ ├── NbpOperatorGroupBy.java │ │ │ ├── NbpOperatorIgnoreElements.java │ │ │ ├── NbpOperatorMap.java │ │ │ ├── NbpOperatorMapNotification.java │ │ │ ├── NbpOperatorMaterialize.java │ │ │ ├── NbpOperatorObserveOn.java │ │ │ ├── NbpOperatorOnErrorNext.java │ │ │ ├── NbpOperatorOnErrorReturn.java │ │ │ ├── NbpOperatorPublish.java │ │ │ ├── NbpOperatorReplay.java │ │ │ ├── NbpOperatorSampleTimed.java │ │ │ ├── NbpOperatorSampleWithObservable.java │ │ │ ├── NbpOperatorScan.java │ │ │ ├── NbpOperatorScanSeed.java │ │ │ ├── NbpOperatorSingle.java │ │ │ ├── NbpOperatorSkip.java │ │ │ ├── NbpOperatorSkipLast.java │ │ │ ├── NbpOperatorSkipLastTimed.java │ │ │ ├── NbpOperatorSkipUntil.java │ │ │ ├── NbpOperatorSkipWhile.java │ │ │ ├── NbpOperatorSwitchIfEmpty.java │ │ │ ├── NbpOperatorSwitchMap.java │ │ │ ├── NbpOperatorTake.java │ │ │ ├── NbpOperatorTakeLast.java │ │ │ ├── NbpOperatorTakeLastOne.java │ │ │ ├── NbpOperatorTakeLastTimed.java │ │ │ ├── NbpOperatorTakeUntil.java │ │ │ ├── NbpOperatorTakeUntilPredicate.java │ │ │ ├── NbpOperatorTakeWhile.java │ │ │ ├── NbpOperatorThrottleFirstTimed.java │ │ │ ├── NbpOperatorTimeInterval.java │ │ │ ├── NbpOperatorTimeout.java │ │ │ ├── NbpOperatorTimeoutTimed.java │ │ │ ├── NbpOperatorToList.java │ │ │ ├── NbpOperatorUnsubscribeOn.java │ │ │ ├── NbpOperatorWindow.java │ │ │ ├── NbpOperatorWindowBoundary.java │ │ │ ├── NbpOperatorWindowBoundarySelector.java │ │ │ ├── NbpOperatorWindowBoundarySupplier.java │ │ │ ├── NbpOperatorWindowTimed.java │ │ │ ├── NbpOperatorWithLatestFrom.java │ │ │ └── NbpSubscriberResourceWrapper.java │ │ └── single │ │ │ ├── SingleOperatorFlatMap.java │ │ │ └── SingleOperatorMap.java │ ├── queue │ │ ├── BaseArrayQueue.java │ │ ├── BaseLinkedQueue.java │ │ ├── LinkedQueueNode.java │ │ ├── MpscLinkedQueue.java │ │ ├── SpscArrayQueue.java │ │ ├── SpscExactArrayQueue.java │ │ └── SpscLinkedArrayQueue.java │ ├── schedulers │ │ ├── ComputationScheduler.java │ │ ├── ExecutorScheduler.java │ │ ├── IOScheduler.java │ │ ├── NewThreadScheduler.java │ │ ├── NewThreadWorker.java │ │ ├── RxThreadFactory.java │ │ ├── ScheduledRunnable.java │ │ ├── SchedulerLifecycle.java │ │ ├── SchedulerPoolFactory.java │ │ ├── SingleScheduler.java │ │ └── TrampolineScheduler.java │ ├── subscribers │ │ ├── BlockingSubscriber.java │ │ ├── CancelledSubscriber.java │ │ ├── ConditionalSubscriber.java │ │ ├── DisposableSubscriber.java │ │ ├── EmptySubscriber.java │ │ ├── FullArbiterSubscriber.java │ │ ├── LambdaSubscriber.java │ │ ├── NbpFullArbiterSubscriber.java │ │ ├── QueueDrainSubscriber.java │ │ ├── SubscriptionLambdaSubscriber.java │ │ ├── ToNotificationSubscriber.java │ │ └── nbp │ │ │ ├── NbpBlockingSubscriber.java │ │ │ ├── NbpCancelledSubscriber.java │ │ │ ├── NbpConditionalSubscriber.java │ │ │ ├── NbpDisposableSubscriber.java │ │ │ ├── NbpEmptySubscriber.java │ │ │ ├── NbpLambdaSubscriber.java │ │ │ ├── NbpQueueDrainSubscriber.java │ │ │ ├── NbpSubscriptionLambdaSubscriber.java │ │ │ └── NbpToNotificationSubscriber.java │ ├── subscriptions │ │ ├── AsyncSubscription.java │ │ ├── BooleanSubscription.java │ │ ├── DisposableSubscription.java │ │ ├── EmptySubscription.java │ │ ├── FullArbiter.java │ │ ├── ScalarAsyncSubscription.java │ │ ├── ScalarSubscription.java │ │ ├── SubscriptionArbiter.java │ │ └── SubscriptionHelper.java │ └── util │ │ ├── AppendOnlyLinkedArrayList.java │ │ ├── BackpressureHelper.java │ │ ├── Exceptions.java │ │ ├── LinkedArrayList.java │ │ ├── NbpQueueDrain.java │ │ ├── NotificationLite.java │ │ ├── OpenHashSet.java │ │ ├── Pow2.java │ │ ├── QueueDrain.java │ │ ├── QueueDrainHelper.java │ │ └── TerminalAtomicsHelper.java │ ├── observables │ ├── BlockingObservable.java │ ├── ConnectableObservable.java │ ├── GroupedObservable.java │ └── nbp │ │ ├── NbpBlockingObservable.java │ │ ├── NbpConnectableObservable.java │ │ └── NbpGroupedObservable.java │ ├── plugins │ └── RxJavaPlugins.java │ ├── schedulers │ ├── Schedulers.java │ ├── TestScheduler.java │ └── Timed.java │ ├── subjects │ ├── AsyncSubject.java │ ├── BehaviorSubject.java │ ├── PublishSubject.java │ ├── ReplaySubject.java │ ├── SerializedSubject.java │ ├── Subject.java │ ├── UnicastSubject.java │ └── nbp │ │ ├── NbpAsyncSubject.java │ │ ├── NbpBehaviorSubject.java │ │ ├── NbpPublishSubject.java │ │ ├── NbpReplaySubject.java │ │ ├── NbpSerializedSubject.java │ │ ├── NbpSubject.java │ │ └── NbpUnicastSubject.java │ └── subscribers │ ├── AsyncObserver.java │ ├── Observers.java │ ├── SafeSubscriber.java │ ├── SerializedSubscriber.java │ ├── Subscribers.java │ ├── TestSubscriber.java │ ├── completable │ └── CompletableSerializedSubscriber.java │ └── nbp │ ├── NbpAsyncObserver.java │ ├── NbpObservers.java │ ├── NbpSafeSubscriber.java │ ├── NbpSerializedSubscriber.java │ ├── NbpSubscribers.java │ └── NbpTestSubscriber.java └── test └── java └── hu └── akarnokd └── rxjava2 ├── BackpressureTests.java ├── CombineLatestTests.java ├── CompletableTest.java ├── ConcatTests.java ├── CovarianceTest.java ├── ErrorHandlingTests.java ├── EventStream.java ├── GroupByTests.java ├── MergeTests.java ├── NbpObservableNullTests.java ├── NbpObservableTest.java ├── NotificationTest.java ├── ObservableConversionTest.java ├── ObservableDoOnTest.java ├── ObservableNullTests.java ├── ObservableTests.java ├── ObservableWindowTests.java ├── ReduceTests.java ├── ScanTests.java ├── SingleNullTests.java ├── SingleTest.java ├── StartWithTests.java ├── SubscriberTest.java ├── TestHelper.java ├── ThrottleLastTests.java ├── ThrottleWithTimeoutTests.java ├── ZipTests.java ├── disposables ├── CompositeDisposableTest.java ├── DisposablesTest.java ├── MultipleAssignmentDisposableTest.java ├── RefCountDisposableTest.java └── SerialDisposableTests.java ├── exceptions └── TestException.java ├── internal ├── operators │ ├── BackpressureHelperTest.java │ ├── BlockingOperatorLatestTest.java │ ├── BlockingOperatorMostRecentTest.java │ ├── BlockingOperatorNextTest.java │ ├── BlockingOperatorToIteratorTest.java │ ├── BufferUntilSubscriberTest.java │ ├── CachedObservableTest.java │ ├── NotificationLiteTest.java │ ├── OnSubscribeAmbTest.java │ ├── OnSubscribeCombineLatestTest.java │ ├── OnSubscribeDeferTest.java │ ├── OnSubscribeFromIterableTest.java │ ├── OnSubscribeRangeTest.java │ ├── OnSubscribeRefCountTest.java │ ├── OnSubscribeTimerTest.java │ ├── OnSubscribeToObservableFutureTest.java │ ├── OnSubscribeUsingTest.java │ ├── OperatorAllTest.java │ ├── OperatorAnyTest.java │ ├── OperatorAsObservableTest.java │ ├── OperatorBufferTest.java │ ├── OperatorCastTest.java │ ├── OperatorConcatTest.java │ ├── OperatorDebounceTest.java │ ├── OperatorDefaultIfEmptyTest.java │ ├── OperatorDelayTest.java │ ├── OperatorDematerializeTest.java │ ├── OperatorDistinctTest.java │ ├── OperatorDistinctUntilChangedTest.java │ ├── OperatorDoOnEachTest.java │ ├── OperatorDoOnRequestTest.java │ ├── OperatorDoOnSubscribeTest.java │ ├── OperatorDoOnUnsubscribeTest.java │ ├── OperatorElementAtTest.java │ ├── OperatorFilterTest.java │ ├── OperatorFinallyTest.java │ ├── OperatorFirstTest.java │ ├── OperatorFlatMapTest.java │ ├── OperatorGroupByTest.java │ ├── OperatorIgnoreElementsTest.java │ ├── OperatorLastTest.java │ ├── OperatorMapNotificationTest.java │ ├── OperatorMapTest.java │ ├── OperatorMaterializeTest.java │ ├── OperatorMergeDelayErrorTest.java │ ├── OperatorMergeMaxConcurrentTest.java │ ├── OperatorMergeTest.java │ ├── OperatorMulticastTest.java │ ├── OperatorObserveOnTest.java │ ├── OperatorOnBackpressureBufferTest.java │ ├── OperatorOnBackpressureDropTest.java │ ├── OperatorOnBackpressureLatestTest.java │ ├── OperatorOnErrorResumeNextViaFunctionTest.java │ ├── OperatorOnErrorResumeNextViaObservableTest.java │ ├── OperatorOnErrorReturnTest.java │ ├── OperatorOnExceptionResumeNextViaObservableTest.java │ ├── OperatorPublishTest.java │ ├── OperatorReduceTest.java │ ├── OperatorRepeatTest.java │ ├── OperatorReplayTest.java │ ├── OperatorRetryTest.java │ ├── OperatorRetryWithPredicateTest.java │ ├── OperatorSampleTest.java │ ├── OperatorScanTest.java │ ├── OperatorSequenceEqualTest.java │ ├── OperatorSerializeTest.java │ ├── OperatorSingleTest.java │ ├── OperatorSkipLastTest.java │ ├── OperatorSkipLastTimedTest.java │ ├── OperatorSkipTest.java │ ├── OperatorSkipTimedTest.java │ ├── OperatorSkipUntilTest.java │ ├── OperatorSkipWhileTest.java │ ├── OperatorSubscribeOnTest.java │ ├── OperatorSwitchIfEmptyTest.java │ ├── OperatorSwitchTest.java │ ├── OperatorTakeLastOneTest.java │ ├── OperatorTakeLastTest.java │ ├── OperatorTakeLastTimedTest.java │ ├── OperatorTakeTest.java │ ├── OperatorTakeTimedTest.java │ ├── OperatorTakeUntilPredicateTest.java │ ├── OperatorTakeUntilTest.java │ ├── OperatorTakeWhileTest.java │ ├── OperatorThrottleFirstTest.java │ ├── OperatorTimeIntervalTest.java │ ├── OperatorTimeoutTests.java │ ├── OperatorTimeoutWithSelectorTest.java │ ├── OperatorTimestampTest.java │ ├── OperatorToMapTest.java │ ├── OperatorToMultimapTest.java │ ├── OperatorToObservableListTest.java │ ├── OperatorToObservableSortedListTest.java │ ├── OperatorUnsubscribeOnTest.java │ ├── OperatorWindowWithObservableTest.java │ ├── OperatorWindowWithSizeTest.java │ ├── OperatorWindowWithStartEndObservableTest.java │ ├── OperatorWindowWithTimeTest.java │ ├── OperatorWithLatestFromTest.java │ ├── OperatorZipCompletionTest.java │ ├── OperatorZipIterableTest.java │ ├── OperatorZipTest.java │ ├── PublisherDelaySubscriptionOtherTest.java │ └── nbp │ │ ├── NbpBlockingOperatorLatestTest.java │ │ ├── NbpBlockingOperatorMostRecentTest.java │ │ ├── NbpBlockingOperatorNextTest.java │ │ ├── NbpBlockingOperatorToIteratorTest.java │ │ ├── NbpBufferUntilSubscriberTest.java │ │ ├── NbpCachedObservableTest.java │ │ ├── NbpOnSubscribeAmbTest.java │ │ ├── NbpOnSubscribeCombineLatestTest.java │ │ ├── NbpOnSubscribeDeferTest.java │ │ ├── NbpOnSubscribeDelaySubscriptionOtherTest.java │ │ ├── NbpOnSubscribeFromIterableTest.java │ │ ├── NbpOnSubscribeRangeTest.java │ │ ├── NbpOnSubscribeRefCountTest.java │ │ ├── NbpOnSubscribeTimerTest.java │ │ ├── NbpOnSubscribeToObservableFutureTest.java │ │ ├── NbpOnSubscribeUsingTest.java │ │ ├── NbpOperatorAllTest.java │ │ ├── NbpOperatorAnyTest.java │ │ ├── NbpOperatorAsObservableTest.java │ │ ├── NbpOperatorBufferTest.java │ │ ├── NbpOperatorCastTest.java │ │ ├── NbpOperatorConcatTest.java │ │ ├── NbpOperatorDebounceTest.java │ │ ├── NbpOperatorDefaultIfEmptyTest.java │ │ ├── NbpOperatorDelayTest.java │ │ ├── NbpOperatorDematerializeTest.java │ │ ├── NbpOperatorDistinctTest.java │ │ ├── NbpOperatorDistinctUntilChangedTest.java │ │ ├── NbpOperatorDoOnEachTest.java │ │ ├── NbpOperatorDoOnSubscribeTest.java │ │ ├── NbpOperatorDoOnUnsubscribeTest.java │ │ ├── NbpOperatorElementAtTest.java │ │ ├── NbpOperatorFilterTest.java │ │ ├── NbpOperatorFinallyTest.java │ │ ├── NbpOperatorFirstTest.java │ │ ├── NbpOperatorFlatMapTest.java │ │ ├── NbpOperatorGroupByTest.java │ │ ├── NbpOperatorIgnoreElementsTest.java │ │ ├── NbpOperatorLastTest.java │ │ ├── NbpOperatorMapNotificationTest.java │ │ ├── NbpOperatorMapTest.java │ │ ├── NbpOperatorMaterializeTest.java │ │ ├── NbpOperatorMergeDelayErrorTest.java │ │ ├── NbpOperatorMergeMaxConcurrentTest.java │ │ ├── NbpOperatorMergeTest.java │ │ ├── NbpOperatorMulticastTest.java │ │ ├── NbpOperatorObserveOnTest.java │ │ ├── NbpOperatorOnErrorResumeNextViaFunctionTest.java │ │ ├── NbpOperatorOnErrorResumeNextViaObservableTest.java │ │ ├── NbpOperatorOnErrorReturnTest.java │ │ ├── NbpOperatorOnExceptionResumeNextViaObservableTest.java │ │ ├── NbpOperatorPublishTest.java │ │ ├── NbpOperatorReduceTest.java │ │ ├── NbpOperatorRepeatTest.java │ │ ├── NbpOperatorReplayTest.java │ │ ├── NbpOperatorRetryTest.java │ │ ├── NbpOperatorRetryWithPredicateTest.java │ │ ├── NbpOperatorSampleTest.java │ │ ├── NbpOperatorScanTest.java │ │ ├── NbpOperatorSequenceEqualTest.java │ │ ├── NbpOperatorSerializeTest.java │ │ ├── NbpOperatorSingleTest.java │ │ ├── NbpOperatorSkipLastTest.java │ │ ├── NbpOperatorSkipLastTimedTest.java │ │ ├── NbpOperatorSkipTest.java │ │ ├── NbpOperatorSkipTimedTest.java │ │ ├── NbpOperatorSkipUntilTest.java │ │ ├── NbpOperatorSkipWhileTest.java │ │ ├── NbpOperatorSubscribeOnTest.java │ │ ├── NbpOperatorSwitchIfEmptyTest.java │ │ ├── NbpOperatorSwitchTest.java │ │ ├── NbpOperatorTakeLastOneTest.java │ │ ├── NbpOperatorTakeLastTest.java │ │ ├── NbpOperatorTakeLastTimedTest.java │ │ ├── NbpOperatorTakeTest.java │ │ ├── NbpOperatorTakeTimedTest.java │ │ ├── NbpOperatorTakeUntilPredicateTest.java │ │ ├── NbpOperatorTakeUntilTest.java │ │ ├── NbpOperatorTakeWhileTest.java │ │ ├── NbpOperatorThrottleFirstTest.java │ │ ├── NbpOperatorTimeIntervalTest.java │ │ ├── NbpOperatorTimeoutTests.java │ │ ├── NbpOperatorTimeoutWithSelectorTest.java │ │ ├── NbpOperatorTimestampTest.java │ │ ├── NbpOperatorToMapTest.java │ │ ├── NbpOperatorToMultimapTest.java │ │ ├── NbpOperatorToObservableListTest.java │ │ ├── NbpOperatorToObservableSortedListTest.java │ │ ├── NbpOperatorUnsubscribeOnTest.java │ │ ├── NbpOperatorWindowWithObservableTest.java │ │ ├── NbpOperatorWindowWithSizeTest.java │ │ ├── NbpOperatorWindowWithStartEndObservableTest.java │ │ ├── NbpOperatorWindowWithTimeTest.java │ │ ├── NbpOperatorWithLatestFromTest.java │ │ ├── NbpOperatorZipCompletionTest.java │ │ ├── NbpOperatorZipIterableTest.java │ │ └── NbpOperatorZipTest.java └── subscriptions │ └── AsyncSubscriptionTest.java ├── nbp ├── NbpCombineLatestTests.java ├── NbpConcatTests.java ├── NbpCovarianceTest.java ├── NbpErrorHandlingTests.java ├── NbpEventStream.java ├── NbpGroupByTests.java ├── NbpMergeTests.java ├── NbpObservableDoOnTest.java ├── NbpObservableTest.java ├── NbpObservableTests.java ├── NbpObservableWindowTests.java ├── NbpReduceTests.java ├── NbpScanTests.java ├── NbpStartWithTests.java ├── NbpSubscriberTest.java ├── NbpThrottleLastTests.java ├── NbpThrottleWithTimeoutTests.java └── NbpZipTests.java ├── schedulers ├── AbstractSchedulerConcurrencyTests.java ├── AbstractSchedulerTests.java ├── CachedThreadSchedulerTest.java ├── ComputationSchedulerTests.java ├── ExecutorSchedulerTest.java ├── NewThreadSchedulerTest.java ├── SchedulerLifecycleTest.java ├── SchedulerTests.java ├── TestSchedulerTest.java └── TrampolineSchedulerTest.java ├── subjects ├── AsyncSubjectTest.java ├── BehaviorSubjectTest.java ├── PublishSubjectTest.java ├── ReplaySubjectBoundedConcurrencyTest.java ├── ReplaySubjectConcurrencyTest.java ├── ReplaySubjectTest.java ├── SerializedSubjectTest.java └── nbp │ ├── NbpAsyncSubjectTest.java │ ├── NbpBehaviorSubjectTest.java │ ├── NbpPublishSubjectTest.java │ ├── NbpReplaySubjectBoundedConcurrencyTest.java │ ├── NbpReplaySubjectConcurrencyTest.java │ ├── NbpReplaySubjectTest.java │ └── NbpSerializedSubjectTest.java └── subscribers ├── ObserversTest.java ├── SafeObserverTest.java ├── SafeSubscriberTest.java ├── SafeSubscriberWithPluginTest.java ├── SerializedObserverTest.java ├── SubscribersTest.java ├── TestObserverTest.java └── TestSubscriberTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behaviour, in case users don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files we want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.java text 7 | *.groovy text 8 | *.scala text 9 | *.clj text 10 | *.txt text 11 | *.md text 12 | 13 | # Denote all files that are truly binary and should not be modified. 14 | *.png binary 15 | *.jpg binary 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | /build/ 8 | 9 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 10 | 11 | hs_err_pid* 12 | 13 | .gradle 14 | .m2 15 | /.nb-gradle/ 16 | /bin/ 17 | .settings/ 18 | .nb-gradle-properties 19 | .classpath 20 | .project 21 | .settings 22 | .metadata 23 | .checkstyle 24 | bin/ 25 | !/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: 4 | - oraclejdk8 5 | 6 | # force upgrade Java8 as per https://github.com/travis-ci/travis-ci/issues/4042 (fixes compilation issue) 7 | addons: 8 | apt: 9 | packages: 10 | - oracle-java8-installer 11 | 12 | # cache between builds 13 | cache: 14 | directories: 15 | - $HOME/.m2 16 | - $HOME/.gradle 17 | 18 | before_install: 19 | - chmod +x gradlew -------------------------------------------------------------------------------- /HEADER: -------------------------------------------------------------------------------- 1 | Copyright 2015 David Karnok and Netflix, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 4 | compliance with the License. You may obtain a copy of the License at 5 | 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | Unless required by applicable law or agreed to in writing, software distributed under the License is 9 | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 10 | the License for the specific language governing permissions and limitations under the License. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RxJava 2.0 Backport 2 | ============== 3 | 4 | Discontinued. The contents have been moved back and enhanced upon in the main [RxJava 2](https://github.com/ReactiveX/RxJava/tree/2.x) project. 5 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=2.0.0-RC3 2 | -------------------------------------------------------------------------------- /gradle/javadocStyleSheet.css: -------------------------------------------------------------------------------- 1 | # originally from http://sensemaya.org/files/stylesheet.css and then modified 2 | # http://sensemaya.org/maya/2009/07/10/making-javadoc-more-legible 3 | 4 | /* Javadoc style sheet */ 5 | 6 | /* Define colors, fonts and other style attributes here to override the defaults */ 7 | 8 | /* Page background color */ 9 | body { background-color: #FFFFFF; color:#333; font-size: 100%; } 10 | 11 | body { font-size: 0.875em; line-height: 1.286em; font-family: "Helvetica", "Arial", sans-serif; } 12 | 13 | code { color: #777; line-height: 1.286em; font-family: "Consolas", "Lucida Console", "Droid Sans Mono", "Andale Mono", "Monaco", "Lucida Sans Typewriter"; } 14 | 15 | a { text-decoration: none; color: #16569A; /* also try #2E85ED, #0033FF, #6C93C6, #1D7BBE, #1D8DD2 */ } 16 | a:hover { text-decoration: underline; } 17 | 18 | 19 | table[border="1"] { border: 1px solid #ddd; } 20 | table[border="1"] td, table[border="1"] th { border: 1px solid #ddd; } 21 | table[cellpadding="3"] td { padding: 0.5em; } 22 | 23 | font[size="-1"] { font-size: 0.85em; line-height: 1.5em; } 24 | font[size="-2"] { font-size: 0.8em; } 25 | font[size="+2"] { font-size: 1.4em; line-height: 1.3em; padding: 0.4em 0; } 26 | 27 | /* Headings */ 28 | h1 { font-size: 1.5em; line-height: 1.286em;} 29 | h2.title { color: #c81f08; } 30 | 31 | /* Table colors */ 32 | .TableHeadingColor { background: #ccc; color:#444; } /* Dark mauve */ 33 | .TableSubHeadingColor { background: #ddd; color:#444; } /* Light mauve */ 34 | .TableRowColor { background: #FFFFFF; color:#666; font-size: 0.95em; } /* White */ 35 | .TableRowColor code { color:#000; } /* White */ 36 | 37 | /* Font used in left-hand frame lists */ 38 | .FrameTitleFont { font-size: 100%; } 39 | .FrameHeadingFont { font-size: 90%; } 40 | .FrameItemFont { font-size: 0.9em; line-height: 1.3em; 41 | } 42 | /* Java Interfaces */ 43 | .FrameItemFont a i { 44 | font-style: normal; color: #16569A; 45 | } 46 | .FrameItemFont a:hover i { 47 | text-decoration: underline; 48 | } 49 | 50 | 51 | /* Navigation bar fonts and colors */ 52 | .NavBarCell1 { background-color:#E0E6DF; } /* Light mauve */ 53 | .NavBarCell1Rev { background-color:#16569A; color:#FFFFFF} /* Dark Blue */ 54 | .NavBarFont1 { } 55 | .NavBarFont1Rev { color:#FFFFFF; } 56 | 57 | .NavBarCell2 { background-color:#FFFFFF; color:#000000} 58 | .NavBarCell3 { background-color:#FFFFFF; color:#000000} 59 | 60 | -------------------------------------------------------------------------------- /gradle/license.gradle: -------------------------------------------------------------------------------- 1 | license { 2 | header rootProject.file('HEADER') 3 | ext.year = Calendar.getInstance().get(Calendar.YEAR) 4 | skipExistingHeaders true 5 | ignoreFailures true 6 | } 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akarnokd/rxjava2-backport/9cac47e86a9b48b2f021d27e3d0031132b57711e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Aug 5 14:18:17 CET 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip 7 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'rxjava2-backport' 2 | -------------------------------------------------------------------------------- /src/jmh/java/hu/akarnokd/rxjava2/LatchedNbpObserver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import java.util.concurrent.CountDownLatch; 17 | 18 | import org.openjdk.jmh.infra.Blackhole; 19 | 20 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 21 | import hu.akarnokd.rxjava2.disposables.Disposable; 22 | 23 | public final class LatchedNbpObserver implements NbpSubscriber { 24 | final CountDownLatch cdl; 25 | final Blackhole bh; 26 | public LatchedNbpObserver(Blackhole bh) { 27 | this.bh = bh; 28 | this.cdl = new CountDownLatch(1); 29 | } 30 | @Override 31 | public void onSubscribe(Disposable d) { 32 | 33 | } 34 | @Override 35 | public void onNext(T value) { 36 | bh.consume(value); 37 | } 38 | @Override 39 | public void onError(Throwable e) { 40 | e.printStackTrace(); 41 | cdl.countDown(); 42 | } 43 | @Override 44 | public void onComplete() { 45 | cdl.countDown(); 46 | } 47 | } -------------------------------------------------------------------------------- /src/jmh/java/hu/akarnokd/rxjava2/LatchedObserver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import java.util.concurrent.CountDownLatch; 17 | 18 | import org.openjdk.jmh.infra.Blackhole; 19 | 20 | public class LatchedObserver extends Observer { 21 | 22 | public CountDownLatch latch = new CountDownLatch(1); 23 | private final Blackhole bh; 24 | 25 | public LatchedObserver(Blackhole bh) { 26 | this.bh = bh; 27 | } 28 | 29 | @Override 30 | public void onComplete() { 31 | latch.countDown(); 32 | } 33 | 34 | @Override 35 | public void onError(Throwable e) { 36 | latch.countDown(); 37 | } 38 | 39 | @Override 40 | public void onNext(T t) { 41 | bh.consume(t); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/jmh/java/hu/akarnokd/rxjava2/LatchedSingleObserver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import java.util.concurrent.CountDownLatch; 17 | 18 | import org.openjdk.jmh.infra.Blackhole; 19 | 20 | import hu.akarnokd.rxjava2.Single.SingleSubscriber; 21 | import hu.akarnokd.rxjava2.disposables.Disposable; 22 | 23 | public final class LatchedSingleObserver implements SingleSubscriber { 24 | final CountDownLatch cdl; 25 | final Blackhole bh; 26 | public LatchedSingleObserver(Blackhole bh) { 27 | this.bh = bh; 28 | this.cdl = new CountDownLatch(1); 29 | } 30 | @Override 31 | public void onSubscribe(Disposable d) { 32 | 33 | } 34 | @Override 35 | public void onSuccess(T value) { 36 | bh.consume(value); 37 | cdl.countDown(); 38 | } 39 | @Override 40 | public void onError(Throwable e) { 41 | e.printStackTrace(); 42 | cdl.countDown(); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/BackpressureStrategy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | public enum BackpressureStrategy { 17 | BUFFER, 18 | DROP, 19 | LATEST 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/NbpObserver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 19 | 20 | public abstract class NbpObserver implements NbpSubscriber { 21 | private Disposable s; 22 | @Override 23 | public final void onSubscribe(Disposable s) { 24 | if (SubscriptionHelper.validateDisposable(this.s, s)) { 25 | return; 26 | } 27 | this.s = s; 28 | onStart(); 29 | } 30 | 31 | protected final void cancel() { 32 | s.dispose(); 33 | } 34 | /** 35 | * Called once the subscription has been set on this observer; override this 36 | * to perform initialization. 37 | */ 38 | protected void onStart() { 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/Notification.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import hu.akarnokd.rxjava2.internal.functions.Objects; 17 | 18 | /** 19 | * Utility class to help construct notification objects. 20 | */ 21 | public final class Notification { 22 | private Notification() { 23 | throw new IllegalStateException(); 24 | } 25 | 26 | static final Try> COMPLETE = Try.ofValue(Optional.empty()); 27 | 28 | @SuppressWarnings({ "rawtypes", "unchecked" }) 29 | public static Try> complete() { 30 | return (Try)COMPLETE; 31 | } 32 | 33 | @SuppressWarnings({ "rawtypes", "unchecked" }) 34 | public static Try> error(Throwable e) { 35 | return (Try)Try.ofError(e); 36 | } 37 | 38 | public static Try> next(T value) { 39 | Objects.requireNonNull(value, "value is null"); // TODO this coud instead return an error of NPE 40 | return Try.ofValue(Optional.of(value)); 41 | } 42 | 43 | public static boolean isNext(Try> notification) { 44 | if (notification.hasValue()) { 45 | return notification.value().isPresent(); 46 | } 47 | return false; 48 | } 49 | 50 | public static boolean isComplete(Try> notification) { 51 | if (notification.hasValue()) { 52 | return !notification.value().isPresent(); 53 | } 54 | return false; 55 | } 56 | 57 | public static boolean isError(Try> notification) { 58 | return notification.hasError(); 59 | } 60 | 61 | public static T getValue(Try> notification) { 62 | if (notification.hasValue()) { 63 | return notification.value.get(); 64 | } 65 | return null; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/Observer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 19 | 20 | public abstract class Observer implements Subscriber { 21 | private Subscription s; 22 | @Override 23 | public final void onSubscribe(Subscription s) { 24 | if (SubscriptionHelper.validateSubscription(this.s, s)) { 25 | return; 26 | } 27 | this.s = s; 28 | onStart(); 29 | } 30 | 31 | protected final Subscription subscription() { 32 | return s; 33 | } 34 | 35 | protected final void request(long n) { 36 | subscription().request(n); 37 | } 38 | 39 | protected final void cancel() { 40 | subscription().cancel(); 41 | } 42 | /** 43 | * Called once the subscription has been set on this observer; override this 44 | * to perform initialization or issue an initial request. 45 | *

46 | * The default implementation requests {@link Long#MAX_VALUE}. 47 | */ 48 | protected void onStart() { 49 | request(Long.MAX_VALUE); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/Optional.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import hu.akarnokd.rxjava2.internal.functions.Objects; 17 | 18 | /** 19 | * Simplified backport of Java 8's Optional type. 20 | * 21 | * @param the value type 22 | */ 23 | public final class Optional { 24 | final T value; 25 | protected Optional(T value) { 26 | this.value = value; 27 | } 28 | 29 | static final Optional EMPTY = new Optional(null); 30 | 31 | @SuppressWarnings("unchecked") 32 | public static Optional empty() { 33 | return (Optional)EMPTY; 34 | } 35 | 36 | public static Optional of(T value) { 37 | Objects.requireNonNull(value, "value is null"); 38 | return new Optional(value); 39 | } 40 | 41 | public boolean isPresent() { 42 | return value != null; 43 | } 44 | 45 | public T get() { 46 | return value; 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | final int prime = 31; 52 | int result = 1; 53 | result = prime * result + ((value == null) ? 0 : value.hashCode()); 54 | return result; 55 | } 56 | 57 | @Override 58 | public boolean equals(Object obj) { 59 | if (this == obj) { 60 | return true; 61 | } 62 | if (obj == null) { 63 | return false; 64 | } 65 | if (getClass() != obj.getClass()) { 66 | return false; 67 | } 68 | Optional other = (Optional) obj; 69 | if (value == null) { 70 | if (other.value != null) { 71 | return false; 72 | } 73 | } else if (!value.equals(other.value)) { 74 | return false; 75 | } 76 | return true; 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/BackpressureKind.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | /** 17 | * Enumeration for various kinds of backpressure support. 18 | */ 19 | public enum BackpressureKind { 20 | /** 21 | * The backpressure-related requests pass through this operator without change 22 | */ 23 | PASS_THROUGH, 24 | /** 25 | * The operator fully supports backpressure and may coordinate downstream requests 26 | * with upstream requests through batching, arbitration or by other means. 27 | */ 28 | FULL, 29 | /** 30 | * The operator performs special backpressure management; see the associated javadoc. 31 | */ 32 | SPECIAL, 33 | /** 34 | * The operator requests Long.MAX_VALUE from upstream but respects the backpressure 35 | * of the downstream. 36 | */ 37 | UNBOUNDED_IN, 38 | /** 39 | * The operator will emit a MissingBackpressureException if the downstream didn't request 40 | * enough or in time. 41 | */ 42 | ERROR, 43 | /** 44 | * The operator ignores all kinds of backpressure and may overflow the downstream. 45 | */ 46 | NONE 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/BackpressureSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | import java.lang.annotation.*; 17 | 18 | /** 19 | * Indicates the backpressure support kind of the associated operator or class. 20 | */ 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Documented 23 | @Target({ElementType.METHOD, ElementType.TYPE}) 24 | public @interface BackpressureSupport { 25 | /** 26 | * The backpressure supported by this method or class. 27 | * @return backpressure supported by this method or class. 28 | */ 29 | BackpressureKind value(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/Beta.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | /** 17 | * Indicates the feature is in beta state: it will be most likely stay but 18 | * the signature may change between versions without warning. 19 | */ 20 | public @interface Beta { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/Experimental.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | /** 17 | * Indicates the feature is in experimental state: its existence, signature or behavior 18 | * might change without warning from one release to the next. 19 | */ 20 | public @interface Experimental { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/SchedulerKind.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | /** 17 | * Indicates what scheduler the method or class uses by default 18 | */ 19 | public enum SchedulerKind { 20 | /** 21 | * The operator/class doesn't use schedulers. 22 | */ 23 | NONE, 24 | /** 25 | * The operator/class runs on the computation scheduler or takes timing information from it. 26 | */ 27 | COMPUTATION, 28 | /** 29 | * The operator/class runs on the io scheduler or takes timing information from it. 30 | */ 31 | IO, 32 | /** 33 | * The operator/class runs on the new thread scheduler or takes timing information from it. 34 | */ 35 | NEW_THREAD, 36 | /** 37 | * The operator/class runs on the trampoline scheduler or takes timing information from it. 38 | */ 39 | TRAMPOLINE, 40 | /** 41 | * The operator/class runs on the single scheduler or takes timing information from it. 42 | */ 43 | SINGLE, 44 | /** 45 | * The operator/class requires a scheduler to be manually specified. 46 | */ 47 | CUSTOM 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/annotations/SchedulerSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.annotations; 15 | 16 | import java.lang.annotation.*; 17 | 18 | /** 19 | * Indicates what kind of scheduler the class or method uses. 20 | */ 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Documented 23 | @Target({ElementType.METHOD, ElementType.TYPE}) 24 | public @interface SchedulerSupport { 25 | /** 26 | * The kind of scheduler the class or method uses. 27 | * @return the kind of scheduler the class or method uses 28 | */ 29 | SchedulerKind value(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/disposables/BooleanDisposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.disposables; 15 | 16 | import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; 17 | 18 | public final class BooleanDisposable implements Disposable { 19 | volatile Runnable run; 20 | 21 | static final AtomicReferenceFieldUpdater RUN = 22 | AtomicReferenceFieldUpdater.newUpdater(BooleanDisposable.class, Runnable.class, "run"); 23 | 24 | static final Runnable DISPOSED = new Runnable() { 25 | @Override 26 | public void run() { } 27 | }; 28 | 29 | public BooleanDisposable() { 30 | this(new Runnable() { 31 | @Override 32 | public void run() { } 33 | }); 34 | } 35 | 36 | public BooleanDisposable(Runnable run) { 37 | RUN.lazySet(this, run); 38 | } 39 | 40 | @Override 41 | public void dispose() { 42 | Runnable r = run; 43 | if (r != DISPOSED) { 44 | r = RUN.getAndSet(this, DISPOSED); 45 | if (r != DISPOSED) { 46 | r.run(); 47 | } 48 | } 49 | } 50 | 51 | public boolean isDisposed() { 52 | return run == DISPOSED; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/disposables/CompositeDisposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.disposables; 14 | 15 | import hu.akarnokd.rxjava2.internal.disposables.SetCompositeResource; 16 | import hu.akarnokd.rxjava2.internal.functions.Objects; 17 | 18 | /** 19 | * A disposable container that can hold onto multiple other disposables. 20 | */ 21 | public final class CompositeDisposable implements Disposable { 22 | 23 | final SetCompositeResource resources; 24 | 25 | public CompositeDisposable() { 26 | resources = new SetCompositeResource(Disposables.consumeAndDispose()); 27 | } 28 | 29 | public CompositeDisposable(Disposable... resources) { 30 | Objects.requireNonNull(resources, "resources is null"); 31 | this.resources = new SetCompositeResource(Disposables.consumeAndDispose(), resources); 32 | } 33 | 34 | public CompositeDisposable(Iterable resources) { 35 | Objects.requireNonNull(resources, "resources is null"); 36 | this.resources = new SetCompositeResource(Disposables.consumeAndDispose(), resources); 37 | } 38 | 39 | @Override 40 | public void dispose() { 41 | resources.dispose(); 42 | } 43 | 44 | public boolean isDisposed() { 45 | return resources.isDisposed(); 46 | } 47 | 48 | public void add(Disposable d) { 49 | resources.add(d); 50 | } 51 | 52 | public void remove(Disposable d) { 53 | resources.remove(d); 54 | } 55 | 56 | public void clear() { 57 | resources.clear(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/disposables/Disposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.disposables; 14 | 15 | /** 16 | * Represents a disposable resource. 17 | */ 18 | public interface Disposable { 19 | /** 20 | * Dispose the resource, the operation should be idempotent. 21 | */ 22 | void dispose(); 23 | 24 | // TODO let's see if we really need this 25 | // boolean isDisposed(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/disposables/MultipleAssignmentDisposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.disposables; 15 | 16 | import hu.akarnokd.rxjava2.internal.disposables.*; 17 | 18 | public final class MultipleAssignmentDisposable implements Disposable { 19 | final MultipleAssignmentResource resource; 20 | 21 | public MultipleAssignmentDisposable() { 22 | this.resource = new MultipleAssignmentResource(Disposables.consumeAndDispose()); 23 | } 24 | 25 | public MultipleAssignmentDisposable(Disposable initialDisposable) { 26 | this.resource = new MultipleAssignmentResource(Disposables.consumeAndDispose(), initialDisposable); 27 | } 28 | 29 | public void set(Disposable d) { 30 | this.resource.setResource(d); 31 | } 32 | 33 | public Disposable get() { 34 | Object o = resource.getResource(); 35 | if (o == null) { 36 | if (resource.isDisposed()) { 37 | return EmptyDisposable.INSTANCE; 38 | } 39 | } 40 | return (Disposable)o; 41 | } 42 | 43 | @Override 44 | public void dispose() { 45 | resource.dispose(); 46 | } 47 | 48 | public boolean isDisposed() { 49 | return resource.isDisposed(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/disposables/SerialDisposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.disposables; 15 | 16 | import hu.akarnokd.rxjava2.internal.disposables.*; 17 | 18 | public final class SerialDisposable implements Disposable { 19 | final SerialResource resource; 20 | 21 | public SerialDisposable() { 22 | this.resource = new SerialResource(Disposables.consumeAndDispose()); 23 | } 24 | 25 | public SerialDisposable(Disposable initialDisposable) { 26 | this.resource = new SerialResource(Disposables.consumeAndDispose(), initialDisposable); 27 | } 28 | 29 | 30 | public void set(Disposable d) { 31 | this.resource.setResource(d); 32 | } 33 | 34 | public Disposable get() { 35 | Object o = resource.getResource(); 36 | if (o == null) { 37 | if (resource.isDisposed()) { 38 | return EmptyDisposable.INSTANCE; 39 | } 40 | } 41 | return (Disposable)o; 42 | } 43 | 44 | @Override 45 | public void dispose() { 46 | resource.dispose(); 47 | } 48 | 49 | public boolean isDisposed() { 50 | return resource.isDisposed(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/exceptions/MissingBackpressureException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | /** 17 | * Indicates that an operator attempted to emit a value but the downstream wasn't ready for it. 18 | */ 19 | public class MissingBackpressureException extends RuntimeException { 20 | /** */ 21 | private static final long serialVersionUID = 8517344746016032542L; 22 | 23 | public MissingBackpressureException() { 24 | super(); 25 | } 26 | 27 | public MissingBackpressureException(String message) { 28 | super(message); 29 | } 30 | 31 | public MissingBackpressureException(String message, Throwable cause) { 32 | super(message, cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/exceptions/OnCompleteFailedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | public final class OnCompleteFailedException extends RuntimeException { 17 | /** */ 18 | private static final long serialVersionUID = -6179993283427447098L; 19 | 20 | public OnCompleteFailedException(Throwable cause) { 21 | super(cause); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/exceptions/OnErrorFailedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | public final class OnErrorFailedException extends RuntimeException { 17 | /** */ 18 | private static final long serialVersionUID = 2656125445290831911L; 19 | 20 | public OnErrorFailedException(Throwable cause) { 21 | super(cause); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/exceptions/OnErrorNotImplementedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | public final class OnErrorNotImplementedException extends RuntimeException { 17 | /** */ 18 | private static final long serialVersionUID = -3698670655303683299L; 19 | 20 | public OnErrorNotImplementedException() { 21 | super(); 22 | } 23 | 24 | public OnErrorNotImplementedException(String message, Throwable cause) { 25 | super(message, cause); 26 | } 27 | 28 | public OnErrorNotImplementedException(String message) { 29 | super(message); 30 | } 31 | 32 | public OnErrorNotImplementedException(Throwable cause) { 33 | super(cause); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/exceptions/UnsubscribeFailedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | public final class UnsubscribeFailedException extends RuntimeException { 17 | /** */ 18 | private static final long serialVersionUID = 8947024194181365640L; 19 | 20 | public UnsubscribeFailedException(Throwable cause) { 21 | super(cause); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/BiConsumer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface BiConsumer { 17 | 18 | void accept(T1 t1, T2 t2); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/BiFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface BiFunction { 17 | 18 | R apply(T1 t1, T2 t2); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/BiPredicate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface BiPredicate { 17 | 18 | boolean test(T1 t1, T2 t2); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/BooleanSupplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface BooleanSupplier { 17 | boolean getAsBoolean(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Consumer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Consumer { 17 | void accept(T t); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function { 17 | R apply(T t); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function3 { 17 | R apply(T1 t1, T2 t2, T3 t3); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function4.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function4 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function5.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function5 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function6.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function6 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function7.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function7 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function8.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function8 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Function9.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Function9 { 17 | R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/IntFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.functions; 14 | 15 | public interface IntFunction { 16 | T apply(int i); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/LongConsumer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.functions; 14 | 15 | public interface LongConsumer { 16 | void accept(long t); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Predicate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Predicate { 17 | boolean test(T t); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/functions/Supplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.functions; 15 | 16 | public interface Supplier { 17 | 18 | T get(); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/disposables/CompositeResource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.disposables; 15 | 16 | public interface CompositeResource { 17 | 18 | boolean add(T resource); 19 | 20 | boolean remove(T resource); 21 | 22 | boolean delete(T resource); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/disposables/EmptyDisposable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.disposables; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | 19 | public enum EmptyDisposable implements Disposable { 20 | INSTANCE 21 | ; 22 | 23 | @Override 24 | public void dispose() { 25 | // no-op 26 | } 27 | 28 | public static void complete(NbpSubscriber s) { 29 | s.onSubscribe(INSTANCE); 30 | s.onComplete(); 31 | } 32 | 33 | public static void error(Throwable e, NbpSubscriber s) { 34 | s.onSubscribe(INSTANCE); 35 | s.onError(e); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/ObservableScalarSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.Observable; 19 | import hu.akarnokd.rxjava2.functions.Function; 20 | import hu.akarnokd.rxjava2.internal.subscriptions.*; 21 | 22 | /** 23 | * Represents a constant scalar value. 24 | * @param the value type 25 | */ 26 | public final class ObservableScalarSource extends Observable { 27 | private final T value; 28 | public ObservableScalarSource(final T value) { 29 | super(new Publisher() { 30 | @Override 31 | public void subscribe(Subscriber s) { 32 | s.onSubscribe(new ScalarSubscription(s, value)); 33 | } 34 | }); 35 | this.value = value; 36 | } 37 | 38 | public T value() { 39 | return value; 40 | } 41 | 42 | public Publisher scalarFlatMap(final Function> mapper) { 43 | return new Publisher() { 44 | @Override 45 | public void subscribe(Subscriber s) { 46 | Publisher other; 47 | try { 48 | other = mapper.apply(value); 49 | } catch (Throwable e) { 50 | EmptySubscription.error(e, s); 51 | return; 52 | } 53 | if (other == null) { 54 | EmptySubscription.error(new NullPointerException("The publisher returned by the function is null"), s); 55 | return; 56 | } 57 | other.subscribe(s); 58 | } 59 | }; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/OperatorDoAfterTerminate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.Observable.Operator; 19 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 20 | 21 | public final class OperatorDoAfterTerminate implements Operator { 22 | final Runnable run; 23 | 24 | public OperatorDoAfterTerminate(Runnable run) { 25 | this.run = run; 26 | } 27 | 28 | @Override 29 | public Subscriber apply(Subscriber t) { 30 | return new DoAfterTerminateSubscriber(t, run); 31 | } 32 | 33 | static final class DoAfterTerminateSubscriber implements Subscriber { 34 | 35 | final Subscriber actual; 36 | final Runnable run; 37 | 38 | public DoAfterTerminateSubscriber(Subscriber actual, Runnable run) { 39 | this.actual = actual; 40 | this.run = run; 41 | } 42 | 43 | @Override 44 | public void onSubscribe(Subscription s) { 45 | actual.onSubscribe(s); 46 | } 47 | 48 | @Override 49 | public void onNext(T t) { 50 | actual.onNext(t); 51 | } 52 | 53 | @Override 54 | public void onError(Throwable t) { 55 | actual.onError(t); 56 | run(); 57 | } 58 | 59 | @Override 60 | public void onComplete() { 61 | actual.onComplete(); 62 | run(); 63 | } 64 | 65 | void run() { 66 | try { 67 | run.run(); 68 | } catch (Throwable ex) { 69 | RxJavaPlugins.onError(ex); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/OperatorIgnoreElements.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.Observable.Operator; 19 | 20 | public enum OperatorIgnoreElements implements Operator { 21 | INSTANCE; 22 | @SuppressWarnings("unchecked") 23 | public static Operator instance() { 24 | return (Operator)INSTANCE; 25 | } 26 | 27 | @Override 28 | public Subscriber apply(final Subscriber t) { 29 | return new Subscriber() { 30 | @Override 31 | public void onSubscribe(Subscription s) { 32 | t.onSubscribe(s); 33 | s.request(Long.MAX_VALUE); 34 | } 35 | 36 | @Override 37 | public void onNext(Object v) { 38 | // deliberately ignored 39 | } 40 | 41 | @Override 42 | public void onError(Throwable e) { 43 | t.onError(e); 44 | } 45 | 46 | @Override 47 | public void onComplete() { 48 | t.onComplete(); 49 | } 50 | }; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/OperatorSkip.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.Observable.Operator; 19 | 20 | public final class OperatorSkip implements Operator { 21 | final long n; 22 | public OperatorSkip(long n) { 23 | this.n = n; 24 | } 25 | 26 | @Override 27 | public Subscriber apply(Subscriber s) { 28 | return new SkipSubscriber(s, n); 29 | } 30 | 31 | static final class SkipSubscriber implements Subscriber { 32 | final Subscriber actual; 33 | long remaining; 34 | public SkipSubscriber(Subscriber actual, long n) { 35 | this.actual = actual; 36 | this.remaining = n; 37 | } 38 | 39 | @Override 40 | public void onSubscribe(Subscription s) { 41 | long n = remaining; 42 | actual.onSubscribe(s); 43 | s.request(n); 44 | } 45 | 46 | @Override 47 | public void onNext(T t) { 48 | if (remaining != 0L) { 49 | remaining--; 50 | } else { 51 | actual.onNext(t); 52 | } 53 | } 54 | 55 | @Override 56 | public void onError(Throwable t) { 57 | actual.onError(t); 58 | } 59 | 60 | @Override 61 | public void onComplete() { 62 | actual.onComplete(); 63 | } 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherAutoConnect.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import java.util.concurrent.atomic.AtomicInteger; 17 | 18 | import org.reactivestreams.*; 19 | 20 | import hu.akarnokd.rxjava2.disposables.Disposable; 21 | import hu.akarnokd.rxjava2.functions.Consumer; 22 | import hu.akarnokd.rxjava2.observables.ConnectableObservable; 23 | 24 | /** 25 | * Wraps a ConnectableObservable and calls its connect() method once 26 | * the specified number of Subscribers have subscribed. 27 | * 28 | * @param the value type of the chain 29 | */ 30 | public final class PublisherAutoConnect implements Publisher { 31 | final ConnectableObservable source; 32 | final int numberOfSubscribers; 33 | final Consumer connection; 34 | final AtomicInteger clients; 35 | 36 | public PublisherAutoConnect(ConnectableObservable source, 37 | int numberOfSubscribers, 38 | Consumer connection) { 39 | if (numberOfSubscribers <= 0) { 40 | throw new IllegalArgumentException("numberOfSubscribers > 0 required"); 41 | } 42 | this.source = source; 43 | this.numberOfSubscribers = numberOfSubscribers; 44 | this.connection = connection; 45 | this.clients = new AtomicInteger(); 46 | } 47 | 48 | @Override 49 | public void subscribe(Subscriber child) { 50 | source.unsafeSubscribe(child); 51 | if (clients.incrementAndGet() == numberOfSubscribers) { 52 | source.connect(connection); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherDefer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.functions.Supplier; 19 | import hu.akarnokd.rxjava2.internal.subscriptions.EmptySubscription; 20 | 21 | public final class PublisherDefer implements Publisher { 22 | final Supplier> supplier; 23 | public PublisherDefer(Supplier> supplier) { 24 | this.supplier = supplier; 25 | } 26 | @Override 27 | public void subscribe(Subscriber s) { 28 | Publisher pub; 29 | try { 30 | pub = supplier.get(); 31 | } catch (Throwable t) { 32 | EmptySubscription.error(t, s); 33 | return; 34 | } 35 | 36 | if (pub == null) { 37 | EmptySubscription.error(new NullPointerException("null publisher supplied"), s); 38 | return; 39 | } 40 | pub.subscribe(s); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherEmptySource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.internal.subscriptions.EmptySubscription; 19 | 20 | /** 21 | * 22 | */ 23 | public enum PublisherEmptySource implements Publisher { 24 | INSTANCE; 25 | 26 | @SuppressWarnings("unchecked") 27 | public static Publisher empty() { 28 | return (Publisher)INSTANCE; 29 | } 30 | 31 | @Override 32 | public void subscribe(Subscriber s) { 33 | EmptySubscription.complete(s); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherErrorSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.functions.Supplier; 19 | import hu.akarnokd.rxjava2.internal.subscriptions.EmptySubscription; 20 | 21 | public final class PublisherErrorSource implements Publisher { 22 | final Supplier errorSupplier; 23 | public PublisherErrorSource(Supplier errorSupplier) { 24 | this.errorSupplier = errorSupplier; 25 | } 26 | @Override 27 | public void subscribe(Subscriber s) { 28 | Throwable error; 29 | try { 30 | error = errorSupplier.get(); 31 | } catch (Throwable t) { 32 | error = t; 33 | return; 34 | } 35 | if (error == null) { 36 | error = new NullPointerException(); 37 | } 38 | EmptySubscription.error(error, s); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherFutureSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import java.util.concurrent.*; 17 | 18 | import org.reactivestreams.*; 19 | 20 | import hu.akarnokd.rxjava2.internal.subscriptions.ScalarAsyncSubscription; 21 | 22 | public final class PublisherFutureSource implements Publisher { 23 | final Future future; 24 | final long timeout; 25 | final TimeUnit unit; 26 | 27 | public PublisherFutureSource(Future future, long timeout, TimeUnit unit) { 28 | this.future = future; 29 | this.timeout = timeout; 30 | this.unit = unit; 31 | } 32 | 33 | @Override 34 | public void subscribe(Subscriber s) { 35 | ScalarAsyncSubscription sas = new ScalarAsyncSubscription(s); 36 | s.onSubscribe(sas); 37 | if (!sas.isComplete()) { 38 | T v; 39 | try { 40 | v = unit != null ? future.get(timeout, unit) : future.get(); 41 | } catch (Throwable ex) { 42 | if (!sas.isComplete()) { 43 | s.onError(ex); 44 | } 45 | return; 46 | } finally { 47 | future.cancel(true); // TODO ?? not sure about this 48 | } 49 | if (v == null) { 50 | s.onError(new NullPointerException("The future returned null")); 51 | } else { 52 | sas.setValue(v); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/PublisherScalarAsyncSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import java.util.concurrent.Callable; 17 | 18 | import org.reactivestreams.*; 19 | 20 | import hu.akarnokd.rxjava2.internal.subscriptions.ScalarAsyncSubscription; 21 | 22 | public final class PublisherScalarAsyncSource implements Publisher { 23 | final Callable callable; 24 | public PublisherScalarAsyncSource(Callable callable) { 25 | this.callable = callable; 26 | } 27 | @Override 28 | public void subscribe(Subscriber s) { 29 | ScalarAsyncSubscription sub = new ScalarAsyncSubscription(s); 30 | s.onSubscribe(sub); 31 | if (sub.isComplete()) { 32 | return; 33 | } 34 | T value; 35 | try { 36 | value = callable.call(); 37 | } catch (Throwable e) { 38 | if (!sub.isComplete()) { 39 | s.onError(e); 40 | } 41 | return; 42 | } 43 | if (sub.isComplete()) { 44 | return; 45 | } 46 | sub.setValue(value); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeArraySource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.*; 17 | import hu.akarnokd.rxjava2.disposables.BooleanDisposable; 18 | 19 | public final class NbpOnSubscribeArraySource implements NbpOnSubscribe { 20 | final T[] array; 21 | public NbpOnSubscribeArraySource(T[] array) { 22 | this.array = array; 23 | } 24 | public T[] array() { 25 | return array; 26 | } 27 | @Override 28 | public void accept(NbpSubscriber s) { 29 | BooleanDisposable bd = new BooleanDisposable(); 30 | 31 | s.onSubscribe(bd); 32 | 33 | T[] a = array; 34 | int n = a.length; 35 | 36 | for (int i = 0; i < n && !bd.isDisposed(); i++) { 37 | T value = a[i]; 38 | if (value == null) { 39 | s.onError(new NullPointerException("The " + i + "th element is null")); 40 | return; 41 | } 42 | s.onNext(value); 43 | } 44 | if (!bd.isDisposed()) { 45 | s.onComplete(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeAutoConnect.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import java.util.concurrent.atomic.AtomicInteger; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable.*; 19 | import hu.akarnokd.rxjava2.disposables.Disposable; 20 | import hu.akarnokd.rxjava2.functions.Consumer; 21 | import hu.akarnokd.rxjava2.observables.nbp.NbpConnectableObservable; 22 | 23 | /** 24 | * Wraps a ConnectableObservable and calls its connect() method once 25 | * the specified number of Subscribers have subscribed. 26 | * 27 | * @param the value type of the chain 28 | */ 29 | public final class NbpOnSubscribeAutoConnect implements NbpOnSubscribe { 30 | final NbpConnectableObservable source; 31 | final int numberOfSubscribers; 32 | final Consumer connection; 33 | final AtomicInteger clients; 34 | 35 | public NbpOnSubscribeAutoConnect(NbpConnectableObservable source, 36 | int numberOfSubscribers, 37 | Consumer connection) { 38 | if (numberOfSubscribers <= 0) { 39 | throw new IllegalArgumentException("numberOfSubscribers > 0 required"); 40 | } 41 | this.source = source; 42 | this.numberOfSubscribers = numberOfSubscribers; 43 | this.connection = connection; 44 | this.clients = new AtomicInteger(); 45 | } 46 | 47 | @Override 48 | public void accept(NbpSubscriber child) { 49 | source.unsafeSubscribe(child); 50 | if (clients.incrementAndGet() == numberOfSubscribers) { 51 | source.connect(connection); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeDefer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable; 17 | import hu.akarnokd.rxjava2.NbpObservable.*; 18 | import hu.akarnokd.rxjava2.functions.Supplier; 19 | import hu.akarnokd.rxjava2.internal.disposables.EmptyDisposable; 20 | 21 | public final class NbpOnSubscribeDefer implements NbpOnSubscribe { 22 | final Supplier> supplier; 23 | public NbpOnSubscribeDefer(Supplier> supplier) { 24 | this.supplier = supplier; 25 | } 26 | @Override 27 | public void accept(NbpSubscriber s) { 28 | NbpObservable pub; 29 | try { 30 | pub = supplier.get(); 31 | } catch (Throwable t) { 32 | EmptyDisposable.error(t, s); 33 | return; 34 | } 35 | 36 | if (pub == null) { 37 | EmptyDisposable.error(new NullPointerException("null publisher supplied"), s); 38 | return; 39 | } 40 | pub.subscribe(s); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeErrorSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.*; 17 | import hu.akarnokd.rxjava2.functions.Supplier; 18 | import hu.akarnokd.rxjava2.internal.disposables.EmptyDisposable; 19 | 20 | public final class NbpOnSubscribeErrorSource implements NbpOnSubscribe { 21 | final Supplier errorSupplier; 22 | public NbpOnSubscribeErrorSource(Supplier errorSupplier) { 23 | this.errorSupplier = errorSupplier; 24 | } 25 | @Override 26 | public void accept(NbpSubscriber s) { 27 | Throwable error; 28 | try { 29 | error = errorSupplier.get(); 30 | } catch (Throwable t) { 31 | error = t; 32 | return; 33 | } 34 | if (error == null) { 35 | error = new NullPointerException(); 36 | } 37 | EmptyDisposable.error(error, s); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeFutureSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import java.util.concurrent.*; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable.*; 19 | import hu.akarnokd.rxjava2.disposables.BooleanDisposable; 20 | 21 | public final class NbpOnSubscribeFutureSource implements NbpOnSubscribe { 22 | final Future future; 23 | final long timeout; 24 | final TimeUnit unit; 25 | 26 | public NbpOnSubscribeFutureSource(Future future, long timeout, TimeUnit unit) { 27 | this.future = future; 28 | this.timeout = timeout; 29 | this.unit = unit; 30 | } 31 | 32 | @Override 33 | public void accept(NbpSubscriber s) { 34 | BooleanDisposable bd = new BooleanDisposable(); 35 | s.onSubscribe(bd); 36 | if (!bd.isDisposed()) { 37 | T v; 38 | try { 39 | v = unit != null ? future.get(timeout, unit) : future.get(); 40 | } catch (Throwable ex) { 41 | if (!bd.isDisposed()) { 42 | s.onError(ex); 43 | } 44 | return; 45 | } finally { 46 | future.cancel(true); // TODO ?? not sure about this 47 | } 48 | if (!bd.isDisposed()) { 49 | if (v != null) { 50 | s.onNext(v); 51 | s.onComplete(); 52 | } else { 53 | s.onError(new NullPointerException("Future returned null")); 54 | } 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOnSubscribeScalarAsyncSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import java.util.concurrent.Callable; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable.*; 19 | import hu.akarnokd.rxjava2.disposables.BooleanDisposable; 20 | 21 | public final class NbpOnSubscribeScalarAsyncSource implements NbpOnSubscribe { 22 | final Callable callable; 23 | public NbpOnSubscribeScalarAsyncSource(Callable callable) { 24 | this.callable = callable; 25 | } 26 | @Override 27 | public void accept(NbpSubscriber s) { 28 | BooleanDisposable bd = new BooleanDisposable(); 29 | s.onSubscribe(bd); 30 | if (bd.isDisposed()) { 31 | return; 32 | } 33 | T value; 34 | try { 35 | value = callable.call(); 36 | } catch (Throwable e) { 37 | if (!bd.isDisposed()) { 38 | s.onError(e); 39 | } 40 | return; 41 | } 42 | if (bd.isDisposed()) { 43 | return; 44 | } 45 | if (value != null) { 46 | s.onNext(value); 47 | s.onComplete(); 48 | } else { 49 | s.onError(new NullPointerException("Callable returned null")); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorCount.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.*; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 19 | 20 | public enum NbpOperatorCount implements NbpOperator { 21 | INSTANCE; 22 | 23 | @SuppressWarnings("unchecked") 24 | public static NbpOperator instance() { 25 | return (NbpOperator)INSTANCE; 26 | } 27 | 28 | @Override 29 | public NbpSubscriber apply(NbpSubscriber t) { 30 | return new CountSubscriber(t); 31 | } 32 | 33 | static final class CountSubscriber implements NbpSubscriber { 34 | final NbpSubscriber actual; 35 | 36 | Disposable s; 37 | 38 | long count; 39 | 40 | public CountSubscriber(NbpSubscriber actual) { 41 | this.actual = actual; 42 | } 43 | 44 | @Override 45 | public void onSubscribe(Disposable s) { 46 | if (SubscriptionHelper.validateDisposable(this.s, s)) { 47 | return; 48 | } 49 | this.s = s; 50 | actual.onSubscribe(s); 51 | } 52 | 53 | @Override 54 | public void onNext(Object t) { 55 | count++; 56 | } 57 | 58 | @Override 59 | public void onError(Throwable t) { 60 | actual.onError(t); 61 | } 62 | 63 | @Override 64 | public void onComplete() { 65 | actual.onNext(count); 66 | actual.onComplete(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorIgnoreElements.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.*; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | 19 | public enum NbpOperatorIgnoreElements implements NbpOperator { 20 | INSTANCE; 21 | 22 | @SuppressWarnings("unchecked") 23 | public static NbpOperator instance() { 24 | return (NbpOperator)INSTANCE; 25 | } 26 | 27 | @Override 28 | public NbpSubscriber apply(final NbpSubscriber t) { 29 | return new NbpSubscriber() { 30 | @Override 31 | public void onSubscribe(Disposable s) { 32 | t.onSubscribe(s); 33 | } 34 | 35 | @Override 36 | public void onNext(Object v) { 37 | // deliberately ignored 38 | } 39 | 40 | @Override 41 | public void onError(Throwable e) { 42 | t.onError(e); 43 | } 44 | 45 | @Override 46 | public void onComplete() { 47 | t.onComplete(); 48 | } 49 | }; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorSkip.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.*; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | 19 | public final class NbpOperatorSkip implements NbpOperator { 20 | final long n; 21 | public NbpOperatorSkip(long n) { 22 | this.n = n; 23 | } 24 | 25 | @Override 26 | public NbpSubscriber apply(NbpSubscriber s) { 27 | return new SkipSubscriber(s, n); 28 | } 29 | 30 | static final class SkipSubscriber implements NbpSubscriber { 31 | final NbpSubscriber actual; 32 | long remaining; 33 | public SkipSubscriber(NbpSubscriber actual, long n) { 34 | this.actual = actual; 35 | this.remaining = n; 36 | } 37 | 38 | @Override 39 | public void onSubscribe(Disposable s) { 40 | actual.onSubscribe(s); 41 | } 42 | 43 | @Override 44 | public void onNext(T t) { 45 | if (remaining != 0L) { 46 | remaining--; 47 | } else { 48 | actual.onNext(t); 49 | } 50 | } 51 | 52 | @Override 53 | public void onError(Throwable t) { 54 | actual.onError(t); 55 | } 56 | 57 | @Override 58 | public void onComplete() { 59 | actual.onComplete(); 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorSkipLast.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import java.util.ArrayDeque; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable.*; 19 | import hu.akarnokd.rxjava2.disposables.Disposable; 20 | 21 | public final class NbpOperatorSkipLast implements NbpOperator { 22 | final int skip; 23 | 24 | public NbpOperatorSkipLast(int skip) { 25 | this.skip = skip; 26 | } 27 | 28 | @Override 29 | public NbpSubscriber apply(NbpSubscriber s) { 30 | return new SkipLastSubscriber(s, skip); 31 | } 32 | 33 | static final class SkipLastSubscriber extends ArrayDeque implements NbpSubscriber { 34 | /** */ 35 | private static final long serialVersionUID = -3807491841935125653L; 36 | final NbpSubscriber actual; 37 | final int skip; 38 | 39 | public SkipLastSubscriber(NbpSubscriber actual, int skip) { 40 | super(skip); 41 | this.actual = actual; 42 | this.skip = skip; 43 | } 44 | 45 | @Override 46 | public void onSubscribe(Disposable s) { 47 | actual.onSubscribe(s); 48 | } 49 | 50 | @Override 51 | public void onNext(T t) { 52 | if (skip == size()) { 53 | actual.onNext(poll()); 54 | } 55 | offer(t); 56 | } 57 | 58 | @Override 59 | public void onError(Throwable t) { 60 | actual.onError(t); 61 | } 62 | 63 | @Override 64 | public void onComplete() { 65 | actual.onComplete(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/operators/single/SingleOperatorMap.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.single; 15 | 16 | import hu.akarnokd.rxjava2.Single.*; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.functions.Function; 19 | 20 | public final class SingleOperatorMap implements SingleOperator { 21 | final Function mapper; 22 | 23 | public SingleOperatorMap(Function mapper) { 24 | this.mapper = mapper; 25 | } 26 | 27 | @Override 28 | public SingleSubscriber apply(final SingleSubscriber t) { 29 | return new SingleSubscriber() { 30 | @Override 31 | public void onSubscribe(Disposable d) { 32 | t.onSubscribe(d); 33 | } 34 | 35 | @Override 36 | public void onSuccess(T value) { 37 | R v; 38 | try { 39 | v = mapper.apply(value); 40 | } catch (Throwable e) { 41 | onError(e); 42 | return; 43 | } 44 | 45 | t.onSuccess(v); 46 | } 47 | 48 | @Override 49 | public void onError(Throwable e) { 50 | t.onError(e); 51 | } 52 | }; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/queue/LinkedQueueNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | /* 15 | * The code was inspired by the similarly named JCTools class: 16 | * https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/queues/atomic 17 | */ 18 | 19 | package hu.akarnokd.rxjava2.internal.queue; 20 | 21 | import java.util.concurrent.atomic.AtomicReference; 22 | 23 | public final class LinkedQueueNode extends AtomicReference> { 24 | /** */ 25 | private static final long serialVersionUID = 2404266111789071508L; 26 | private E value; 27 | LinkedQueueNode() { 28 | } 29 | LinkedQueueNode(E val) { 30 | spValue(val); 31 | } 32 | /** 33 | * Gets the current value and nulls out the reference to it from this node. 34 | * 35 | * @return value 36 | */ 37 | public E getAndNullValue() { 38 | E temp = lpValue(); 39 | spValue(null); 40 | return temp; 41 | } 42 | 43 | public E lpValue() { 44 | return value; 45 | } 46 | 47 | public void spValue(E newValue) { 48 | value = newValue; 49 | } 50 | 51 | public void soNext(LinkedQueueNode n) { 52 | lazySet(n); 53 | } 54 | 55 | public LinkedQueueNode lvNext() { 56 | return get(); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/schedulers/NewThreadScheduler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package hu.akarnokd.rxjava2.internal.schedulers; 18 | 19 | import hu.akarnokd.rxjava2.Scheduler; 20 | 21 | /** 22 | * Schedules work on a new thread. 23 | */ 24 | public final class NewThreadScheduler extends Scheduler { 25 | 26 | private static final String THREAD_NAME_PREFIX = "RxNewThreadScheduler-"; 27 | private static final RxThreadFactory THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX); 28 | private static final NewThreadScheduler INSTANCE = new NewThreadScheduler(); 29 | 30 | public static NewThreadScheduler instance() { 31 | return INSTANCE; 32 | } 33 | 34 | private NewThreadScheduler() { 35 | 36 | } 37 | 38 | @Override 39 | public Worker createWorker() { 40 | return new NewThreadWorker(THREAD_FACTORY); 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/schedulers/RxThreadFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.schedulers; 15 | 16 | import java.util.concurrent.ThreadFactory; 17 | import java.util.concurrent.atomic.AtomicLong; 18 | 19 | public final class RxThreadFactory extends AtomicLong implements ThreadFactory { 20 | /** */ 21 | private static final long serialVersionUID = -7789753024099756196L; 22 | 23 | final String prefix; 24 | 25 | public RxThreadFactory(String prefix) { 26 | this.prefix = prefix; 27 | } 28 | 29 | @Override 30 | public Thread newThread(Runnable r) { 31 | Thread t = new Thread(r, prefix + incrementAndGet()); 32 | t.setDaemon(true); 33 | return t; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "RxThreadFactory[" + prefix + "]"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/schedulers/SchedulerLifecycle.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.schedulers; 15 | 16 | public interface SchedulerLifecycle { 17 | 18 | void start(); 19 | 20 | void shutdown(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/CancelledSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 19 | 20 | /** 21 | * A subscriber that cancels the subscription sent to it 22 | * and ignores all events (onError is forwarded to RxJavaPlugins though). 23 | */ 24 | public enum CancelledSubscriber implements Subscriber { 25 | INSTANCE; 26 | 27 | @SuppressWarnings("unchecked") 28 | public static Subscriber instance() { 29 | return (Subscriber)INSTANCE; 30 | } 31 | 32 | @Override 33 | public void onSubscribe(Subscription s) { 34 | s.cancel(); 35 | } 36 | 37 | @Override 38 | public void onNext(Object t) { 39 | 40 | } 41 | 42 | @Override 43 | public void onError(Throwable t) { 44 | RxJavaPlugins.onError(t); 45 | } 46 | 47 | @Override 48 | public void onComplete() { 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/ConditionalSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import org.reactivestreams.Subscriber; 17 | 18 | /** 19 | * An Subscriber with an additional onNextIf(T) method that 20 | * tells the caller the specified value has been accepted or 21 | * not. 22 | * 23 | *

This allows certain queue-drain or source-drain operators 24 | * to avoid requesting 1 on behalf of a dropped value. 25 | * 26 | * @param the value type 27 | */ 28 | public interface ConditionalSubscriber extends Subscriber { 29 | /** 30 | * Conditionally takes the value. 31 | * @param t the value to deliver 32 | * @return true if the value has been accepted, false if the value has been rejected 33 | */ 34 | boolean onNextIf(T t); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/EmptySubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 19 | 20 | /** 21 | * A subscriber that ignores all events (onError is forwarded to RxJavaPlugins though). 22 | */ 23 | public enum EmptySubscriber implements Subscriber { 24 | /** Empty instance that reports error to the plugins. */ 25 | INSTANCE(true), 26 | /** Empty instance that doesn't report to the plugins to avoid flooding the test output. */ 27 | INSTANCE_NOERROR(false); 28 | 29 | final boolean reportError; 30 | 31 | EmptySubscriber(boolean reportError) { 32 | this.reportError = reportError; 33 | } 34 | 35 | @Override 36 | public void onSubscribe(Subscription s) { 37 | 38 | } 39 | 40 | @Override 41 | public void onNext(Object t) { 42 | 43 | } 44 | 45 | @Override 46 | public void onError(Throwable t) { 47 | if (reportError) { 48 | RxJavaPlugins.onError(t); 49 | } 50 | } 51 | 52 | @Override 53 | public void onComplete() { 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/FullArbiterSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.internal.subscriptions.*; 19 | 20 | /** 21 | * Subscriber that communicates with a FullArbiter. 22 | * 23 | * @param the value type 24 | */ 25 | public final class FullArbiterSubscriber implements Subscriber { 26 | final FullArbiter arbiter; 27 | 28 | Subscription s; 29 | 30 | public FullArbiterSubscriber(FullArbiter arbiter) { 31 | this.arbiter = arbiter; 32 | } 33 | 34 | @Override 35 | public void onSubscribe(Subscription s) { 36 | if (SubscriptionHelper.validateSubscription(this.s, s)) { 37 | return; 38 | } 39 | this.s = s; 40 | arbiter.setSubscription(s); 41 | } 42 | 43 | @Override 44 | public void onNext(T t) { 45 | arbiter.onNext(t, s); 46 | } 47 | 48 | @Override 49 | public void onError(Throwable t) { 50 | arbiter.onError(t, s); 51 | } 52 | 53 | @Override 54 | public void onComplete() { 55 | arbiter.onComplete(s); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/NbpFullArbiterSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.internal.disposables.NbpFullArbiter; 19 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 20 | 21 | /** 22 | * Subscriber that communicates with a FullArbiter. 23 | * 24 | * @param the value type 25 | */ 26 | public final class NbpFullArbiterSubscriber implements NbpSubscriber { 27 | final NbpFullArbiter arbiter; 28 | 29 | Disposable s; 30 | 31 | public NbpFullArbiterSubscriber(NbpFullArbiter arbiter) { 32 | this.arbiter = arbiter; 33 | } 34 | 35 | @Override 36 | public void onSubscribe(Disposable s) { 37 | if (SubscriptionHelper.validateDisposable(this.s, s)) { 38 | return; 39 | } 40 | this.s = s; 41 | arbiter.setSubscription(s); 42 | } 43 | 44 | @Override 45 | public void onNext(T t) { 46 | arbiter.onNext(t, s); 47 | } 48 | 49 | @Override 50 | public void onError(Throwable t) { 51 | arbiter.onError(t, s); 52 | } 53 | 54 | @Override 55 | public void onComplete() { 56 | arbiter.onComplete(s); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/ToNotificationSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers; 15 | 16 | import org.reactivestreams.*; 17 | 18 | import hu.akarnokd.rxjava2.*; 19 | import hu.akarnokd.rxjava2.functions.Consumer; 20 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 21 | 22 | public final class ToNotificationSubscriber implements Subscriber { 23 | final Consumer>> consumer; 24 | 25 | Subscription s; 26 | 27 | public ToNotificationSubscriber(Consumer>> consumer) { 28 | this.consumer = consumer; 29 | } 30 | 31 | @Override 32 | public void onSubscribe(Subscription s) { 33 | if (SubscriptionHelper.validateSubscription(this.s, s)) { 34 | return; 35 | } 36 | this.s = s; 37 | s.request(Long.MAX_VALUE); 38 | } 39 | 40 | @Override 41 | public void onNext(T t) { 42 | if (t == null) { 43 | s.cancel(); 44 | onError(new NullPointerException()); 45 | } else { 46 | consumer.accept(Try.ofValue(Optional.of(t))); 47 | } 48 | } 49 | 50 | @Override 51 | public void onError(Throwable t) { 52 | consumer.accept(Try.>ofError(t)); 53 | } 54 | 55 | @Override 56 | public void onComplete() { 57 | consumer.accept(Notification.complete()); 58 | } 59 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/nbp/NbpCancelledSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 19 | 20 | /** 21 | * A subscriber that cancels the subscription sent to it 22 | * and ignores all events (onError is forwarded to RxJavaPlugins though). 23 | */ 24 | public enum NbpCancelledSubscriber implements NbpSubscriber { 25 | INSTANCE; 26 | 27 | @SuppressWarnings("unchecked") 28 | public static NbpSubscriber instance() { 29 | return (NbpSubscriber)INSTANCE; 30 | } 31 | 32 | @Override 33 | public void onSubscribe(Disposable s) { 34 | s.dispose(); 35 | } 36 | 37 | @Override 38 | public void onNext(Object t) { 39 | 40 | } 41 | 42 | @Override 43 | public void onError(Throwable t) { 44 | RxJavaPlugins.onError(t); 45 | } 46 | 47 | @Override 48 | public void onComplete() { 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/nbp/NbpConditionalSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | 18 | /** 19 | * An Subscriber with an additional onNextIf(T) method that 20 | * tells the caller the specified value has been accepted or 21 | * not. 22 | * 23 | *

This allows certain queue-drain or source-drain operators 24 | * to avoid requesting 1 on behalf of a dropped value. 25 | * 26 | * @param the value type 27 | */ 28 | public interface NbpConditionalSubscriber extends NbpSubscriber { 29 | /** 30 | * Conditionally takes the value. 31 | * @param t the value to deliver 32 | * @return true if the value has been accepted, false if the value has been rejected 33 | */ 34 | boolean onNextIf(T t); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/nbp/NbpDisposableSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers.nbp; 15 | 16 | import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 19 | import hu.akarnokd.rxjava2.disposables.Disposable; 20 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 21 | 22 | /** 23 | * An abstract subscription that allows asynchronous cancellation. 24 | * 25 | * @param 26 | */ 27 | public abstract class NbpDisposableSubscriber implements NbpSubscriber, Disposable { 28 | volatile Disposable s; 29 | @SuppressWarnings("rawtypes") 30 | static final AtomicReferenceFieldUpdater S = 31 | AtomicReferenceFieldUpdater.newUpdater(NbpDisposableSubscriber.class, Disposable.class, "s"); 32 | 33 | static final Disposable CANCELLED = new Disposable() { 34 | @Override 35 | public void dispose() { } 36 | }; 37 | 38 | @Override 39 | public final void onSubscribe(Disposable s) { 40 | if (!S.compareAndSet(this, null, s)) { 41 | s.dispose(); 42 | if (this.s != CANCELLED) { 43 | SubscriptionHelper.reportSubscriptionSet(); 44 | } 45 | return; 46 | } 47 | onStart(); 48 | } 49 | 50 | protected void onStart() { 51 | } 52 | 53 | public final boolean isDisposed() { 54 | return s == CANCELLED; 55 | } 56 | 57 | @Override 58 | public final void dispose() { 59 | Disposable a = s; 60 | if (a != CANCELLED) { 61 | a = S.getAndSet(this, CANCELLED); 62 | if (a != CANCELLED && a != null) { 63 | a.dispose(); 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/nbp/NbpEmptySubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers.nbp; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | import hu.akarnokd.rxjava2.disposables.Disposable; 18 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 19 | 20 | /** 21 | * A subscriber that ignores all events (onError is forwarded to RxJavaPlugins though). 22 | */ 23 | public enum NbpEmptySubscriber implements NbpSubscriber { 24 | /** Empty instance that reports error to the plugins. */ 25 | INSTANCE(true), 26 | /** Empty instance that doesn't report to the plugins to avoid flooding the test output. */ 27 | INSTANCE_NOERROR(false); 28 | 29 | final boolean reportError; 30 | 31 | NbpEmptySubscriber(boolean reportError) { 32 | this.reportError = reportError; 33 | } 34 | 35 | @Override 36 | public void onSubscribe(Disposable s) { 37 | 38 | } 39 | 40 | @Override 41 | public void onNext(Object t) { 42 | 43 | } 44 | 45 | @Override 46 | public void onError(Throwable t) { 47 | if (reportError) { 48 | RxJavaPlugins.onError(t); 49 | } 50 | } 51 | 52 | @Override 53 | public void onComplete() { 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscribers/nbp/NbpToNotificationSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscribers.nbp; 15 | 16 | import hu.akarnokd.rxjava2.*; 17 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 18 | import hu.akarnokd.rxjava2.disposables.Disposable; 19 | import hu.akarnokd.rxjava2.functions.Consumer; 20 | import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper; 21 | 22 | public final class NbpToNotificationSubscriber implements NbpSubscriber { 23 | final Consumer>> consumer; 24 | 25 | Disposable s; 26 | 27 | public NbpToNotificationSubscriber(Consumer>> consumer) { 28 | this.consumer = consumer; 29 | } 30 | 31 | @Override 32 | public void onSubscribe(Disposable s) { 33 | if (SubscriptionHelper.validateDisposable(this.s, s)) { 34 | return; 35 | } 36 | this.s = s; 37 | } 38 | 39 | @Override 40 | public void onNext(T t) { 41 | if (t == null) { 42 | s.dispose(); 43 | onError(new NullPointerException()); 44 | } else { 45 | consumer.accept(Try.ofValue(Optional.of(t))); 46 | } 47 | } 48 | 49 | @Override 50 | public void onError(Throwable t) { 51 | consumer.accept(Try.>ofError(t)); 52 | } 53 | 54 | @Override 55 | public void onComplete() { 56 | consumer.accept(Notification.complete()); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscriptions/BooleanSubscription.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.internal.subscriptions; 14 | 15 | import java.util.concurrent.atomic.AtomicBoolean; 16 | 17 | import org.reactivestreams.Subscription; 18 | 19 | /** 20 | * Subscription implementation that ignores request but remembers the cancellation 21 | * which can be checked via isCancelled. 22 | */ 23 | public final class BooleanSubscription extends AtomicBoolean implements Subscription { 24 | /** */ 25 | private static final long serialVersionUID = -8127758972444290902L; 26 | 27 | @Override 28 | public void request(long n) { 29 | if (SubscriptionHelper.validateRequest(n)) { 30 | return; 31 | } 32 | } 33 | 34 | @Override 35 | public void cancel() { 36 | lazySet(true); 37 | } 38 | 39 | /** 40 | * Returns true if this BooleanSubscription has been cancelled. 41 | * @return true if this BooleanSubscription has been cancelled 42 | */ 43 | public boolean isCancelled() { 44 | return get(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscriptions/EmptySubscription.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscriptions; 15 | 16 | import org.reactivestreams.*; 17 | 18 | /** 19 | * An empty subscription that does nothing other than validates the request amount. 20 | */ 21 | public enum EmptySubscription implements Subscription { 22 | /** A singleton, stateless instance. */ 23 | INSTANCE; 24 | 25 | @Override 26 | public void request(long n) { 27 | SubscriptionHelper.validateRequest(n); 28 | } 29 | @Override 30 | public void cancel() { 31 | // no-op 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "EmptySubscription"; 37 | } 38 | 39 | /** 40 | * Sets the empty subscription instance on the subscriber and then 41 | * calls onError with the supplied error. 42 | * 43 | *

Make sure this is only called if the subscriber hasn't received a 44 | * subscription already (there is no way of telling this). 45 | * 46 | * @param e the error to deliver to the subscriber 47 | * @param s the target subscriber 48 | */ 49 | public static void error(Throwable e, Subscriber s) { 50 | s.onSubscribe(INSTANCE); 51 | s.onError(e); 52 | } 53 | 54 | /** 55 | * Sets the empty subscription instance on the subscriber and then 56 | * calls onComplete. 57 | * 58 | *

Make sure this is only called if the subscriber hasn't received a 59 | * subscription already (there is no way of telling this). 60 | * 61 | * @param s the target subscriber 62 | */ 63 | public static void complete(Subscriber s) { 64 | s.onSubscribe(INSTANCE); 65 | s.onComplete(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/subscriptions/ScalarSubscription.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.subscriptions; 15 | 16 | import java.util.concurrent.atomic.AtomicBoolean; 17 | 18 | import org.reactivestreams.*; 19 | 20 | import hu.akarnokd.rxjava2.plugins.RxJavaPlugins; 21 | 22 | /** 23 | * A Subscription that holds a constant value and emits it only when requested. 24 | * @param the value type 25 | */ 26 | public final class ScalarSubscription extends AtomicBoolean implements Subscription { 27 | /** */ 28 | private static final long serialVersionUID = -3830916580126663321L; 29 | /** The single value to emit, set to null. */ 30 | private final T value; 31 | /** The actual subscriber. */ 32 | private final Subscriber subscriber; 33 | 34 | public ScalarSubscription(Subscriber subscriber, T value) { 35 | this.subscriber = subscriber; 36 | this.value = value; 37 | } 38 | 39 | @Override 40 | public void request(long n) { 41 | if (n <= 0) { 42 | RxJavaPlugins.onError(new IllegalArgumentException("n > 0 required but it was " + n)); 43 | return; 44 | } 45 | if (compareAndSet(false, true)) { 46 | T v = value; 47 | Subscriber s = subscriber; 48 | 49 | s.onNext(v); 50 | s.onComplete(); 51 | } 52 | 53 | } 54 | 55 | @Override 56 | public void cancel() { 57 | lazySet(true); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/util/Exceptions.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.util; 15 | 16 | public enum Exceptions { 17 | ; 18 | /** 19 | * Convenience method to throw a {@code RuntimeException} and {@code Error} directly 20 | * or wrap any other exception type into a {@code RuntimeException}. 21 | * @param t the exception to throw directly or wrapped 22 | * @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return 23 | * value; {@code propagate} does not actually return anything 24 | */ 25 | public static RuntimeException propagate(Throwable t) { 26 | /* 27 | * The return type of RuntimeException is a trick for code to be like this: 28 | * 29 | * throw Exceptions.propagate(e); 30 | * 31 | * Even though nothing will return and throw via that 'throw', it allows the code to look like it 32 | * so it's easy to read and understand that it will always result in a throw. 33 | */ 34 | if (t instanceof RuntimeException) { 35 | throw (RuntimeException) t; 36 | } else if (t instanceof Error) { 37 | throw (Error) t; 38 | } else { 39 | throw new RuntimeException(t); 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/util/NbpQueueDrain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.util; 15 | 16 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 17 | 18 | public interface NbpQueueDrain { 19 | 20 | boolean cancelled(); 21 | 22 | boolean done(); 23 | 24 | Throwable error(); 25 | 26 | boolean enter(); 27 | 28 | /** 29 | * Adds m to the wip counter. 30 | * @param m the value to add 31 | * @return the wip value after adding the value 32 | */ 33 | int leave(int m); 34 | 35 | /** 36 | * Accept the value and return true if forwarded. 37 | * @param a the subscriber to deliver values to 38 | * @param v the value to deliver 39 | */ 40 | void accept(NbpSubscriber a, T v); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/util/Pow2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | 15 | /* 16 | * Original License: https://github.com/JCTools/JCTools/blob/master/LICENSE 17 | * Original location: https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/util/Pow2.java 18 | */ 19 | package hu.akarnokd.rxjava2.internal.util; 20 | 21 | public final class Pow2 { 22 | private Pow2() { 23 | throw new IllegalStateException("No instances!"); 24 | } 25 | 26 | /** 27 | * Find the next larger positive power of two value up from the given value. If value is a power of two then 28 | * this value will be returned. 29 | * 30 | * @param value from which next positive power of two will be found. 31 | * @return the next positive power of 2 or this value if it is a power of 2. 32 | */ 33 | public static int roundToPowerOfTwo(final int value) { 34 | return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); 35 | } 36 | 37 | /** 38 | * Is this value a power of two. 39 | * 40 | * @param value to be tested to see if it is a power of two. 41 | * @return true if the value is a power of 2 otherwise false. 42 | */ 43 | public static boolean isPowerOfTwo(final int value) { 44 | return (value & (value - 1)) == 0; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/internal/util/QueueDrain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.util; 15 | 16 | import org.reactivestreams.Subscriber; 17 | 18 | public interface QueueDrain { 19 | 20 | boolean cancelled(); 21 | 22 | boolean done(); 23 | 24 | Throwable error(); 25 | 26 | boolean enter(); 27 | 28 | long requested(); 29 | 30 | long produced(long n); 31 | 32 | /** 33 | * Adds m to the wip counter. 34 | * @param m the value to add 35 | * @return the current value after adding m 36 | */ 37 | int leave(int m); 38 | 39 | /** 40 | * Accept the value and return true if forwarded. 41 | * @param a the subscriber 42 | * @param v the value 43 | * @return true if the value was delivered 44 | */ 45 | boolean accept(Subscriber a, T v); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/observables/GroupedObservable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.observables; 14 | 15 | import org.reactivestreams.Publisher; 16 | 17 | import hu.akarnokd.rxjava2.Observable; 18 | 19 | public class GroupedObservable extends Observable { 20 | final K key; 21 | protected GroupedObservable(Publisher onSubscribe, K key) { 22 | super(onSubscribe); 23 | this.key = key; 24 | } 25 | 26 | // FIXME short one or long one? 27 | public K key() { 28 | return key; 29 | } 30 | 31 | public K getKey() { 32 | return key; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/observables/nbp/NbpGroupedObservable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package hu.akarnokd.rxjava2.observables.nbp; 14 | 15 | import hu.akarnokd.rxjava2.NbpObservable; 16 | 17 | public class NbpGroupedObservable extends NbpObservable { 18 | final K key; 19 | protected NbpGroupedObservable(NbpOnSubscribe onSubscribe, K key) { 20 | super(onSubscribe); 21 | this.key = key; 22 | } 23 | 24 | // FIXME short one or long one? 25 | public K key() { 26 | return key; 27 | } 28 | 29 | public K getKey() { 30 | return key; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/schedulers/Timed.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.schedulers; 15 | 16 | import java.util.concurrent.TimeUnit; 17 | 18 | import hu.akarnokd.rxjava2.internal.functions.Objects; 19 | 20 | /** 21 | * Holds onto a value along with time information. 22 | * 23 | * @param the value type 24 | */ 25 | public final class Timed { 26 | final T value; 27 | final long time; 28 | final TimeUnit unit; 29 | public Timed(T value, long time, TimeUnit unit) { 30 | this.value = value; 31 | this.time = time; 32 | this.unit = Objects.requireNonNull(unit, "unit is null"); 33 | } 34 | 35 | public T value() { 36 | return value; 37 | } 38 | 39 | public TimeUnit unit() { 40 | return unit; 41 | } 42 | 43 | public long time() { 44 | return time; 45 | } 46 | 47 | public long time(TimeUnit unit) { 48 | return unit.convert(time, this.unit); 49 | } 50 | 51 | @Override 52 | public boolean equals(Object other) { 53 | if (other instanceof Timed) { 54 | Timed o = (Timed) other; 55 | return Objects.equals(value, o.value) 56 | && time == o.time 57 | && Objects.equals(unit, o.unit); 58 | } 59 | return false; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "Timed[time=" + time + ", unit=" + unit + ", value=" + value + "]"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/hu/akarnokd/rxjava2/subscribers/completable/CompletableSerializedSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.subscribers.completable; 15 | 16 | import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; 17 | 18 | import hu.akarnokd.rxjava2.Completable.CompletableSubscriber; 19 | import hu.akarnokd.rxjava2.disposables.Disposable; 20 | 21 | public final class CompletableSerializedSubscriber implements CompletableSubscriber { 22 | 23 | final CompletableSubscriber actual; 24 | 25 | volatile int once; 26 | static final AtomicIntegerFieldUpdater ONCE = 27 | AtomicIntegerFieldUpdater.newUpdater(CompletableSerializedSubscriber.class, "once"); 28 | 29 | public CompletableSerializedSubscriber(CompletableSubscriber actual) { 30 | this.actual = actual; 31 | } 32 | 33 | @Override 34 | public void onSubscribe(Disposable d) { 35 | actual.onSubscribe(d); 36 | } 37 | 38 | @Override 39 | public void onError(Throwable e) { 40 | if (ONCE.compareAndSet(this, 0, 1)) { 41 | actual.onError(e); 42 | } 43 | } 44 | 45 | @Override 46 | public void onComplete() { 47 | if (ONCE.compareAndSet(this, 0, 1)) { 48 | actual.onComplete(); 49 | } 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/NbpObservableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import java.util.*; 17 | 18 | import org.junit.*; 19 | 20 | import hu.akarnokd.rxjava2.functions.Function; 21 | 22 | public class NbpObservableTest { 23 | @Test 24 | public void testFlatMap() { 25 | List list = NbpObservable.range(1, 5).flatMap(new Function>() { 26 | @Override 27 | public NbpObservable apply(Integer v) { 28 | return NbpObservable.range(v, 2); 29 | } 30 | }).getList(); 31 | 32 | Assert.assertEquals(Arrays.asList(1, 2, 2, 3, 3, 4, 4, 5, 5, 6), list); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/ObservableDoOnTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | import java.util.concurrent.atomic.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.functions.Consumer; 23 | 24 | public class ObservableDoOnTest { 25 | 26 | @Test 27 | public void testDoOnEach() { 28 | final AtomicReference r = new AtomicReference(); 29 | String output = Observable.just("one").doOnNext(new Consumer() { 30 | @Override 31 | public void accept(String v) { 32 | r.set(v); 33 | } 34 | }).toBlocking().single(); 35 | 36 | assertEquals("one", output); 37 | assertEquals("one", r.get()); 38 | } 39 | 40 | @Test 41 | public void testDoOnError() { 42 | final AtomicReference r = new AtomicReference(); 43 | Throwable t = null; 44 | try { 45 | Observable. error(new RuntimeException("an error")) 46 | .doOnError(new Consumer() { 47 | @Override 48 | public void accept(Throwable v) { 49 | r.set(v); 50 | } 51 | }).toBlocking().single(); 52 | fail("expected exception, not a return value"); 53 | } catch (Throwable e) { 54 | t = e; 55 | } 56 | 57 | assertNotNull(t); 58 | assertEquals(t, r.get()); 59 | } 60 | 61 | @Test 62 | public void testDoOnCompleted() { 63 | final AtomicBoolean r = new AtomicBoolean(); 64 | String output = Observable.just("one").doOnComplete(new Runnable() { 65 | @Override 66 | public void run() { 67 | r.set(true); 68 | } 69 | }).toBlocking().single(); 70 | 71 | assertEquals("one", output); 72 | assertTrue(r.get()); 73 | } 74 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/ObservableWindowTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | import java.util.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.functions.*; 23 | 24 | public class ObservableWindowTests { 25 | 26 | @Test 27 | public void testWindow() { 28 | final ArrayList> lists = new ArrayList>(); 29 | 30 | Observable.concat( 31 | Observable.just(1, 2, 3, 4, 5, 6) 32 | .window(3) 33 | .map(new Function, Observable>>() { 34 | @Override 35 | public Observable> apply(Observable xs) { 36 | return xs.toList(); 37 | } 38 | }) 39 | ) 40 | .toBlocking().forEach(new Consumer>() { 41 | @Override 42 | public void accept(List xs) { 43 | lists.add(xs); 44 | } 45 | }); 46 | 47 | assertArrayEquals(lists.get(0).toArray(new Integer[3]), new Integer[] { 1, 2, 3 }); 48 | assertArrayEquals(lists.get(1).toArray(new Integer[3]), new Integer[] { 4, 5, 6 }); 49 | assertEquals(2, lists.size()); 50 | 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/ScanTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import java.util.HashMap; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.EventStream.Event; 21 | import hu.akarnokd.rxjava2.functions.*; 22 | 23 | public class ScanTests { 24 | 25 | @Test 26 | public void testUnsubscribeScan() { 27 | 28 | EventStream.getEventStream("HTTP-ClusterB", 20) 29 | .scan(new HashMap(), new BiFunction, Event, HashMap>() { 30 | @Override 31 | public HashMap apply(HashMap accum, Event perInstanceEvent) { 32 | accum.put("instance", perInstanceEvent.instanceId); 33 | return accum; 34 | } 35 | }) 36 | .take(10) 37 | .toBlocking() 38 | .forEach(new Consumer>() { 39 | @Override 40 | public void accept(HashMap v) { 41 | System.out.println(v); 42 | } 43 | }); 44 | } 45 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/StartWithTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | import java.util.*; 19 | 20 | import org.junit.Test; 21 | 22 | public class StartWithTests { 23 | 24 | @Test 25 | public void startWith1() { 26 | List values = Observable.just("one", "two") 27 | .startWithArray("zero").toList().toBlocking().single(); 28 | 29 | assertEquals("zero", values.get(0)); 30 | assertEquals("two", values.get(2)); 31 | } 32 | 33 | @Test 34 | public void startWithIterable() { 35 | List li = new ArrayList(); 36 | li.add("alpha"); 37 | li.add("beta"); 38 | List values = Observable.just("one", "two").startWith(li).toList().toBlocking().single(); 39 | 40 | assertEquals("alpha", values.get(0)); 41 | assertEquals("beta", values.get(1)); 42 | assertEquals("one", values.get(2)); 43 | assertEquals("two", values.get(3)); 44 | } 45 | 46 | @Test 47 | public void startWithObservable() { 48 | List li = new ArrayList(); 49 | li.add("alpha"); 50 | li.add("beta"); 51 | List values = Observable.just("one", "two") 52 | .startWith(Observable.fromIterable(li)) 53 | .toList() 54 | .toBlocking() 55 | .single(); 56 | 57 | assertEquals("alpha", values.get(0)); 58 | assertEquals("beta", values.get(1)); 59 | assertEquals("one", values.get(2)); 60 | assertEquals("two", values.get(3)); 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/TestHelper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.mockito.Matchers.any; 17 | import static org.mockito.Mockito.mock; 18 | 19 | import org.mockito.Mockito; 20 | import org.mockito.invocation.InvocationOnMock; 21 | import org.mockito.stubbing.Answer; 22 | import org.reactivestreams.*; 23 | 24 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 25 | 26 | /** 27 | * Common methods for helping with tests from 1.x mostly. 28 | */ 29 | public enum TestHelper { 30 | ; 31 | /** 32 | * Mocks a subscriber and prepares it to request Long.MAX_VALUE. 33 | * @param the value type 34 | * @return the mocked subscriber 35 | */ 36 | @SuppressWarnings("unchecked") 37 | public static Subscriber mockSubscriber() { 38 | Subscriber w = mock(Subscriber.class); 39 | 40 | Mockito.doAnswer(new Answer() { 41 | @Override 42 | public Object answer(InvocationOnMock a) throws Throwable { 43 | Subscription s = a.getArgumentAt(0, Subscription.class); 44 | s.request(Long.MAX_VALUE); 45 | return null; 46 | } 47 | }).when(w).onSubscribe((Subscription)any()); 48 | 49 | return w; 50 | } 51 | 52 | @SuppressWarnings("unchecked") 53 | public static NbpSubscriber mockNbpSubscriber() { 54 | return mock(NbpSubscriber.class); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/ThrottleLastTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.mockito.Mockito.inOrder; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.Test; 21 | import org.mockito.InOrder; 22 | import org.reactivestreams.Subscriber; 23 | 24 | import hu.akarnokd.rxjava2.schedulers.TestScheduler; 25 | import hu.akarnokd.rxjava2.subjects.PublishSubject; 26 | 27 | public class ThrottleLastTests { 28 | 29 | @Test 30 | public void testThrottle() { 31 | Subscriber observer = TestHelper.mockSubscriber(); 32 | 33 | TestScheduler s = new TestScheduler(); 34 | PublishSubject o = PublishSubject.create(); 35 | o.throttleLast(500, TimeUnit.MILLISECONDS, s).subscribe(observer); 36 | 37 | // send events with simulated time increments 38 | s.advanceTimeTo(0, TimeUnit.MILLISECONDS); 39 | o.onNext(1); // skip 40 | o.onNext(2); // deliver 41 | s.advanceTimeTo(501, TimeUnit.MILLISECONDS); 42 | o.onNext(3); // skip 43 | s.advanceTimeTo(600, TimeUnit.MILLISECONDS); 44 | o.onNext(4); // skip 45 | s.advanceTimeTo(700, TimeUnit.MILLISECONDS); 46 | o.onNext(5); // skip 47 | o.onNext(6); // deliver 48 | s.advanceTimeTo(1001, TimeUnit.MILLISECONDS); 49 | o.onNext(7); // deliver 50 | s.advanceTimeTo(1501, TimeUnit.MILLISECONDS); 51 | o.onComplete(); 52 | 53 | InOrder inOrder = inOrder(observer); 54 | inOrder.verify(observer).onNext(2); 55 | inOrder.verify(observer).onNext(6); 56 | inOrder.verify(observer).onNext(7); 57 | inOrder.verify(observer).onComplete(); 58 | inOrder.verifyNoMoreInteractions(); 59 | } 60 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/ThrottleWithTimeoutTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2; 15 | 16 | import static org.mockito.Mockito.inOrder; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.Test; 21 | import org.mockito.InOrder; 22 | import org.reactivestreams.Subscriber; 23 | 24 | import hu.akarnokd.rxjava2.schedulers.TestScheduler; 25 | import hu.akarnokd.rxjava2.subjects.PublishSubject; 26 | 27 | public class ThrottleWithTimeoutTests { 28 | 29 | @Test 30 | public void testThrottle() { 31 | Subscriber observer = TestHelper.mockSubscriber(); 32 | 33 | TestScheduler s = new TestScheduler(); 34 | PublishSubject o = PublishSubject.create(); 35 | o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s) 36 | .subscribe(observer); 37 | 38 | // send events with simulated time increments 39 | s.advanceTimeTo(0, TimeUnit.MILLISECONDS); 40 | o.onNext(1); // skip 41 | o.onNext(2); // deliver 42 | s.advanceTimeTo(501, TimeUnit.MILLISECONDS); 43 | o.onNext(3); // skip 44 | s.advanceTimeTo(600, TimeUnit.MILLISECONDS); 45 | o.onNext(4); // skip 46 | s.advanceTimeTo(700, TimeUnit.MILLISECONDS); 47 | o.onNext(5); // skip 48 | o.onNext(6); // deliver at 1300 after 500ms has passed since onNext(5) 49 | s.advanceTimeTo(1300, TimeUnit.MILLISECONDS); 50 | o.onNext(7); // deliver 51 | s.advanceTimeTo(1800, TimeUnit.MILLISECONDS); 52 | o.onComplete(); 53 | 54 | InOrder inOrder = inOrder(observer); 55 | inOrder.verify(observer).onNext(2); 56 | inOrder.verify(observer).onNext(6); 57 | inOrder.verify(observer).onNext(7); 58 | inOrder.verify(observer).onComplete(); 59 | inOrder.verifyNoMoreInteractions(); 60 | } 61 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/disposables/DisposablesTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.disposables; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.Test; 19 | 20 | 21 | public class DisposablesTest { 22 | 23 | @Test 24 | public void testUnsubscribeOnlyOnce() { 25 | Runnable dispose = mock(Runnable.class); 26 | Disposable subscription = Disposables.from(dispose); 27 | subscription.dispose(); 28 | subscription.dispose(); 29 | verify(dispose, times(1)).run(); 30 | } 31 | 32 | @Test 33 | public void testEmpty() { 34 | Disposable empty = Disposables.empty(); 35 | // FIXME not assertable 36 | // assertFalse(empty.isUnsubscribed()); 37 | empty.dispose(); 38 | // FIXME not assertable 39 | // assertTrue(empty.isUnsubscribed()); 40 | } 41 | 42 | @Test 43 | public void testUnsubscribed() { 44 | // FIXME not assertable 45 | /*Disposable disposed = */Disposables.disposed(); 46 | // assertTrue(disposed.isUnsubscribed()); 47 | } 48 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/disposables/MultipleAssignmentDisposableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.disposables; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.*; 19 | 20 | public class MultipleAssignmentDisposableTest { 21 | 22 | Runnable dispose; 23 | Disposable s; 24 | 25 | @Before 26 | public void before() { 27 | dispose = mock(Runnable.class); 28 | s = Disposables.from(dispose); 29 | } 30 | 31 | @Test 32 | public void testNoUnsubscribeWhenReplaced() { 33 | MultipleAssignmentDisposable mas = new MultipleAssignmentDisposable(); 34 | 35 | mas.set(s); 36 | mas.set(Disposables.empty()); 37 | mas.dispose(); 38 | 39 | verify(dispose, never()).run(); 40 | } 41 | 42 | @Test 43 | public void testUnsubscribeWhenParentUnsubscribes() { 44 | MultipleAssignmentDisposable mas = new MultipleAssignmentDisposable(); 45 | mas.set(s); 46 | mas.dispose(); 47 | mas.dispose(); 48 | 49 | verify(dispose, times(1)).run(); 50 | 51 | Assert.assertEquals(true, mas.isDisposed()); 52 | } 53 | 54 | @Test 55 | public void subscribingWhenUnsubscribedCausesImmediateUnsubscription() { 56 | MultipleAssignmentDisposable mas = new MultipleAssignmentDisposable(); 57 | mas.dispose(); 58 | Disposable underlying = mock(Disposable.class); 59 | mas.set(underlying); 60 | verify(underlying).dispose(); 61 | } 62 | 63 | @Test 64 | public void testDisposableRemainsAfterUnsubscribe() { 65 | MultipleAssignmentDisposable mas = new MultipleAssignmentDisposable(); 66 | 67 | mas.set(s); 68 | mas.dispose(); 69 | // FIXME MultipleAssignmentDisposable no longer retains the reference after diposing 70 | Assert.assertNotSame(s, mas.get()); 71 | } 72 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/exceptions/TestException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.exceptions; 15 | 16 | public final class TestException extends RuntimeException { 17 | /** */ 18 | private static final long serialVersionUID = -1438148770465406172L; 19 | 20 | /** 21 | * 22 | */ 23 | public TestException() { 24 | super(); 25 | } 26 | 27 | /** 28 | * @param message 29 | * @param cause 30 | */ 31 | public TestException(String message, Throwable cause) { 32 | super(message, cause); 33 | } 34 | 35 | /** 36 | * @param message 37 | */ 38 | public TestException(String message) { 39 | super(message); 40 | } 41 | 42 | /** 43 | * @param cause 44 | */ 45 | public TestException(Throwable cause) { 46 | super(cause); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/BackpressureHelperTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.internal.util.BackpressureHelper; 21 | 22 | public class BackpressureHelperTest { 23 | @Test 24 | public void testAddCap() { 25 | assertEquals(2L, BackpressureHelper.addCap(1, 1)); 26 | assertEquals(Long.MAX_VALUE, BackpressureHelper.addCap(1, Long.MAX_VALUE - 1)); 27 | assertEquals(Long.MAX_VALUE, BackpressureHelper.addCap(1, Long.MAX_VALUE)); 28 | assertEquals(Long.MAX_VALUE, BackpressureHelper.addCap(Long.MAX_VALUE - 1, Long.MAX_VALUE - 1)); 29 | assertEquals(Long.MAX_VALUE, BackpressureHelper.addCap(Long.MAX_VALUE, Long.MAX_VALUE)); 30 | } 31 | 32 | @Test 33 | public void testMultiplyCap() { 34 | assertEquals(6, BackpressureHelper.multiplyCap(2, 3)); 35 | assertEquals(Long.MAX_VALUE, BackpressureHelper.multiplyCap(2, Long.MAX_VALUE)); 36 | assertEquals(Long.MAX_VALUE, BackpressureHelper.multiplyCap(Long.MAX_VALUE, Long.MAX_VALUE)); 37 | assertEquals(Long.MAX_VALUE, BackpressureHelper.multiplyCap(1L << 32, 1L << 32)); 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/NotificationLiteTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.internal.util.NotificationLite; 21 | 22 | 23 | public class NotificationLiteTest { 24 | 25 | @Test 26 | public void testComplete() { 27 | Object n = NotificationLite.next("Hello"); 28 | Object c = NotificationLite.complete(); 29 | 30 | assertTrue(NotificationLite.isComplete(c)); 31 | assertFalse(NotificationLite.isComplete(n)); 32 | 33 | assertEquals("Hello", NotificationLite.getValue(n)); 34 | } 35 | 36 | @Test 37 | public void testValueKind() { 38 | assertSame(1, NotificationLite.next(1)); 39 | } 40 | 41 | // TODO this test is no longer relevant as nulls are not allowed and value maps to itself 42 | // @Test 43 | // public void testValueKind() { 44 | // assertTrue(NotificationLite.isNull(NotificationLite.next(null))); 45 | // assertFalse(NotificationLite.isNull(NotificationLite.next(1))); 46 | // assertFalse(NotificationLite.isNull(NotificationLite.error(new TestException()))); 47 | // assertFalse(NotificationLite.isNull(NotificationLite.completed())); 48 | // assertFalse(NotificationLite.isNull(null)); 49 | // 50 | // assertTrue(NotificationLite.isNext(NotificationLite.next(null))); 51 | // assertTrue(NotificationLite.isNext(NotificationLite.next(1))); 52 | // assertFalse(NotificationLite.isNext(NotificationLite.completed())); 53 | // assertFalse(NotificationLite.isNext(null)); 54 | // assertFalse(NotificationLite.isNext(NotificationLite.error(new TestException()))); 55 | // } 56 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorAsObservableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.junit.Assert.assertFalse; 17 | import static org.mockito.Matchers.any; 18 | import static org.mockito.Mockito.*; 19 | 20 | import org.junit.Test; 21 | import org.reactivestreams.Subscriber; 22 | 23 | import hu.akarnokd.rxjava2.*; 24 | import hu.akarnokd.rxjava2.exceptions.TestException; 25 | import hu.akarnokd.rxjava2.subjects.PublishSubject; 26 | 27 | public class OperatorAsObservableTest { 28 | @Test 29 | public void testHiding() { 30 | PublishSubject src = PublishSubject.create(); 31 | 32 | Observable dst = src.asObservable(); 33 | 34 | assertFalse(dst instanceof PublishSubject); 35 | 36 | Subscriber o = TestHelper.mockSubscriber(); 37 | 38 | dst.subscribe(o); 39 | 40 | src.onNext(1); 41 | src.onComplete(); 42 | 43 | verify(o).onNext(1); 44 | verify(o).onComplete(); 45 | verify(o, never()).onError(any(Throwable.class)); 46 | } 47 | @Test 48 | public void testHidingError() { 49 | PublishSubject src = PublishSubject.create(); 50 | 51 | Observable dst = src.asObservable(); 52 | 53 | assertFalse(dst instanceof PublishSubject); 54 | 55 | @SuppressWarnings("unchecked") 56 | Observer o = mock(Observer.class); 57 | 58 | dst.subscribe(o); 59 | 60 | src.onError(new TestException()); 61 | 62 | verify(o, never()).onNext(any()); 63 | verify(o, never()).onComplete(); 64 | verify(o).onError(any(TestException.class)); 65 | } 66 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorCastTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.Test; 19 | import org.reactivestreams.Subscriber; 20 | 21 | import hu.akarnokd.rxjava2.*; 22 | 23 | public class OperatorCastTest { 24 | 25 | @Test 26 | public void testCast() { 27 | Observable source = Observable.just(1, 2); 28 | Observable observable = source.cast(Integer.class); 29 | 30 | Subscriber observer = TestHelper.mockSubscriber(); 31 | 32 | observable.subscribe(observer); 33 | 34 | verify(observer, times(1)).onNext(1); 35 | verify(observer, times(1)).onNext(1); 36 | verify(observer, never()).onError( 37 | org.mockito.Matchers.any(Throwable.class)); 38 | verify(observer, times(1)).onComplete(); 39 | } 40 | 41 | @Test 42 | public void testCastWithWrongType() { 43 | Observable source = Observable.just(1, 2); 44 | Observable observable = source.cast(Boolean.class); 45 | 46 | Subscriber observer = TestHelper.mockSubscriber(); 47 | 48 | observable.subscribe(observer); 49 | 50 | verify(observer, times(1)).onError( 51 | org.mockito.Matchers.any(ClassCastException.class)); 52 | } 53 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorElementAtTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.Observable; 21 | 22 | public class OperatorElementAtTest { 23 | 24 | @Test 25 | public void testElementAt() { 26 | assertEquals(2, Observable.fromArray(1, 2).elementAt(1).toBlocking().single() 27 | .intValue()); 28 | } 29 | 30 | @Test(expected = IndexOutOfBoundsException.class) 31 | public void testElementAtWithMinusIndex() { 32 | Observable.fromArray(1, 2).elementAt(-1); 33 | } 34 | 35 | @Test(expected = IndexOutOfBoundsException.class) 36 | public void testElementAtWithIndexOutOfBounds() { 37 | Observable.fromArray(1, 2).elementAt(2).toBlocking().single(); 38 | } 39 | 40 | @Test 41 | public void testElementAtOrDefault() { 42 | assertEquals(2, Observable.fromArray(1, 2).elementAt(1, 0).toBlocking() 43 | .single().intValue()); 44 | } 45 | 46 | @Test 47 | public void testElementAtOrDefaultWithIndexOutOfBounds() { 48 | assertEquals(0, Observable.fromArray(1, 2).elementAt(2, 0).toBlocking() 49 | .single().intValue()); 50 | } 51 | 52 | @Test(expected = IndexOutOfBoundsException.class) 53 | public void testElementAtOrDefaultWithMinusIndex() { 54 | Observable.fromArray(1, 2).elementAt(-1, 0); 55 | } 56 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorFinallyTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.*; 19 | import org.reactivestreams.Subscriber; 20 | 21 | import hu.akarnokd.rxjava2.*; 22 | 23 | public class OperatorFinallyTest { 24 | 25 | private Runnable aAction0; 26 | private Subscriber observer; 27 | 28 | // mocking has to be unchecked, unfortunately 29 | @Before 30 | public void before() { 31 | aAction0 = mock(Runnable.class); 32 | observer = TestHelper.mockSubscriber(); 33 | } 34 | 35 | private void checkActionCalled(Observable input) { 36 | input.finallyDo(aAction0).subscribe(observer); 37 | verify(aAction0, times(1)).run(); 38 | } 39 | 40 | @Test 41 | public void testFinallyCalledOnComplete() { 42 | checkActionCalled(Observable.fromArray("1", "2", "3")); 43 | } 44 | 45 | @Test 46 | public void testFinallyCalledOnError() { 47 | checkActionCalled(Observable. error(new RuntimeException("expected"))); 48 | } 49 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorMapNotificationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import org.junit.Test; 17 | 18 | import hu.akarnokd.rxjava2.Observable; 19 | import hu.akarnokd.rxjava2.functions.*; 20 | import hu.akarnokd.rxjava2.subscribers.TestSubscriber; 21 | 22 | public class OperatorMapNotificationTest { 23 | @Test 24 | public void testJust() { 25 | TestSubscriber ts = new TestSubscriber(); 26 | Observable.just(1) 27 | .flatMap( 28 | new Function>() { 29 | @Override 30 | public Observable apply(Integer item) { 31 | return Observable.just((Object)(item + 1)); 32 | } 33 | }, 34 | new Function>() { 35 | @Override 36 | public Observable apply(Throwable e) { 37 | return Observable.error(e); 38 | } 39 | }, 40 | new Supplier>() { 41 | @Override 42 | public Observable get() { 43 | return Observable.never(); 44 | } 45 | } 46 | ).subscribe(ts); 47 | 48 | ts.assertNoErrors(); 49 | ts.assertNotComplete(); 50 | ts.assertValue(2); 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/OperatorTimeIntervalTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.*; 21 | import org.mockito.InOrder; 22 | import org.reactivestreams.Subscriber; 23 | 24 | import hu.akarnokd.rxjava2.*; 25 | import hu.akarnokd.rxjava2.schedulers.*; 26 | import hu.akarnokd.rxjava2.subjects.PublishSubject; 27 | 28 | public class OperatorTimeIntervalTest { 29 | 30 | private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS; 31 | 32 | private Subscriber> observer; 33 | 34 | private TestScheduler testScheduler; 35 | private PublishSubject subject; 36 | private Observable> observable; 37 | 38 | @Before 39 | public void setUp() { 40 | observer = TestHelper.mockSubscriber(); 41 | testScheduler = new TestScheduler(); 42 | subject = PublishSubject.create(); 43 | observable = subject.timeInterval(testScheduler); 44 | } 45 | 46 | @Test 47 | public void testTimeInterval() { 48 | InOrder inOrder = inOrder(observer); 49 | observable.subscribe(observer); 50 | 51 | testScheduler.advanceTimeBy(1000, TIME_UNIT); 52 | subject.onNext(1); 53 | testScheduler.advanceTimeBy(2000, TIME_UNIT); 54 | subject.onNext(2); 55 | testScheduler.advanceTimeBy(3000, TIME_UNIT); 56 | subject.onNext(3); 57 | subject.onComplete(); 58 | 59 | inOrder.verify(observer, times(1)).onNext( 60 | new Timed(1, 1000, TIME_UNIT)); 61 | inOrder.verify(observer, times(1)).onNext( 62 | new Timed(2, 2000, TIME_UNIT)); 63 | inOrder.verify(observer, times(1)).onNext( 64 | new Timed(3, 3000, TIME_UNIT)); 65 | inOrder.verify(observer, times(1)).onComplete(); 66 | inOrder.verifyNoMoreInteractions(); 67 | } 68 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorAsObservableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import static org.junit.Assert.assertFalse; 17 | import static org.mockito.Matchers.any; 18 | import static org.mockito.Mockito.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.*; 23 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 24 | import hu.akarnokd.rxjava2.exceptions.TestException; 25 | import hu.akarnokd.rxjava2.subjects.nbp.NbpPublishSubject; 26 | 27 | public class NbpOperatorAsObservableTest { 28 | @Test 29 | public void testHiding() { 30 | NbpPublishSubject src = NbpPublishSubject.create(); 31 | 32 | NbpObservable dst = src.asObservable(); 33 | 34 | assertFalse(dst instanceof NbpPublishSubject); 35 | 36 | NbpSubscriber o = TestHelper.mockNbpSubscriber(); 37 | 38 | dst.subscribe(o); 39 | 40 | src.onNext(1); 41 | src.onComplete(); 42 | 43 | verify(o).onNext(1); 44 | verify(o).onComplete(); 45 | verify(o, never()).onError(any(Throwable.class)); 46 | } 47 | @Test 48 | public void testHidingError() { 49 | NbpPublishSubject src = NbpPublishSubject.create(); 50 | 51 | NbpObservable dst = src.asObservable(); 52 | 53 | assertFalse(dst instanceof NbpPublishSubject); 54 | 55 | NbpSubscriber o = TestHelper.mockNbpSubscriber(); 56 | 57 | dst.subscribe(o); 58 | 59 | src.onError(new TestException()); 60 | 61 | verify(o, never()).onNext(any()); 62 | verify(o, never()).onComplete(); 63 | verify(o).onError(any(TestException.class)); 64 | } 65 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorCastTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.*; 21 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 22 | 23 | public class NbpOperatorCastTest { 24 | 25 | @Test 26 | public void testCast() { 27 | NbpObservable source = NbpObservable.just(1, 2); 28 | NbpObservable NbpObservable = source.cast(Integer.class); 29 | 30 | NbpSubscriber NbpObserver = TestHelper.mockNbpSubscriber(); 31 | 32 | NbpObservable.subscribe(NbpObserver); 33 | 34 | verify(NbpObserver, times(1)).onNext(1); 35 | verify(NbpObserver, times(1)).onNext(1); 36 | verify(NbpObserver, never()).onError( 37 | org.mockito.Matchers.any(Throwable.class)); 38 | verify(NbpObserver, times(1)).onComplete(); 39 | } 40 | 41 | @Test 42 | public void testCastWithWrongType() { 43 | NbpObservable source = NbpObservable.just(1, 2); 44 | NbpObservable NbpObservable = source.cast(Boolean.class); 45 | 46 | NbpSubscriber NbpObserver = TestHelper.mockNbpSubscriber(); 47 | 48 | NbpObservable.subscribe(NbpObserver); 49 | 50 | verify(NbpObserver, times(1)).onError( 51 | org.mockito.Matchers.any(ClassCastException.class)); 52 | } 53 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorElementAtTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.NbpObservable; 21 | 22 | public class NbpOperatorElementAtTest { 23 | 24 | @Test 25 | public void testElementAt() { 26 | assertEquals(2, NbpObservable.fromArray(1, 2).elementAt(1).toBlocking().single() 27 | .intValue()); 28 | } 29 | 30 | @Test(expected = IndexOutOfBoundsException.class) 31 | public void testElementAtWithMinusIndex() { 32 | NbpObservable.fromArray(1, 2).elementAt(-1); 33 | } 34 | 35 | @Test(expected = IndexOutOfBoundsException.class) 36 | public void testElementAtWithIndexOutOfBounds() { 37 | NbpObservable.fromArray(1, 2).elementAt(2).toBlocking().single(); 38 | } 39 | 40 | @Test 41 | public void testElementAtOrDefault() { 42 | assertEquals(2, NbpObservable.fromArray(1, 2).elementAt(1, 0).toBlocking() 43 | .single().intValue()); 44 | } 45 | 46 | @Test 47 | public void testElementAtOrDefaultWithIndexOutOfBounds() { 48 | assertEquals(0, NbpObservable.fromArray(1, 2).elementAt(2, 0).toBlocking() 49 | .single().intValue()); 50 | } 51 | 52 | @Test(expected = IndexOutOfBoundsException.class) 53 | public void testElementAtOrDefaultWithMinusIndex() { 54 | NbpObservable.fromArray(1, 2).elementAt(-1, 0); 55 | } 56 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorFinallyTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import org.junit.*; 19 | 20 | import hu.akarnokd.rxjava2.*; 21 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 22 | 23 | public class NbpOperatorFinallyTest { 24 | 25 | private Runnable aAction0; 26 | private NbpSubscriber NbpObserver; 27 | 28 | // mocking has to be unchecked, unfortunately 29 | @Before 30 | public void before() { 31 | aAction0 = mock(Runnable.class); 32 | NbpObserver = TestHelper.mockNbpSubscriber(); 33 | } 34 | 35 | private void checkActionCalled(NbpObservable input) { 36 | input.finallyDo(aAction0).subscribe(NbpObserver); 37 | verify(aAction0, times(1)).run(); 38 | } 39 | 40 | @Test 41 | public void testFinallyCalledOnComplete() { 42 | checkActionCalled(NbpObservable.fromArray("1", "2", "3")); 43 | } 44 | 45 | @Test 46 | public void testFinallyCalledOnError() { 47 | checkActionCalled(NbpObservable. error(new RuntimeException("expected"))); 48 | } 49 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorMapNotificationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import org.junit.Test; 17 | 18 | import hu.akarnokd.rxjava2.NbpObservable; 19 | import hu.akarnokd.rxjava2.functions.*; 20 | import hu.akarnokd.rxjava2.subscribers.nbp.NbpTestSubscriber; 21 | 22 | public class NbpOperatorMapNotificationTest { 23 | @Test 24 | public void testJust() { 25 | NbpTestSubscriber ts = new NbpTestSubscriber(); 26 | NbpObservable.just(1) 27 | .flatMap( 28 | new Function>() { 29 | @Override 30 | public NbpObservable apply(Integer item) { 31 | return NbpObservable.just((Object)(item + 1)); 32 | } 33 | }, 34 | new Function>() { 35 | @Override 36 | public NbpObservable apply(Throwable e) { 37 | return NbpObservable.error(e); 38 | } 39 | }, 40 | new Supplier>() { 41 | @Override 42 | public NbpObservable get() { 43 | return NbpObservable.never(); 44 | } 45 | } 46 | ).subscribe(ts); 47 | 48 | ts.assertNoErrors(); 49 | ts.assertNotComplete(); 50 | ts.assertValue(2); 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/internal/operators/nbp/NbpOperatorTimeIntervalTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.internal.operators.nbp; 15 | 16 | import static org.mockito.Mockito.*; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.*; 21 | import org.mockito.InOrder; 22 | 23 | import hu.akarnokd.rxjava2.*; 24 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 25 | import hu.akarnokd.rxjava2.schedulers.*; 26 | import hu.akarnokd.rxjava2.subjects.nbp.NbpPublishSubject; 27 | 28 | public class NbpOperatorTimeIntervalTest { 29 | 30 | private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS; 31 | 32 | private NbpSubscriber> NbpObserver; 33 | 34 | private TestScheduler testScheduler; 35 | private NbpPublishSubject subject; 36 | private NbpObservable> NbpObservable; 37 | 38 | @Before 39 | public void setUp() { 40 | NbpObserver = TestHelper.mockNbpSubscriber(); 41 | testScheduler = new TestScheduler(); 42 | subject = NbpPublishSubject.create(); 43 | NbpObservable = subject.timeInterval(testScheduler); 44 | } 45 | 46 | @Test 47 | public void testTimeInterval() { 48 | InOrder inOrder = inOrder(NbpObserver); 49 | NbpObservable.subscribe(NbpObserver); 50 | 51 | testScheduler.advanceTimeBy(1000, TIME_UNIT); 52 | subject.onNext(1); 53 | testScheduler.advanceTimeBy(2000, TIME_UNIT); 54 | subject.onNext(2); 55 | testScheduler.advanceTimeBy(3000, TIME_UNIT); 56 | subject.onNext(3); 57 | subject.onComplete(); 58 | 59 | inOrder.verify(NbpObserver, times(1)).onNext( 60 | new Timed(1, 1000, TIME_UNIT)); 61 | inOrder.verify(NbpObserver, times(1)).onNext( 62 | new Timed(2, 2000, TIME_UNIT)); 63 | inOrder.verify(NbpObserver, times(1)).onNext( 64 | new Timed(3, 3000, TIME_UNIT)); 65 | inOrder.verify(NbpObserver, times(1)).onComplete(); 66 | inOrder.verifyNoMoreInteractions(); 67 | } 68 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpObservableDoOnTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | import java.util.concurrent.atomic.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.NbpObservable; 23 | import hu.akarnokd.rxjava2.functions.Consumer; 24 | 25 | public class NbpObservableDoOnTest { 26 | 27 | @Test 28 | public void testDoOnEach() { 29 | final AtomicReference r = new AtomicReference(); 30 | String output = NbpObservable.just("one").doOnNext(new Consumer() { 31 | @Override 32 | public void accept(String v) { 33 | r.set(v); 34 | } 35 | }).toBlocking().single(); 36 | 37 | assertEquals("one", output); 38 | assertEquals("one", r.get()); 39 | } 40 | 41 | @Test 42 | public void testDoOnError() { 43 | final AtomicReference r = new AtomicReference(); 44 | Throwable t = null; 45 | try { 46 | NbpObservable. error(new RuntimeException("an error")) 47 | .doOnError(new Consumer() { 48 | @Override 49 | public void accept(Throwable v) { 50 | r.set(v); 51 | } 52 | }).toBlocking().single(); 53 | fail("expected exception, not a return value"); 54 | } catch (Throwable e) { 55 | t = e; 56 | } 57 | 58 | assertNotNull(t); 59 | assertEquals(t, r.get()); 60 | } 61 | 62 | @Test 63 | public void testDoOnCompleted() { 64 | final AtomicBoolean r = new AtomicBoolean(); 65 | String output = NbpObservable.just("one").doOnComplete(new Runnable() { 66 | @Override 67 | public void run() { 68 | r.set(true); 69 | } 70 | }).toBlocking().single(); 71 | 72 | assertEquals("one", output); 73 | assertTrue(r.get()); 74 | } 75 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpObservableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import java.util.*; 17 | 18 | import org.junit.*; 19 | 20 | import hu.akarnokd.rxjava2.NbpObservable; 21 | import hu.akarnokd.rxjava2.functions.Function; 22 | 23 | public class NbpObservableTest { 24 | @Test 25 | public void testFlatMap() { 26 | List list = NbpObservable.range(1, 5).flatMap(new Function>() { 27 | @Override 28 | public NbpObservable apply(Integer v) { 29 | return NbpObservable.range(v, 2); 30 | } 31 | }).getList(); 32 | 33 | Assert.assertEquals(Arrays.asList(1, 2, 2, 3, 3, 4, 4, 5, 5, 6), list); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpObservableWindowTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | import java.util.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.NbpObservable; 23 | import hu.akarnokd.rxjava2.functions.*; 24 | 25 | public class NbpObservableWindowTests { 26 | 27 | @Test 28 | public void testWindow() { 29 | final ArrayList> lists = new ArrayList>(); 30 | 31 | NbpObservable.concat( 32 | NbpObservable.just(1, 2, 3, 4, 5, 6) 33 | .window(3) 34 | .map(new Function, NbpObservable>>() { 35 | @Override 36 | public NbpObservable> apply(NbpObservable xs) { 37 | return xs.toList(); 38 | } 39 | }) 40 | ) 41 | .toBlocking().forEach(new Consumer>() { 42 | @Override 43 | public void accept(List xs) { 44 | lists.add(xs); 45 | } 46 | }); 47 | 48 | assertArrayEquals(lists.get(0).toArray(new Integer[3]), new Integer[] { 1, 2, 3 }); 49 | assertArrayEquals(lists.get(1).toArray(new Integer[3]), new Integer[] { 4, 5, 6 }); 50 | assertEquals(2, lists.size()); 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpScanTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import java.util.HashMap; 17 | 18 | import org.junit.Test; 19 | 20 | import hu.akarnokd.rxjava2.functions.*; 21 | import hu.akarnokd.rxjava2.nbp.NbpEventStream.Event; 22 | 23 | public class NbpScanTests { 24 | 25 | @Test 26 | public void testUnsubscribeScan() { 27 | 28 | NbpEventStream.getEventStream("HTTP-ClusterB", 20) 29 | .scan(new HashMap(), new BiFunction, Event, HashMap>() { 30 | @Override 31 | public HashMap apply(HashMap accum, Event perInstanceEvent) { 32 | accum.put("instance", perInstanceEvent.instanceId); 33 | return accum; 34 | } 35 | }) 36 | .take(10) 37 | .toBlocking() 38 | .forEach(new Consumer>() { 39 | @Override 40 | public void accept(HashMap pv) { 41 | System.out.println(pv); 42 | } 43 | }); 44 | } 45 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpStartWithTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | import java.util.*; 19 | 20 | import org.junit.Test; 21 | 22 | import hu.akarnokd.rxjava2.NbpObservable; 23 | 24 | public class NbpStartWithTests { 25 | 26 | @Test 27 | public void startWith1() { 28 | List values = NbpObservable.just("one", "two") 29 | .startWithArray("zero").toList().toBlocking().single(); 30 | 31 | assertEquals("zero", values.get(0)); 32 | assertEquals("two", values.get(2)); 33 | } 34 | 35 | @Test 36 | public void startWithIterable() { 37 | List li = new ArrayList(); 38 | li.add("alpha"); 39 | li.add("beta"); 40 | List values = NbpObservable.just("one", "two").startWith(li).toList().toBlocking().single(); 41 | 42 | assertEquals("alpha", values.get(0)); 43 | assertEquals("beta", values.get(1)); 44 | assertEquals("one", values.get(2)); 45 | assertEquals("two", values.get(3)); 46 | } 47 | 48 | @Test 49 | public void startWithObservable() { 50 | List li = new ArrayList(); 51 | li.add("alpha"); 52 | li.add("beta"); 53 | List values = NbpObservable.just("one", "two") 54 | .startWith(NbpObservable.fromIterable(li)) 55 | .toList() 56 | .toBlocking() 57 | .single(); 58 | 59 | assertEquals("alpha", values.get(0)); 60 | assertEquals("beta", values.get(1)); 61 | assertEquals("one", values.get(2)); 62 | assertEquals("two", values.get(3)); 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpThrottleLastTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import static org.mockito.Mockito.inOrder; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.Test; 21 | import org.mockito.InOrder; 22 | 23 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 24 | import hu.akarnokd.rxjava2.TestHelper; 25 | import hu.akarnokd.rxjava2.schedulers.TestScheduler; 26 | import hu.akarnokd.rxjava2.subjects.nbp.NbpPublishSubject; 27 | 28 | public class NbpThrottleLastTests { 29 | 30 | @Test 31 | public void testThrottle() { 32 | NbpSubscriber observer = TestHelper.mockNbpSubscriber(); 33 | 34 | TestScheduler s = new TestScheduler(); 35 | NbpPublishSubject o = NbpPublishSubject.create(); 36 | o.throttleLast(500, TimeUnit.MILLISECONDS, s).subscribe(observer); 37 | 38 | // send events with simulated time increments 39 | s.advanceTimeTo(0, TimeUnit.MILLISECONDS); 40 | o.onNext(1); // skip 41 | o.onNext(2); // deliver 42 | s.advanceTimeTo(501, TimeUnit.MILLISECONDS); 43 | o.onNext(3); // skip 44 | s.advanceTimeTo(600, TimeUnit.MILLISECONDS); 45 | o.onNext(4); // skip 46 | s.advanceTimeTo(700, TimeUnit.MILLISECONDS); 47 | o.onNext(5); // skip 48 | o.onNext(6); // deliver 49 | s.advanceTimeTo(1001, TimeUnit.MILLISECONDS); 50 | o.onNext(7); // deliver 51 | s.advanceTimeTo(1501, TimeUnit.MILLISECONDS); 52 | o.onComplete(); 53 | 54 | InOrder inOrder = inOrder(observer); 55 | inOrder.verify(observer).onNext(2); 56 | inOrder.verify(observer).onNext(6); 57 | inOrder.verify(observer).onNext(7); 58 | inOrder.verify(observer).onComplete(); 59 | inOrder.verifyNoMoreInteractions(); 60 | } 61 | } -------------------------------------------------------------------------------- /src/test/java/hu/akarnokd/rxjava2/nbp/NbpThrottleWithTimeoutTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 David Karnok and Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | 14 | package hu.akarnokd.rxjava2.nbp; 15 | 16 | import static org.mockito.Mockito.inOrder; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.junit.Test; 21 | import org.mockito.InOrder; 22 | 23 | import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber; 24 | import hu.akarnokd.rxjava2.TestHelper; 25 | import hu.akarnokd.rxjava2.schedulers.TestScheduler; 26 | import hu.akarnokd.rxjava2.subjects.nbp.NbpPublishSubject; 27 | 28 | public class NbpThrottleWithTimeoutTests { 29 | 30 | @Test 31 | public void testThrottle() { 32 | NbpSubscriber observer = TestHelper.mockNbpSubscriber(); 33 | 34 | TestScheduler s = new TestScheduler(); 35 | NbpPublishSubject o = NbpPublishSubject.create(); 36 | o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s) 37 | .subscribe(observer); 38 | 39 | // send events with simulated time increments 40 | s.advanceTimeTo(0, TimeUnit.MILLISECONDS); 41 | o.onNext(1); // skip 42 | o.onNext(2); // deliver 43 | s.advanceTimeTo(501, TimeUnit.MILLISECONDS); 44 | o.onNext(3); // skip 45 | s.advanceTimeTo(600, TimeUnit.MILLISECONDS); 46 | o.onNext(4); // skip 47 | s.advanceTimeTo(700, TimeUnit.MILLISECONDS); 48 | o.onNext(5); // skip 49 | o.onNext(6); // deliver at 1300 after 500ms has passed since onNext(5) 50 | s.advanceTimeTo(1300, TimeUnit.MILLISECONDS); 51 | o.onNext(7); // deliver 52 | s.advanceTimeTo(1800, TimeUnit.MILLISECONDS); 53 | o.onComplete(); 54 | 55 | InOrder inOrder = inOrder(observer); 56 | inOrder.verify(observer).onNext(2); 57 | inOrder.verify(observer).onNext(6); 58 | inOrder.verify(observer).onNext(7); 59 | inOrder.verify(observer).onComplete(); 60 | inOrder.verifyNoMoreInteractions(); 61 | } 62 | } --------------------------------------------------------------------------------