Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.internal.operators;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Observable.Operator;
import rx.Observer;
import rx.Producer;
import rx.Subscriber;
import rx.exceptions.OnErrorThrowable;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.observables.GroupedObservable;
import rx.subjects.Subject;
import rx.subscriptions.Subscriptions;
/**
* Groups the items emitted by an Observable according to a specified criterion, and emits these
* grouped items as Observables, one Observable per group.
*
*
*
* @param
* the key type
* @param
* the source and group value type
* @param
* the value type of the groups
*/
public class OperatorGroupBy implements Operator, T> {
final Func1 super T, ? extends K> keySelector;
final Func1 super T, ? extends R> valueSelector;
@SuppressWarnings("unchecked")
public OperatorGroupBy(final Func1 super T, ? extends K> keySelector) {
this(keySelector, (Func1) IDENTITY);
}
public OperatorGroupBy(
Func1 super T, ? extends K> keySelector,
Func1 super T, ? extends R> valueSelector) {
this.keySelector = keySelector;
this.valueSelector = valueSelector;
}
@Override
public Subscriber super T> call(final Subscriber super GroupedObservable> child) {
return new GroupBySubscriber(keySelector, valueSelector, child);
}
static final class GroupBySubscriber extends Subscriber {
private static final int MAX_QUEUE_SIZE = 1024;
final GroupBySubscriber self = this;
final Func1 super T, ? extends K> keySelector;
final Func1 super T, ? extends R> elementSelector;
final Subscriber super GroupedObservable> child;
// We should not call `unsubscribe()` until `groups.isEmpty() && child.isUnsubscribed()` is true.
// Use `WIP_FOR_UNSUBSCRIBE_UPDATER` to monitor these statuses and call `unsubscribe()` properly.
// Should check both when `child.unsubscribe` is called and any group is removed.
@SuppressWarnings("rawtypes")
static final AtomicIntegerFieldUpdater WIP_FOR_UNSUBSCRIBE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(GroupBySubscriber.class, "wipForUnsubscribe");
volatile int wipForUnsubscribe = 1;
public GroupBySubscriber(
Func1 super T, ? extends K> keySelector,
Func1 super T, ? extends R> elementSelector,
Subscriber super GroupedObservable> child) {
super();
this.keySelector = keySelector;
this.elementSelector = elementSelector;
this.child = child;
child.add(Subscriptions.create(new Action0() {
@Override
public void call() {
if (WIP_FOR_UNSUBSCRIBE_UPDATER.decrementAndGet(self) == 0) {
self.unsubscribe();
}
}
}));
}
private static class GroupState {
private final Subject s = BufferUntilSubscriber.create();
private final AtomicLong requested = new AtomicLong();
private final AtomicLong count = new AtomicLong();
private final Queue