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

rx.operators.OperationToMap 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.operators;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.functions.Functions;
import rx.subscriptions.Subscriptions;

/**
 * Maps the elements of the source observable into a java.util.Map instance and
 * emits that once the source observable completes.
 * 
 * @see Issue #96
 */
public class OperationToMap {
    /**
     * ToMap with key selector, identity value selector and default HashMap factory.
     */
    public static  OnSubscribeFunc> toMap(Observable source,
            Func1 keySelector) {
        return new ToMap(source, keySelector,
                Functions. identity(), new DefaultToMapFactory());
    }

    /**
     * ToMap with key selector, value selector and default HashMap factory.
     */
    public static  OnSubscribeFunc> toMap(Observable source,
            Func1 keySelector,
            Func1 valueSelector) {
        return new ToMap(source, keySelector,
                valueSelector, new DefaultToMapFactory());
    }

    /**
     * ToMap with key selector, value selector and custom Map factory.
     */
    public static  OnSubscribeFunc> toMap(Observable source,
            Func1 keySelector,
            Func1 valueSelector,
            Func0> mapFactory) {
        return new ToMap(source, keySelector,
                valueSelector, mapFactory);
    }

    /** The default map factory. */
    public static class DefaultToMapFactory implements Func0> {
        @Override
        public Map call() {
            return new HashMap();
        }
    }

    /**
     * Maps the elements of the source observable into a java.util.Map instance
     * returned by the mapFactory function by using the keySelector and
     * valueSelector.
     * 
     * @param 
     *            the source's value type
     * @param 
     *            the key type
     * @param 
     *            the value type
     */
    public static class ToMap implements OnSubscribeFunc> {
        /** The source. */
        private final Observable source;
        /** Key extractor. */
        private final Func1 keySelector;
        /** Value extractor. */
        private final Func1 valueSelector;
        /** Map factory. */
        private final Func0> mapFactory;

        public ToMap(
                Observable source,
                Func1 keySelector,
                Func1 valueSelector,
                Func0> mapFactory) {
            this.source = source;
            this.keySelector = keySelector;
            this.valueSelector = valueSelector;
            this.mapFactory = mapFactory;

        }

        @Override
        public Subscription onSubscribe(Observer> t1) {
            Map map;
            try {
                map = mapFactory.call();
            } catch (Throwable t) {
                t1.onError(t);
                return Subscriptions.empty();
            }
            return source.subscribe(new ToMapObserver(
                    t1, keySelector, valueSelector, map));
        }

        /**
         * Observer that collects the source values of T into
         * a map.
         */
        public static class ToMapObserver implements Observer {
            /** The map. */
            Map map;
            /** Key extractor. */
            private final Func1 keySelector;
            /** Value extractor. */
            private final Func1 valueSelector;
            /** The observer who is receiving the completed map. */
            private final Observer> t1;

            public ToMapObserver(
                    Observer> t1,
                    Func1 keySelector,
                    Func1 valueSelector,
                    Map map) {
                this.map = map;
                this.t1 = t1;
                this.keySelector = keySelector;
                this.valueSelector = valueSelector;
            }

            @Override
            public void onNext(T args) {
                K key = keySelector.call(args);
                V value = valueSelector.call(args);
                map.put(key, value);
            }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy