All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava2.internal.operators.OperatorDistinct Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and 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 hu.akarnokd.rxjava2.internal.operators;

import java.util.Collection;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.exceptions.CompositeException;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.functions.*;
import hu.akarnokd.rxjava2.internal.subscribers.CancelledSubscriber;
import hu.akarnokd.rxjava2.internal.subscriptions.*;

public final class OperatorDistinct implements Operator {
    final Function keySelector;
    final Supplier> predicateSupplier;
    
    public OperatorDistinct(Function keySelector, Supplier> predicateSupplier) {
        this.predicateSupplier = predicateSupplier;
        this.keySelector = keySelector;
    }
    
    public static  OperatorDistinct withCollection(Function keySelector, final Supplier> collectionSupplier) {
        Supplier> p = new Supplier>() {
            @Override
            public Predicate get() {
                final Collection coll = collectionSupplier.get();
                
                return new Predicate() {
                    @Override
                    public boolean test(K t) {
                        if (t == null) {
                            coll.clear();
                            return true;
                        }
                        return coll.add(t);
                    }
                };
            }
        };
        
        return new OperatorDistinct(keySelector, p);
    }
    
    static final OperatorDistinct UNTIL_CHANGED;
    static {
        Supplier> p = new Supplier>() {
            @Override
            public Predicate get() {
                final Object[] last = { null };
                
                return new Predicate() {
                    @Override
                    public boolean test(Object t) {
                        if (t == null) {
                            last[0] = null;
                            return true;
                        }
                        Object o = last[0];
                        last[0] = t;
                        return !Objects.equals(o, t);
                    }
                };
            }
        };
        UNTIL_CHANGED = new OperatorDistinct(Functions.identity(), p);
    }
    
    @SuppressWarnings("unchecked")
    public static  OperatorDistinct untilChanged() {
        return (OperatorDistinct)UNTIL_CHANGED;
    }

    public static  OperatorDistinct untilChanged(Function keySelector) {
        Supplier> p = new Supplier>() {
            @Override
            public Predicate get() {
                final Object[] last = { null };
                
                return new Predicate() {
                    @Override
                    public boolean test(K t) {
                        if (t == null) {
                            last[0] = null;
                            return true;
                        }
                        Object o = last[0];
                        last[0] = t;
                        return !Objects.equals(o, t);
                    }
                };
            }
        };
        return new OperatorDistinct(keySelector, p);
    }

    
    @Override
    public Subscriber apply(Subscriber t) {
        Predicate coll;
        try {
            coll = predicateSupplier.get();
        } catch (Throwable e) {
            EmptySubscription.error(e, t);
            return CancelledSubscriber.INSTANCE;
        }
        
        if (coll == null) {
            EmptySubscription.error(new NullPointerException("predicateSupplier returned null"), t);
            return CancelledSubscriber.INSTANCE;
        }
        
        return new DistinctSubscriber(t, keySelector, coll);
    }
    
    static final class DistinctSubscriber implements Subscriber {
        final Subscriber actual;
        final Predicate predicate;
        final Function keySelector;
        
        Subscription s;

        public DistinctSubscriber(Subscriber actual, Function keySelector, Predicate predicate) {
            this.actual = actual;
            this.keySelector = keySelector;
            this.predicate = predicate;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            actual.onSubscribe(s);
        }
        
        @Override
        public void onNext(T t) {
            K key;
            
            try {
                key = keySelector.apply(t);
            } catch (Throwable e) {
                s.cancel();
                actual.onError(e);
                return;
            }
            
            if (key == null) {
                s.cancel();
                actual.onError(new NullPointerException("Null key supplied"));
                return;
            }
            
            
            boolean b;
            try {
                b = predicate.test(key);
            } catch (Throwable e) {
                s.cancel();
                actual.onError(e);
                return;
            }
            
            if (b) {
                actual.onNext(t);
            } else {
                s.request(1);
            }
        }
        
        @Override
        public void onError(Throwable t) {
            try {
                predicate.test(null); // special case: poison pill
            } catch (Throwable e) {
                actual.onError(new CompositeException(e, t));
                return;
            }
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            try {
                predicate.test(null); // special case: poison pill
            } catch (Throwable e) {
                actual.onError(e);
                return;
            }
            actual.onComplete();
        }
    }
}