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

rx.internal.operators.OperatorToMultimap Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
/**
 * 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.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Func0;
import rx.functions.Func1;

/**
 * Maps the elements of the source observable into a multimap
 * (Map<K, Collection<V>>) where each
 * key entry has a collection of the source's values.
 *
 * @see Issue #97
 */
public final class OperatorToMultimap implements Operator>, T> {
    /**
     * The default multimap factory returning a HashMap.
     */
    public static final class DefaultToMultimapFactory implements Func0>> {
        @Override
        public Map> call() {
            return new HashMap>();
        }
    }

    /**
     * The default collection factory for a key in the multimap returning
     * an ArrayList independent of the key.
     */
    public static final class DefaultMultimapCollectionFactory
            implements Func1> {
        @Override
        public Collection call(K t1) {
            return new ArrayList();
        }
    }

    private final Func1 keySelector;
    private final Func1 valueSelector;
    private final Func0>> mapFactory;
    private final Func1> collectionFactory;

    /**
     * ToMultimap with key selector, custom value selector,
     * default HashMap factory and default ArrayList collection factory.
     */
    public OperatorToMultimap(
            Func1 keySelector,
            Func1 valueSelector) {
        this(keySelector, valueSelector,
                new DefaultToMultimapFactory(),
                new DefaultMultimapCollectionFactory());
    }

    /**
     * ToMultimap with key selector, custom value selector,
     * custom Map factory and default ArrayList collection factory.
     */
    public OperatorToMultimap(
            Func1 keySelector,
            Func1 valueSelector,
            Func0>> mapFactory) {
        this(keySelector, valueSelector,
                mapFactory,
                new DefaultMultimapCollectionFactory());
    }

    /**
     * ToMultimap with key selector, custom value selector,
     * custom Map factory and custom collection factory.
     */
    public OperatorToMultimap(
            Func1 keySelector,
            Func1 valueSelector,
            Func0>> mapFactory,
            Func1> collectionFactory) {
        this.keySelector = keySelector;
        this.valueSelector = valueSelector;
        this.mapFactory = mapFactory;
        this.collectionFactory = collectionFactory;
    }

    @Override
    public Subscriber call(final Subscriber>> subscriber) {
        return new Subscriber(subscriber) {
            private Map> map = mapFactory.call();

            @Override
            public void onNext(T v) {
                K key = keySelector.call(v);
                V value = valueSelector.call(v);
                Collection collection = map.get(key);
                if (collection == null) {
                    collection = collectionFactory.call(key);
                    map.put(key, collection);
                }
                collection.add(value);
            }

            @Override
            public void onError(Throwable e) {
                map = null;
                subscriber.onError(e);
            }

            @Override
            public void onCompleted() {
                Map> map0 = map;
                map = null;
                subscriber.onNext(map0);
                subscriber.onCompleted();
            }

        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy