Skip to content

Commit

Permalink
Merge pull request #18 from thomasnield/master
Browse files Browse the repository at this point in the history
GuavaTransformers
  • Loading branch information
abersnaze committed Aug 22, 2015
2 parents 4e1568c + ef7adcf commit c9ad21a
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/main/java/rx/transformer/GuavaTransformers.java
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>&gt 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&lt;T,ImmutableSet&lt;T&gt;&gt that maps an Observable&lt;T&gt; to an Observable&lt;ImmutableSet&lt;T&gt;&gt;
*/
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&lt;T,ImmutableMap&lt;K,V&gt;&gt that maps an Observable&lt;T&gt; to an Observable&lt;ImmutableMap&lt;K,V&gt;&gt><br><br>
* with a given Func1&lt;T,K&gt; keyMapper and Func1&lt;T,V&gt; 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&lt;T,ImmutableListMultimap&lt;K,V&gt;&gt that maps an Observable&lt;T&gt; to an Observable&lt;ImmutableListMultimap&lt;K,V&gt;&gt><br><br>
* with a given Func1&lt;T,K&gt; keyMapper and Func1&lt;T,V&gt; 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();
}
});
}
};
}
}
51 changes: 51 additions & 0 deletions src/test/java/rx/transformer/GuavaTransformersTest.java
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());
}
}

0 comments on commit c9ad21a

Please sign in to comment.