-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from thomasnield/master
GuavaTransformers
- Loading branch information
Showing
2 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package rx.transformer; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableListMultimap; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import rx.Observable; | ||
import rx.functions.Action2; | ||
import rx.functions.Func0; | ||
import rx.functions.Func1; | ||
|
||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* A class of static factory methods to transform an Observable into an Observable of Guauva collections | ||
*/ | ||
public final class GuavaTransformers { | ||
|
||
private GuavaTransformers() {} | ||
|
||
/** | ||
* Returns a Transformer<T,ImmutableList<T>> that maps an Observable<T> to an Observable<ImmutableList<T>> | ||
*/ | ||
public static <T> Observable.Transformer<T,ImmutableList<T>> toImmutableList() { | ||
return new Observable.Transformer<T, ImmutableList<T>>() { | ||
@Override | ||
public Observable<ImmutableList<T>> call(Observable<T> source) { | ||
return source.collect(new Func0<ImmutableList.Builder<T>>() { | ||
@Override | ||
public ImmutableList.Builder<T> call() { | ||
return ImmutableList.builder(); | ||
} | ||
}, new Action2<ImmutableList.Builder<T>, T>() { | ||
@Override | ||
public void call(ImmutableList.Builder<T> builder, T t) { | ||
builder.add(t); | ||
} | ||
}) | ||
.map(new Func1<ImmutableList.Builder<T>, ImmutableList<T>>() { | ||
@Override | ||
public ImmutableList<T> call(ImmutableList.Builder<T> builder) { | ||
return builder.build(); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
/** | ||
* Returns a Transformer<T,ImmutableSet<T>> that maps an Observable<T> to an Observable<ImmutableSet<T>> | ||
*/ | ||
public static <T> Observable.Transformer<T,ImmutableSet<T>> toImmutableSet() { | ||
return new Observable.Transformer<T, ImmutableSet<T>>() { | ||
@Override | ||
public Observable<ImmutableSet<T>> call(Observable<T> source) { | ||
return source.collect(new Func0<ImmutableSet.Builder<T>>() { | ||
@Override | ||
public ImmutableSet.Builder<T> call() { | ||
return ImmutableSet.builder(); | ||
} | ||
}, new Action2<ImmutableSet.Builder<T>, T>() { | ||
@Override | ||
public void call(ImmutableSet.Builder<T> builder, T t) { | ||
builder.add(t); | ||
} | ||
}) | ||
.map(new Func1<ImmutableSet.Builder<T>, ImmutableSet<T>>() { | ||
@Override | ||
public ImmutableSet<T> call(ImmutableSet.Builder<T> builder) { | ||
return builder.build(); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
/** | ||
* Returns a Transformer<T,ImmutableMap<K,V>> that maps an Observable<T> to an Observable<ImmutableMap<K,V>>><br><br> | ||
* with a given Func1<T,K> keyMapper and Func1<T,V> valueMapper | ||
*/ | ||
public static <T,K,V> Observable.Transformer<T,ImmutableMap<K,V>> toImmutableMap(final Func1<T,K> keyMapper, final Func1<T,V> valueMapper) { | ||
return new Observable.Transformer<T,ImmutableMap<K,V>>() { | ||
@Override | ||
public Observable<ImmutableMap<K, V>> call(Observable<T> observable) { | ||
return observable.collect(new Func0<ImmutableMap.Builder<K, V>>() { | ||
@Override | ||
public ImmutableMap.Builder<K, V> call() { | ||
return ImmutableMap.builder(); | ||
} | ||
}, new Action2<ImmutableMap.Builder<K, V>, T>() { | ||
@Override | ||
public void call(ImmutableMap.Builder<K, V> builder, T t) { | ||
builder.put(keyMapper.call(t), valueMapper.call(t)); | ||
} | ||
}) | ||
.map(new Func1<ImmutableMap.Builder<K, V>, ImmutableMap<K, V>>() { | ||
@Override | ||
public ImmutableMap<K, V> call(ImmutableMap.Builder<K, V> builder) { | ||
return builder.build(); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a Transformer<T,ImmutableListMultimap<K,V>> that maps an Observable<T> to an Observable<ImmutableListMultimap<K,V>>><br><br> | ||
* with a given Func1<T,K> keyMapper and Func1<T,V> valueMapper | ||
*/ | ||
public static <T,K,V> Observable.Transformer<T,ImmutableListMultimap<K,V>> toImmutableListMultimap(final Func1<T,K> keyMapper, final Func1<T,V> valueMapper) { | ||
return new Observable.Transformer<T,ImmutableListMultimap<K,V>>() { | ||
@Override | ||
public Observable<ImmutableListMultimap<K, V>> call(Observable<T> observable) { | ||
return observable.collect(new Func0<ImmutableListMultimap.Builder<K, V>>() { | ||
@Override | ||
public ImmutableListMultimap.Builder<K, V> call() { | ||
return ImmutableListMultimap.builder(); | ||
} | ||
}, new Action2<ImmutableListMultimap.Builder<K, V>, T>() { | ||
@Override | ||
public void call(ImmutableListMultimap.Builder<K, V> builder, T t) { | ||
builder.put(keyMapper.call(t), valueMapper.call(t)); | ||
} | ||
}) | ||
.map(new Func1<ImmutableListMultimap.Builder<K, V>, ImmutableListMultimap<K, V>>() { | ||
@Override | ||
public ImmutableListMultimap<K, V> call(ImmutableListMultimap.Builder<K, V> builder) { | ||
return builder.build(); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package rx.transformer; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableListMultimap; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.Observable; | ||
import rx.functions.Func1; | ||
|
||
public class GuavaTransformersTest { | ||
@Test | ||
public void testList() { | ||
assertEquals(ImmutableList.builder().add(1).add(3).add(2).add(3).build(), Observable.just(1, 3, 2, 3).compose(GuavaTransformers.toImmutableList()).toBlocking().last()); | ||
} | ||
|
||
@Test | ||
public void testSet() { | ||
assertEquals(ImmutableSet.builder().add(1).add(3).add(2).add(3).build(), Observable.just(1, 3, 2, 3).compose(GuavaTransformers.toImmutableSet()).toBlocking().last()); | ||
} | ||
|
||
Func1<String, String> keyMapper = new Func1<String, String>() { | ||
@Override | ||
public String call(String t) { | ||
return t.split(":")[0]; | ||
} | ||
}; | ||
|
||
Func1<String, String> valueMapper = new Func1<String, String>() { | ||
@Override | ||
public String call(String t) { | ||
return t.split(":")[1]; | ||
} | ||
}; | ||
|
||
@Test | ||
public void testMap() { | ||
assertEquals(ImmutableMap.builder().put("a", "1").put("b", "2").put("c", "3").build(), Observable.just("a:1", "b:2", "c:3").compose(GuavaTransformers.toImmutableMap(keyMapper, valueMapper)) | ||
.toBlocking().last()); | ||
} | ||
|
||
@Test | ||
public void testListMap() { | ||
assertEquals(ImmutableListMultimap.builder().put("a", "1").putAll("b", "2", "4").put("c", "3").build(), Observable.just("a:1", "b:2", "c:3", "b:4").compose( | ||
GuavaTransformers.toImmutableListMultimap(keyMapper, valueMapper)).toBlocking().last()); | ||
} | ||
} |