Skip to content

Commit e74d0be

Browse files
committed
Operations toMap (3) and toMultimap (4)
1 parent d511fa3 commit e74d0be

File tree

6 files changed

+1094
-0
lines changed

6 files changed

+1094
-0
lines changed

language-adaptors/rxjava-groovy/src/test/groovy/rx/lang/groovy/ObservableTests.groovy

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,143 @@ def class ObservableTests {
345345
assertEquals(6, count);
346346
}
347347

348+
@Test
349+
public void testToMap1() {
350+
Map actual = new HashMap();
351+
352+
Observable.from("a", "bb", "ccc", "dddd")
353+
.toMap({String s -> s.length()})
354+
.toBlockingObservable()
355+
.forEach({s -> actual.putAll(s); });
356+
357+
Map expected = new HashMap();
358+
expected.put(1, "a");
359+
expected.put(2, "bb");
360+
expected.put(3, "ccc");
361+
expected.put(4, "dddd");
362+
363+
assertEquals(expected, actual);
364+
}
365+
366+
@Test
367+
public void testToMap2() {
368+
Map actual = new HashMap();
369+
370+
Observable.from("a", "bb", "ccc", "dddd")
371+
.toMap({String s -> s.length()}, {String s -> s + s})
372+
.toBlockingObservable()
373+
.forEach({s -> actual.putAll(s); });
374+
375+
Map expected = new HashMap();
376+
expected.put(1, "aa");
377+
expected.put(2, "bbbb");
378+
expected.put(3, "cccccc");
379+
expected.put(4, "dddddddd");
380+
381+
assertEquals(expected, actual);
382+
}
383+
384+
@Test
385+
public void testToMap3() {
386+
Map actual = new HashMap();
387+
388+
LinkedHashMap last3 = new LinkedHashMap() {
389+
public boolean removeEldestEntry(Map.Entry e) {
390+
return size() > 3;
391+
}
392+
};
393+
394+
Observable.from("a", "bb", "ccc", "dddd")
395+
.toMap({String s -> s.length()}, {String s -> s + s}, { last3 })
396+
.toBlockingObservable()
397+
.forEach({s -> actual.putAll(s); });
398+
399+
Map expected = new HashMap();
400+
expected.put(2, "bbbb");
401+
expected.put(3, "cccccc");
402+
expected.put(4, "dddddddd");
403+
404+
assertEquals(expected, actual);
405+
}
406+
@Test
407+
public void testToMultimap1() {
408+
Map actual = new HashMap();
409+
410+
Observable.from("a", "b", "cc", "dd")
411+
.toMultimap({String s -> s.length()})
412+
.toBlockingObservable()
413+
.forEach({s -> actual.putAll(s); });
414+
415+
Map expected = new HashMap();
416+
417+
expected.put(1, Arrays.asList("a", "b"));
418+
expected.put(2, Arrays.asList("cc", "dd"));
419+
420+
assertEquals(expected, actual);
421+
}
422+
423+
@Test
424+
public void testToMultimap2() {
425+
Map actual = new HashMap();
426+
427+
Observable.from("a", "b", "cc", "dd")
428+
.toMultimap({String s -> s.length()}, {String s -> s + s})
429+
.toBlockingObservable()
430+
.forEach({s -> actual.putAll(s); });
431+
432+
Map expected = new HashMap();
433+
434+
expected.put(1, Arrays.asList("aa", "bb"));
435+
expected.put(2, Arrays.asList("cccc", "dddd"));
436+
437+
assertEquals(expected, actual);
438+
}
439+
440+
@Test
441+
public void testToMultimap3() {
442+
Map actual = new HashMap();
443+
444+
LinkedHashMap last1 = new LinkedHashMap() {
445+
public boolean removeEldestEntry(Map.Entry e) {
446+
return size() > 1;
447+
}
448+
};
449+
450+
Observable.from("a", "b", "cc", "dd")
451+
.toMultimap({String s -> s.length()}, {String s -> s + s}, { last1 })
452+
.toBlockingObservable()
453+
.forEach({s -> actual.putAll(s); });
454+
455+
Map expected = new HashMap();
456+
457+
expected.put(2, Arrays.asList("cccc", "dddd"));
458+
459+
assertEquals(expected, actual);
460+
}
461+
462+
@Test
463+
public void testToMultimap4() {
464+
Map actual = new HashMap();
465+
466+
LinkedHashMap last1 = new LinkedHashMap() {
467+
public boolean removeEldestEntry(Map.Entry e) {
468+
return size() > 2;
469+
}
470+
};
471+
472+
Observable.from("a", "b", "cc", "dd", "eee", "eee")
473+
.toMultimap({String s -> s.length()}, {String s -> s + s}, { last1 },
474+
{i -> i == 2 ? new ArrayList() : new HashSet() })
475+
.toBlockingObservable()
476+
.forEach({s -> actual.putAll(s); });
477+
478+
Map expected = new HashMap();
479+
480+
expected.put(2, Arrays.asList("cccc", "dddd"));
481+
expected.put(3, new HashSet(Arrays.asList("eeeeee")));
482+
483+
assertEquals(expected, actual);
484+
}
348485

349486
def class AsyncObservable implements OnSubscribeFunc {
350487

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22+
import java.util.Collection;
2223
import java.util.Comparator;
2324
import java.util.List;
25+
import java.util.Map;
2426
import java.util.concurrent.ConcurrentHashMap;
2527
import java.util.concurrent.Future;
2628
import java.util.concurrent.TimeUnit;
@@ -86,6 +88,8 @@
8688
import rx.operators.OperationTimeInterval;
8789
import rx.operators.OperationTimeout;
8890
import rx.operators.OperationTimestamp;
91+
import rx.operators.OperationToMap;
92+
import rx.operators.OperationToMultimap;
8993
import rx.operators.OperationToObservableFuture;
9094
import rx.operators.OperationToObservableIterable;
9195
import rx.operators.OperationToObservableList;
@@ -5861,4 +5865,127 @@ public static <R> Observable<R> when(Plan0<R> p1, Plan0<R> p2, Plan0<R> p3, Plan
58615865
public static <R> Observable<R> when(Plan0<R> p1, Plan0<R> p2, Plan0<R> p3, Plan0<R> p4, Plan0<R> p5, Plan0<R> p6, Plan0<R> p7, Plan0<R> p8, Plan0<R> p9) {
58625866
return create(OperationJoinPatterns.when(p1, p2, p3, p4, p5, p6, p7, p8, p9));
58635867
}
5868+
5869+
/**
5870+
* Return an Observable that emits a single HashMap containing all items
5871+
* emitted by the source Observable, mapped by the keys returned by the
5872+
* {@code keySelector} function.
5873+
*
5874+
* If a source item maps to the same key, the HashMap will contain the latest
5875+
* of those items.
5876+
*
5877+
* @param keySelector the function that extracts the key from the source items
5878+
* to be used as keys in the HashMap.
5879+
* @return an Observable that emits a single HashMap containing the mapped
5880+
* values of the source Observable
5881+
* @see <a href='http://msdn.microsoft.com/en-us/library/hh229137(v=vs.103).aspx'>MSDN: Observable.ToDictionary</a>
5882+
*/
5883+
public <K> Observable<Map<K, T>> toMap(Func1<? super T, ? extends K> keySelector) {
5884+
return create(OperationToMap.toMap(this, keySelector));
5885+
}
5886+
5887+
/**
5888+
* Return an Observable that emits a single HashMap containing elements with
5889+
* key and value extracted from the values emitted by the source Observable.
5890+
*
5891+
* If a source item maps to the same key, the HashMap will contain the latest
5892+
* of those items.
5893+
*
5894+
* @param keySelector the function that extracts the key from the source items
5895+
* to be used as key in the HashMap
5896+
* @param valueSelector the function that extracts the value from the source items
5897+
* to be used as value in the HashMap
5898+
* @return an Observable that emits a single HashMap containing the mapped
5899+
* values of the source Observable
5900+
* @see <a href='http://msdn.microsoft.com/en-us/library/hh212075(v=vs.103).aspx'>MSDN: Observable.ToDictionary</a>
5901+
*/
5902+
public <K, V> Observable<Map<K, V>> toMap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector) {
5903+
return create(OperationToMap.toMap(this, keySelector, valueSelector));
5904+
}
5905+
5906+
/**
5907+
* Return an Observable that emits a single Map, returned by the mapFactory function,
5908+
* containing key and value extracted from the values emitted by the source Observable.
5909+
*
5910+
* @param keySelector the function that extracts the key from the source items
5911+
* to be used as key in the Map
5912+
* @param valueSelector the function that extracts the value from the source items
5913+
* to be used as value in the Map
5914+
* @param mapFactory the function that returns an Map instance to be used
5915+
* @return an Observable that emits a single Map containing the mapped
5916+
* values of the source Observable
5917+
*/
5918+
public <K, V> Observable<Map<K, V>> toMap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector, Func0<? extends Map<K, V>> mapFactory) {
5919+
return create(OperationToMap.toMap(this, keySelector, valueSelector, mapFactory));
5920+
}
5921+
5922+
/**
5923+
* Return an Observable that emits a single HashMap containing an ArrayList of elements,
5924+
* emitted by the source Observable and keyed by the keySelector function.
5925+
*
5926+
* @param keySelector the function that extracts the key from the source items
5927+
* to be used as key in the HashMap
5928+
* @return an Observable that emits a single HashMap containing an ArrayList of elements
5929+
* mapped from the source Observable
5930+
* @see <a href='http://msdn.microsoft.com/en-us/library/hh212098(v=vs.103).aspx'>MSDN: Observable.ToLookup</a>
5931+
*/
5932+
public <K> Observable<Map<K, Collection<T>>> toMultimap(Func1<? super T, ? extends K> keySelector) {
5933+
return create(OperationToMultimap.toMultimap(this, keySelector));
5934+
}
5935+
5936+
/**
5937+
* Return an Observable that emits a single HashMap containing an ArrayList of values,
5938+
* extracted by the valueSelector function, emitted by the source Observable
5939+
* and keyed by the keySelector function.
5940+
*
5941+
* @param keySelector the function that extracts the key from the source items
5942+
* to be used as key in the HashMap
5943+
* @param valueSelector the function that extracts the value from the source items
5944+
* to be used as value in the Map
5945+
* @return an Observable that emits a single HashMap containing an ArrayList of elements
5946+
* mapped from the source Observable
5947+
*
5948+
* @see <a href='http://msdn.microsoft.com/en-us/library/hh229101(v=vs.103).aspx'>MSDN: Observable.ToLookup</a>
5949+
*/
5950+
public <K, V> Observable<Map<K, Collection<V>>> toMultimap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector) {
5951+
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector));
5952+
}
5953+
5954+
/**
5955+
* Return an Observable that emits a single Map, returned by the mapFactory function,
5956+
* containing an ArrayList of values, extracted by the valueSelector function,
5957+
* emitted by the source Observable and keyed by the
5958+
* keySelector function.
5959+
*
5960+
* @param keySelector the function that extracts the key from the source items
5961+
* to be used as key in the Map
5962+
* @param valueSelector the function that extracts the value from the source items
5963+
* to be used as value in the Map
5964+
* @param mapFactory the function that returns an Map instance to be used
5965+
* @return an Observable that emits a single Map containing the list of mapped values
5966+
* of the source observable.
5967+
*/
5968+
public <K, V> Observable<Map<K, Collection<V>>> toMultimap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector, Func0<? extends Map<K, Collection<V>>> mapFactory) {
5969+
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector, mapFactory));
5970+
}
5971+
5972+
/**
5973+
* Return an Observable that emits a single Map, returned by the mapFactory function,
5974+
* containing a custom collection of values, extracted by the valueSelector function,
5975+
* emitted by the source Observable and keyed by the
5976+
* keySelector function.
5977+
*
5978+
* @param keySelector the function that extracts the key from the source items
5979+
* to be used as key in the Map
5980+
* @param valueSelector the function that extracts the value from the source items
5981+
* to be used as value in the Map
5982+
* @param mapFactory the function that returns an Map instance to be used
5983+
* @param collectionFactory the function that returns a Collection instance for
5984+
* a particular key to be used in the Map
5985+
* @return an Observable that emits a single Map containing the collection of mapped values
5986+
* of the source observable.
5987+
*/
5988+
public <K, V> Observable<Map<K, Collection<V>>> toMultimap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector, Func0<? extends Map<K, Collection<V>>> mapFactory, Func1<? super K, ? extends Collection<V>> collectionFactory) {
5989+
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector, mapFactory, collectionFactory));
5990+
}
58645991
}

0 commit comments

Comments
 (0)