|
1 |
| -/** |
2 |
| - * Copyright 2013 Netflix, Inc. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -package rx.operators; |
18 |
| - |
19 |
| -import java.util.HashMap; |
20 |
| -import java.util.Map; |
21 |
| -import rx.Observable; |
22 |
| -import rx.Observable.OnSubscribeFunc; |
23 |
| -import rx.Observer; |
24 |
| -import rx.Subscription; |
25 |
| -import rx.subscriptions.Subscriptions; |
26 |
| -import rx.util.functions.Func0; |
27 |
| -import rx.util.functions.Func1; |
28 |
| -import rx.util.functions.Functions; |
29 |
| - |
30 |
| -/** |
31 |
| - * Maps the elements of the source observable into a java.util.Map instance and |
32 |
| - * emits that once the source observable completes. |
33 |
| - * |
34 |
| - * @see <a href='https://github.com/Netflix/RxJava/issues/96'>Issue #96</a> |
35 |
| - */ |
36 |
| -public class OperationToMap { |
37 |
| - /** |
38 |
| - * ToMap with key selector, identity value selector and default HashMap factory. |
39 |
| - */ |
40 |
| - public static <T, K> OnSubscribeFunc<Map<K, T>> toMap(Observable<T> source, |
41 |
| - Func1<? super T, ? extends K> keySelector) { |
42 |
| - return new ToMap<T, K, T>(source, keySelector, |
43 |
| - Functions.<T>identity(), new DefaultToMapFactory<K, T>()); |
44 |
| - } |
45 |
| - |
46 |
| - /** |
47 |
| - * ToMap with key selector, value selector and default HashMap factory. |
48 |
| - */ |
49 |
| - public static <T, K, V> OnSubscribeFunc<Map<K, V>> toMap(Observable<T> source, |
50 |
| - Func1<? super T, ? extends K> keySelector, |
51 |
| - Func1<? super T, ? extends V> valueSelector) { |
52 |
| - return new ToMap<T, K, V>(source, keySelector, |
53 |
| - valueSelector, new DefaultToMapFactory<K, V>()); |
54 |
| - } |
55 |
| - |
56 |
| - /** |
57 |
| - * ToMap with key selector, value selector and custom Map factory. |
58 |
| - */ |
59 |
| - public static <T, K, V> OnSubscribeFunc<Map<K, V>> toMap(Observable<T> source, |
60 |
| - Func1<? super T, ? extends K> keySelector, |
61 |
| - Func1<? super T, ? extends V> valueSelector, |
62 |
| - Func0<? extends Map<K, V>> mapFactory) { |
63 |
| - return new ToMap<T, K, V>(source, keySelector, |
64 |
| - valueSelector, mapFactory); |
65 |
| - } |
66 |
| - |
67 |
| - /** The default map factory. */ |
68 |
| - public static class DefaultToMapFactory<K, V> implements Func0<Map<K, V>> { |
69 |
| - @Override |
70 |
| - public Map<K, V> call() { |
71 |
| - return new HashMap<K, V>(); |
72 |
| - } |
73 |
| - } |
74 |
| - /** |
75 |
| - * Maps the elements of the source observable into a java.util.Map instance |
76 |
| - * returned by the mapFactory function by using the keySelector and |
77 |
| - * valueSelector. |
78 |
| - * @param <T> the source's value type |
79 |
| - * @param <K> the key type |
80 |
| - * @param <V> the value type |
81 |
| - */ |
82 |
| - public static class ToMap<T, K, V> implements OnSubscribeFunc<Map<K, V>> { |
83 |
| - /** The source. */ |
84 |
| - private final Observable<T> source; |
85 |
| - /** Key extractor. */ |
86 |
| - private final Func1<? super T, ? extends K> keySelector; |
87 |
| - /** Value extractor. */ |
88 |
| - private final Func1<? super T, ? extends V> valueSelector; |
89 |
| - /** Map factory. */ |
90 |
| - private final Func0<? extends Map<K, V>> mapFactory; |
91 |
| - public ToMap( |
92 |
| - Observable<T> source, |
93 |
| - Func1<? super T, ? extends K> keySelector, |
94 |
| - Func1<? super T, ? extends V> valueSelector, |
95 |
| - Func0<? extends Map<K, V>> mapFactory |
96 |
| - ) { |
97 |
| - this.source = source; |
98 |
| - this.keySelector = keySelector; |
99 |
| - this.valueSelector = valueSelector; |
100 |
| - this.mapFactory = mapFactory; |
101 |
| - |
102 |
| - } |
103 |
| - @Override |
104 |
| - public Subscription onSubscribe(Observer<? super Map<K, V>> t1) { |
105 |
| - Map<K, V> map; |
106 |
| - try { |
107 |
| - map = mapFactory.call(); |
108 |
| - } catch (Throwable t) { |
109 |
| - t1.onError(t); |
110 |
| - return Subscriptions.empty(); |
111 |
| - } |
112 |
| - return source.subscribe(new ToMapObserver<K, V, T>( |
113 |
| - t1, keySelector, valueSelector, map)); |
114 |
| - } |
115 |
| - /** |
116 |
| - * Observer that collects the source values of T into |
117 |
| - * a map. |
118 |
| - */ |
119 |
| - public static class ToMapObserver<K, V, T> implements Observer<T> { |
120 |
| - /** The map. */ |
121 |
| - Map<K, V> map; |
122 |
| - /** Key extractor. */ |
123 |
| - private final Func1<? super T, ? extends K> keySelector; |
124 |
| - /** Value extractor. */ |
125 |
| - private final Func1<? super T, ? extends V> valueSelector; |
126 |
| - /** The observer who is receiving the completed map. */ |
127 |
| - private final Observer<? super Map<K, V>> t1; |
128 |
| - |
129 |
| - public ToMapObserver( |
130 |
| - Observer<? super Map<K, V>> t1, |
131 |
| - Func1<? super T, ? extends K> keySelector, |
132 |
| - Func1<? super T, ? extends V> valueSelector, |
133 |
| - Map<K, V> map) { |
134 |
| - this.map = map; |
135 |
| - this.t1 = t1; |
136 |
| - this.keySelector = keySelector; |
137 |
| - this.valueSelector = valueSelector; |
138 |
| - } |
139 |
| - @Override |
140 |
| - public void onNext(T args) { |
141 |
| - K key = keySelector.call(args); |
142 |
| - V value = valueSelector.call(args); |
143 |
| - map.put(key, value); |
144 |
| - } |
145 |
| - @Override |
146 |
| - public void onError(Throwable e) { |
147 |
| - map = null; |
148 |
| - t1.onError(e); |
149 |
| - } |
150 |
| - @Override |
151 |
| - public void onCompleted() { |
152 |
| - Map<K, V> map0 = map; |
153 |
| - map = null; |
154 |
| - t1.onNext(map0); |
155 |
| - t1.onCompleted(); |
156 |
| - } |
157 |
| - } |
158 |
| - } |
159 |
| -} |
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package rx.operators; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import rx.Observable; |
| 22 | +import rx.Observable.OnSubscribeFunc; |
| 23 | +import rx.Observer; |
| 24 | +import rx.Subscription; |
| 25 | +import rx.subscriptions.Subscriptions; |
| 26 | +import rx.util.functions.Func0; |
| 27 | +import rx.util.functions.Func1; |
| 28 | +import rx.util.functions.Functions; |
| 29 | + |
| 30 | +/** |
| 31 | + * Maps the elements of the source observable into a java.util.Map instance and |
| 32 | + * emits that once the source observable completes. |
| 33 | + * |
| 34 | + * @see <a href='https://github.com/Netflix/RxJava/issues/96'>Issue #96</a> |
| 35 | + */ |
| 36 | +public class OperationToMap { |
| 37 | + /** |
| 38 | + * ToMap with key selector, identity value selector and default HashMap factory. |
| 39 | + */ |
| 40 | + public static <T, K> OnSubscribeFunc<Map<K, T>> toMap(Observable<T> source, |
| 41 | + Func1<? super T, ? extends K> keySelector) { |
| 42 | + return new ToMap<T, K, T>(source, keySelector, |
| 43 | + Functions.<T>identity(), new DefaultToMapFactory<K, T>()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * ToMap with key selector, value selector and default HashMap factory. |
| 48 | + */ |
| 49 | + public static <T, K, V> OnSubscribeFunc<Map<K, V>> toMap(Observable<T> source, |
| 50 | + Func1<? super T, ? extends K> keySelector, |
| 51 | + Func1<? super T, ? extends V> valueSelector) { |
| 52 | + return new ToMap<T, K, V>(source, keySelector, |
| 53 | + valueSelector, new DefaultToMapFactory<K, V>()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * ToMap with key selector, value selector and custom Map factory. |
| 58 | + */ |
| 59 | + public static <T, K, V> OnSubscribeFunc<Map<K, V>> toMap(Observable<T> source, |
| 60 | + Func1<? super T, ? extends K> keySelector, |
| 61 | + Func1<? super T, ? extends V> valueSelector, |
| 62 | + Func0<? extends Map<K, V>> mapFactory) { |
| 63 | + return new ToMap<T, K, V>(source, keySelector, |
| 64 | + valueSelector, mapFactory); |
| 65 | + } |
| 66 | + |
| 67 | + /** The default map factory. */ |
| 68 | + public static class DefaultToMapFactory<K, V> implements Func0<Map<K, V>> { |
| 69 | + @Override |
| 70 | + public Map<K, V> call() { |
| 71 | + return new HashMap<K, V>(); |
| 72 | + } |
| 73 | + } |
| 74 | + /** |
| 75 | + * Maps the elements of the source observable into a java.util.Map instance |
| 76 | + * returned by the mapFactory function by using the keySelector and |
| 77 | + * valueSelector. |
| 78 | + * @param <T> the source's value type |
| 79 | + * @param <K> the key type |
| 80 | + * @param <V> the value type |
| 81 | + */ |
| 82 | + public static class ToMap<T, K, V> implements OnSubscribeFunc<Map<K, V>> { |
| 83 | + /** The source. */ |
| 84 | + private final Observable<T> source; |
| 85 | + /** Key extractor. */ |
| 86 | + private final Func1<? super T, ? extends K> keySelector; |
| 87 | + /** Value extractor. */ |
| 88 | + private final Func1<? super T, ? extends V> valueSelector; |
| 89 | + /** Map factory. */ |
| 90 | + private final Func0<? extends Map<K, V>> mapFactory; |
| 91 | + public ToMap( |
| 92 | + Observable<T> source, |
| 93 | + Func1<? super T, ? extends K> keySelector, |
| 94 | + Func1<? super T, ? extends V> valueSelector, |
| 95 | + Func0<? extends Map<K, V>> mapFactory |
| 96 | + ) { |
| 97 | + this.source = source; |
| 98 | + this.keySelector = keySelector; |
| 99 | + this.valueSelector = valueSelector; |
| 100 | + this.mapFactory = mapFactory; |
| 101 | + |
| 102 | + } |
| 103 | + @Override |
| 104 | + public Subscription onSubscribe(Observer<? super Map<K, V>> t1) { |
| 105 | + Map<K, V> map; |
| 106 | + try { |
| 107 | + map = mapFactory.call(); |
| 108 | + } catch (Throwable t) { |
| 109 | + t1.onError(t); |
| 110 | + return Subscriptions.empty(); |
| 111 | + } |
| 112 | + return source.subscribe(new ToMapObserver<K, V, T>( |
| 113 | + t1, keySelector, valueSelector, map)); |
| 114 | + } |
| 115 | + /** |
| 116 | + * Observer that collects the source values of T into |
| 117 | + * a map. |
| 118 | + */ |
| 119 | + public static class ToMapObserver<K, V, T> implements Observer<T> { |
| 120 | + /** The map. */ |
| 121 | + Map<K, V> map; |
| 122 | + /** Key extractor. */ |
| 123 | + private final Func1<? super T, ? extends K> keySelector; |
| 124 | + /** Value extractor. */ |
| 125 | + private final Func1<? super T, ? extends V> valueSelector; |
| 126 | + /** The observer who is receiving the completed map. */ |
| 127 | + private final Observer<? super Map<K, V>> t1; |
| 128 | + |
| 129 | + public ToMapObserver( |
| 130 | + Observer<? super Map<K, V>> t1, |
| 131 | + Func1<? super T, ? extends K> keySelector, |
| 132 | + Func1<? super T, ? extends V> valueSelector, |
| 133 | + Map<K, V> map) { |
| 134 | + this.map = map; |
| 135 | + this.t1 = t1; |
| 136 | + this.keySelector = keySelector; |
| 137 | + this.valueSelector = valueSelector; |
| 138 | + } |
| 139 | + @Override |
| 140 | + public void onNext(T args) { |
| 141 | + K key = keySelector.call(args); |
| 142 | + V value = valueSelector.call(args); |
| 143 | + map.put(key, value); |
| 144 | + } |
| 145 | + @Override |
| 146 | + public void onError(Throwable e) { |
| 147 | + map = null; |
| 148 | + t1.onError(e); |
| 149 | + } |
| 150 | + @Override |
| 151 | + public void onCompleted() { |
| 152 | + Map<K, V> map0 = map; |
| 153 | + map = null; |
| 154 | + t1.onNext(map0); |
| 155 | + t1.onCompleted(); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments