Skip to content

Manual Merge of toMap/toMultiMap #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,143 @@ def class ObservableTests {
assertEquals(6, count);
}

@Test
public void testToMap1() {
Map actual = new HashMap();

Observable.from("a", "bb", "ccc", "dddd")
.toMap({String s -> s.length()})
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();
expected.put(1, "a");
expected.put(2, "bb");
expected.put(3, "ccc");
expected.put(4, "dddd");

assertEquals(expected, actual);
}

@Test
public void testToMap2() {
Map actual = new HashMap();

Observable.from("a", "bb", "ccc", "dddd")
.toMap({String s -> s.length()}, {String s -> s + s})
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();
expected.put(1, "aa");
expected.put(2, "bbbb");
expected.put(3, "cccccc");
expected.put(4, "dddddddd");

assertEquals(expected, actual);
}

@Test
public void testToMap3() {
Map actual = new HashMap();

LinkedHashMap last3 = new LinkedHashMap() {
public boolean removeEldestEntry(Map.Entry e) {
return size() > 3;
}
};

Observable.from("a", "bb", "ccc", "dddd")
.toMap({String s -> s.length()}, {String s -> s + s}, { last3 })
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();
expected.put(2, "bbbb");
expected.put(3, "cccccc");
expected.put(4, "dddddddd");

assertEquals(expected, actual);
}
@Test
public void testToMultimap1() {
Map actual = new HashMap();

Observable.from("a", "b", "cc", "dd")
.toMultimap({String s -> s.length()})
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();

expected.put(1, Arrays.asList("a", "b"));
expected.put(2, Arrays.asList("cc", "dd"));

assertEquals(expected, actual);
}

@Test
public void testToMultimap2() {
Map actual = new HashMap();

Observable.from("a", "b", "cc", "dd")
.toMultimap({String s -> s.length()}, {String s -> s + s})
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();

expected.put(1, Arrays.asList("aa", "bb"));
expected.put(2, Arrays.asList("cccc", "dddd"));

assertEquals(expected, actual);
}

@Test
public void testToMultimap3() {
Map actual = new HashMap();

LinkedHashMap last1 = new LinkedHashMap() {
public boolean removeEldestEntry(Map.Entry e) {
return size() > 1;
}
};

Observable.from("a", "b", "cc", "dd")
.toMultimap({String s -> s.length()}, {String s -> s + s}, { last1 })
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();

expected.put(2, Arrays.asList("cccc", "dddd"));

assertEquals(expected, actual);
}

@Test
public void testToMultimap4() {
Map actual = new HashMap();

LinkedHashMap last1 = new LinkedHashMap() {
public boolean removeEldestEntry(Map.Entry e) {
return size() > 2;
}
};

Observable.from("a", "b", "cc", "dd", "eee", "eee")
.toMultimap({String s -> s.length()}, {String s -> s + s}, { last1 },
{i -> i == 2 ? new ArrayList() : new HashSet() })
.toBlockingObservable()
.forEach({s -> actual.putAll(s); });

Map expected = new HashMap();

expected.put(2, Arrays.asList("cccc", "dddd"));
expected.put(3, new HashSet(Arrays.asList("eeeeee")));

assertEquals(expected, actual);
}

def class AsyncObservable implements OnSubscribeFunc {

Expand Down
128 changes: 128 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -87,6 +89,8 @@
import rx.operators.OperationTimeInterval;
import rx.operators.OperationTimeout;
import rx.operators.OperationTimestamp;
import rx.operators.OperationToMap;
import rx.operators.OperationToMultimap;
import rx.operators.OperationToObservableFuture;
import rx.operators.OperationToObservableIterable;
import rx.operators.OperationToObservableList;
Expand Down Expand Up @@ -5943,6 +5947,7 @@ public static <R> Observable<R> when(Plan0<R> p1, Plan0<R> p2, Plan0<R> p3, Plan
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) {
return create(OperationJoinPatterns.when(p1, p2, p3, p4, p5, p6, p7, p8, p9));
}

/**
* Correlates the elements of two sequences based on overlapping durations.
* @param right The right observable sequence to join elements for.
Expand All @@ -5964,5 +5969,128 @@ public <TRight, TLeftDuration, TRightDuration, R> Observable<R> join(Observable<
Func2<T, TRight, R> resultSelector) {
return create(new OperationJoin<T, TRight, TLeftDuration, TRightDuration, R>(this, right, leftDurationSelector, rightDurationSelector, resultSelector));
}

/**
* Return an Observable that emits a single HashMap containing all items
* emitted by the source Observable, mapped by the keys returned by the
* {@code keySelector} function.
*
* If a source item maps to the same key, the HashMap will contain the latest
* of those items.
*
* @param keySelector the function that extracts the key from the source items
* to be used as keys in the HashMap.
* @return an Observable that emits a single HashMap containing the mapped
* values of the source Observable
* @see <a href='http://msdn.microsoft.com/en-us/library/hh229137(v=vs.103).aspx'>MSDN: Observable.ToDictionary</a>
*/
public <K> Observable<Map<K, T>> toMap(Func1<? super T, ? extends K> keySelector) {
return create(OperationToMap.toMap(this, keySelector));
}

/**
* Return an Observable that emits a single HashMap containing elements with
* key and value extracted from the values emitted by the source Observable.
*
* If a source item maps to the same key, the HashMap will contain the latest
* of those items.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the HashMap
* @param valueSelector the function that extracts the value from the source items
* to be used as value in the HashMap
* @return an Observable that emits a single HashMap containing the mapped
* values of the source Observable
* @see <a href='http://msdn.microsoft.com/en-us/library/hh212075(v=vs.103).aspx'>MSDN: Observable.ToDictionary</a>
*/
public <K, V> Observable<Map<K, V>> toMap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector) {
return create(OperationToMap.toMap(this, keySelector, valueSelector));
}

/**
* Return an Observable that emits a single Map, returned by the mapFactory function,
* containing key and value extracted from the values emitted by the source Observable.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the Map
* @param valueSelector the function that extracts the value from the source items
* to be used as value in the Map
* @param mapFactory the function that returns an Map instance to be used
* @return an Observable that emits a single Map containing the mapped
* values of the source Observable
*/
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) {
return create(OperationToMap.toMap(this, keySelector, valueSelector, mapFactory));
}

/**
* Return an Observable that emits a single HashMap containing an ArrayList of elements,
* emitted by the source Observable and keyed by the keySelector function.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the HashMap
* @return an Observable that emits a single HashMap containing an ArrayList of elements
* mapped from the source Observable
* @see <a href='http://msdn.microsoft.com/en-us/library/hh212098(v=vs.103).aspx'>MSDN: Observable.ToLookup</a>
*/
public <K> Observable<Map<K, Collection<T>>> toMultimap(Func1<? super T, ? extends K> keySelector) {
return create(OperationToMultimap.toMultimap(this, keySelector));
}

/**
* Return an Observable that emits a single HashMap containing an ArrayList of values,
* extracted by the valueSelector function, emitted by the source Observable
* and keyed by the keySelector function.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the HashMap
* @param valueSelector the function that extracts the value from the source items
* to be used as value in the Map
* @return an Observable that emits a single HashMap containing an ArrayList of elements
* mapped from the source Observable
*
* @see <a href='http://msdn.microsoft.com/en-us/library/hh229101(v=vs.103).aspx'>MSDN: Observable.ToLookup</a>
*/
public <K, V> Observable<Map<K, Collection<V>>> toMultimap(Func1<? super T, ? extends K> keySelector, Func1<? super T, ? extends V> valueSelector) {
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector));
}

/**
* Return an Observable that emits a single Map, returned by the mapFactory function,
* containing an ArrayList of values, extracted by the valueSelector function,
* emitted by the source Observable and keyed by the
* keySelector function.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the Map
* @param valueSelector the function that extracts the value from the source items
* to be used as value in the Map
* @param mapFactory the function that returns an Map instance to be used
* @return an Observable that emits a single Map containing the list of mapped values
* of the source observable.
*/
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) {
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector, mapFactory));
}

/**
* Return an Observable that emits a single Map, returned by the mapFactory function,
* containing a custom collection of values, extracted by the valueSelector function,
* emitted by the source Observable and keyed by the
* keySelector function.
*
* @param keySelector the function that extracts the key from the source items
* to be used as key in the Map
* @param valueSelector the function that extracts the value from the source items
* to be used as value in the Map
* @param mapFactory the function that returns an Map instance to be used
* @param collectionFactory the function that returns a Collection instance for
* a particular key to be used in the Map
* @return an Observable that emits a single Map containing the collection of mapped values
* of the source observable.
*/
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) {
return create(OperationToMultimap.toMultimap(this, keySelector, valueSelector, mapFactory, collectionFactory));
}
}

Loading