update code style

This commit is contained in:
huangyuhui
2016-01-01 11:03:09 +08:00
parent 1f7eb04215
commit b82243a9c0
298 changed files with 3902 additions and 3998 deletions

View File

@@ -1,12 +1,12 @@
/**
* Copyright 2013 Netflix, Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,11 +30,14 @@ import rx.util.functions.Func1;
public final class OperationMerge {
/**
* Flattens the observable sequences from the list of Observables into one observable sequence without any transformation.
*
* @param source
* An observable sequence of elements to project.
* @return An observable sequence whose elements are the result of flattening the output from the list of Observables.
* Flattens the observable sequences from the list of Observables into one
* observable sequence without any transformation.
*
* @param source An observable sequence of elements to project.
*
* @return An observable sequence whose elements are the result of
* flattening the output from the list of Observables.
*
* @see http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx
*/
public static <T> Func1<Observer<T>, Subscription> merge(final Observable<Observable<T>> source) {
@@ -53,17 +56,14 @@ public final class OperationMerge {
@Override
public Subscription call(Observer<Observable<T>> observer) {
for (Observable<T> o : sequences) {
if (!unsubscribed) {
for (Observable<T> o : sequences)
if (!unsubscribed)
observer.onNext(o);
} else {
else
// break out of the loop if we are unsubscribed
break;
}
}
if (!unsubscribed) {
if (!unsubscribed)
observer.onCompleted();
}
return () -> {
unsubscribed = true;
@@ -73,17 +73,24 @@ public final class OperationMerge {
}
/**
* This class is NOT thread-safe if invoked and referenced multiple times. In other words, don't subscribe to it multiple times from different threads.
* This class is NOT thread-safe if invoked and referenced multiple times.
* In other words, don't subscribe to it multiple times from different
* threads.
* <p>
* It IS thread-safe from within it while receiving onNext events from multiple threads.
* It IS thread-safe from within it while receiving onNext events from
* multiple threads.
* <p>
* This should all be fine as long as it's kept as a private class and a new instance created from static factory method above.
* This should all be fine as long as it's kept as a private class and a new
* instance created from static factory method above.
* <p>
* Note how the take() factory method above protects us from a single instance being exposed with the Observable wrapper handling the subscribe flow.
*
* Note how the take() factory method above protects us from a single
* instance being exposed with the Observable wrapper handling the subscribe
* flow.
*
* @param <T>
*/
private static final class MergeObservable<T> implements Func1<Observer<T>, Subscription> {
private final Observable<Observable<T>> sequences;
private final MergeSubscription ourSubscription = new MergeSubscription();
private final AtomicBoolean stopped = new AtomicBoolean(false);
@@ -99,7 +106,8 @@ public final class OperationMerge {
public Subscription call(Observer<T> actualObserver) {
/**
* We must synchronize a merge because we subscribe to multiple sequences in parallel that will each be emitting.
* We must synchronize a merge because we subscribe to multiple
* sequences in parallel that will each be emitting.
* <p>
* The calls from each sequence must be serialized.
* <p>
@@ -109,18 +117,23 @@ public final class OperationMerge {
SynchronizedObserver<T> synchronizedObserver = new SynchronizedObserver<>(actualObserver, subscription);
/**
* Subscribe to the parent Observable to get to the children Observables
* Subscribe to the parent Observable to get to the children
* Observables
*/
sequences.subscribe(new ParentObserver(synchronizedObserver));
/* return our subscription to allow unsubscribing */
/*
* return our subscription to allow unsubscribing
*/
return subscription;
}
/**
* Manage the internal subscription with a thread-safe means of stopping/unsubscribing so we don't unsubscribe twice.
* Manage the internal subscription with a thread-safe means of
* stopping/unsubscribing so we don't unsubscribe twice.
* <p>
* Also has the stop() method returning a boolean so callers know if their thread "won" and should perform further actions.
* Also has the stop() method returning a boolean so callers know if
* their thread "won" and should perform further actions.
*/
private class MergeSubscription implements Subscription {
@@ -134,23 +147,23 @@ public final class OperationMerge {
boolean didSet = stopped.compareAndSet(false, true);
if (didSet) {
// this thread won the race to stop, so unsubscribe from the actualSubscription
for (Subscription _s : childSubscriptions.values()) {
for (Subscription _s : childSubscriptions.values())
_s.unsubscribe();
}
return true;
} else {
} else
// another thread beat us
return false;
}
}
}
/**
* Subscribe to the top level Observable to receive the sequence of Observable<T> children.
*
* Subscribe to the top level Observable to receive the sequence of
* Observable<T> children.
*
* @param <T>
*/
private class ParentObserver implements Observer<Observable<T>> {
private final Observer<T> actualObserver;
public ParentObserver(Observer<T> actualObserver) {
@@ -164,13 +177,10 @@ public final class OperationMerge {
// but will let the child worry about it
// if however this completes and there are no children processing, then we will send onCompleted
if (childObservers.isEmpty()) {
if (!stopped.get()) {
if (ourSubscription.stop()) {
if (childObservers.isEmpty())
if (!stopped.get())
if (ourSubscription.stop())
actualObserver.onCompleted();
}
}
}
}
@Override
@@ -180,20 +190,21 @@ public final class OperationMerge {
@Override
public void onNext(Observable<T> childObservable) {
if (stopped.get()) {
if (stopped.get())
// we won't act on any further items
return;
}
if (childObservable == null) {
if (childObservable == null)
throw new IllegalArgumentException("Observable<T> can not be null.");
}
/**
* For each child Observable we receive we'll subscribe with a separate Observer
* that will each then forward their sequences to the actualObserver.
* For each child Observable we receive we'll subscribe with a
* separate Observer that will each then forward their sequences
* to the actualObserver.
* <p>
* We use separate child Observers for each sequence to simplify the onComplete/onError handling so each sequence has its own lifecycle.
* We use separate child Observers for each sequence to simplify
* the onComplete/onError handling so each sequence has its own
* lifecycle.
*/
ChildObserver _w = new ChildObserver(actualObserver);
childObservers.put(_w, _w);
@@ -204,8 +215,9 @@ public final class OperationMerge {
}
/**
* Subscribe to each child Observable<T> and forward their sequence of data to the actualObserver
*
* Subscribe to each child Observable<T> and forward their sequence of
* data to the actualObserver
*
*/
private class ChildObserver implements Observer<T> {
@@ -221,33 +233,27 @@ public final class OperationMerge {
childObservers.remove(this);
// if there are now 0 Observers left, so if the parent is also completed we send the onComplete to the actualObserver
// if the parent is not complete that means there is another sequence (and child Observer) to come
if (!stopped.get()) {
if (childObservers.isEmpty() && parentCompleted) {
if (ourSubscription.stop()) {
if (!stopped.get())
if (childObservers.isEmpty() && parentCompleted)
if (ourSubscription.stop())
// this thread 'won' the race to unsubscribe/stop so let's send onCompleted
actualObserver.onCompleted();
}
}
}
}
@Override
public void onError(Exception e) {
if (!stopped.get()) {
if (ourSubscription.stop()) {
if (!stopped.get())
if (ourSubscription.stop())
// this thread 'won' the race to unsubscribe/stop so let's send the error
actualObserver.onError(e);
}
}
}
@Override
public void onNext(T args) {
// in case the Observable is poorly behaved and doesn't listen to the unsubscribe request
// we'll ignore anything that comes in after we've unsubscribed
if (!stopped.get()) {
if (!stopped.get())
actualObserver.onNext(args);
}
}
}