diff --git a/src/main/java/rx/Observable.java b/src/main/java/rx/Observable.java index ac5b1d3481..a8b6f1213a 100644 --- a/src/main/java/rx/Observable.java +++ b/src/main/java/rx/Observable.java @@ -12,8 +12,6 @@ */ package rx; -import static rx.functions.Functions.alwaysFalse; - import java.util.*; import java.util.concurrent.*; @@ -21,6 +19,8 @@ import rx.functions.*; import rx.internal.operators.*; import rx.internal.util.ScalarSynchronousObservable; +import rx.internal.util.UtilityFunctions; + import rx.observables.*; import rx.observers.SafeSubscriber; import rx.plugins.*; @@ -4030,7 +4030,7 @@ public final Observable dematerialize() { * @see MSDN: Observable.distinct */ public final Observable distinct() { - return lift(new OperatorDistinct(Functions.identity())); + return lift(new OperatorDistinct(UtilityFunctions.identity())); } /** @@ -4070,7 +4070,7 @@ public final Observable distinct(Func1 keySelecto * @see MSDN: Observable.distinctUntilChanged */ public final Observable distinctUntilChanged() { - return lift(new OperatorDistinctUntilChanged(Functions.identity())); + return lift(new OperatorDistinctUntilChanged(UtilityFunctions.identity())); } /** @@ -4854,7 +4854,7 @@ public final Observable groupJoin(Observable right, Func1 * @see MSDN: Observable.IgnoreElements */ public final Observable ignoreElements() { - return filter(alwaysFalse()); + return filter(UtilityFunctions.alwaysFalse()); } /** @@ -4874,7 +4874,7 @@ public final Observable ignoreElements() { * @see MSDN: Observable.Any */ public final Observable isEmpty() { - return lift(new OperatorAny(Functions.alwaysTrue(), true)); + return lift(new OperatorAny(UtilityFunctions.alwaysTrue(), true)); } /** @@ -8481,7 +8481,7 @@ public final Observable> toList() { * @see MSDN: Observable.ToDictionary */ public final Observable> toMap(Func1 keySelector) { - return lift(new OperatorToMap(keySelector, Functions.identity())); + return lift(new OperatorToMap(keySelector, UtilityFunctions.identity())); } /** @@ -8558,7 +8558,7 @@ public final Observable> toMap(Func1 ke * @see MSDN: Observable.ToLookup */ public final Observable>> toMultimap(Func1 keySelector) { - return lift(new OperatorToMultimap(keySelector, Functions.identity())); + return lift(new OperatorToMultimap(keySelector, UtilityFunctions.identity())); } /** diff --git a/src/main/java/rx/functions/Functions.java b/src/main/java/rx/functions/Functions.java index 0baa4906ed..4eb7ca111a 100644 --- a/src/main/java/rx/functions/Functions.java +++ b/src/main/java/rx/functions/Functions.java @@ -330,134 +330,4 @@ public Void call(Object... args) { }; } - /** - * Returns a function that always returns {@code true}. - * - * @return a {@link Func1} that accepts an Object and returns the Boolean {@code true} - */ - public static Func1 alwaysTrue() { - return AlwaysTrue.INSTANCE; - } - - /** - * Returns a function that always returns {@code false}. - * - * @return a {@link Func1} that accepts an Object and returns the Boolean {@code false} - */ - public static Func1 alwaysFalse() { - return AlwaysFalse.INSTANCE; - } - - /** - * Returns a function that always returns the Object it is passed. - * - * @return a {@link Func1} that accepts an Object and returns the same Object - */ - public static Func1 identity() { - return new Func1() { - @Override - public T call(T o) { - return o; - } - }; - } - - private enum AlwaysTrue implements Func1 { - INSTANCE; - - @Override - public Boolean call(Object o) { - return true; - } - } - - private enum AlwaysFalse implements Func1 { - INSTANCE; - - @Override - public Boolean call(Object o) { - return false; - } - } - - /** - * Returns a function that merely returns {@code null}, without side effects. - * - * @return a function that returns {@code null} - */ - @SuppressWarnings("unchecked") - public static NullFunction returnNull() { - return NULL_FUNCTION; - } - - @SuppressWarnings("rawtypes") - private static final NullFunction NULL_FUNCTION = new NullFunction(); - - private static final class NullFunction implements - Func0, - Func1, - Func2, - Func3, - Func4, - Func5, - Func6, - Func7, - Func8, - Func9, - FuncN { - @Override - public R call() { - return null; - } - - @Override - public R call(T0 t1) { - return null; - } - - @Override - public R call(T0 t1, T1 t2) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8) { - return null; - } - - @Override - public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8, T8 t9) { - return null; - } - - @Override - public R call(Object... args) { - return null; - } - } } diff --git a/src/main/java/rx/internal/operators/OperatorSequenceEqual.java b/src/main/java/rx/internal/operators/OperatorSequenceEqual.java index 24d32b2cac..b03855f63a 100644 --- a/src/main/java/rx/internal/operators/OperatorSequenceEqual.java +++ b/src/main/java/rx/internal/operators/OperatorSequenceEqual.java @@ -21,7 +21,7 @@ import rx.Observable; import rx.functions.Func1; import rx.functions.Func2; -import rx.functions.Functions; +import rx.internal.util.UtilityFunctions; /** * Returns an {@link Observable} that emits a single {@code Boolean} value that indicates whether two source @@ -84,6 +84,6 @@ public Boolean call(Object t1, Object t2) { return equality.call((T)t1, (T)t2); } - }).all(Functions. identity()); + }).all(UtilityFunctions. identity()); } } diff --git a/src/main/java/rx/internal/util/UtilityFunctions.java b/src/main/java/rx/internal/util/UtilityFunctions.java new file mode 100644 index 0000000000..2a94cb95f9 --- /dev/null +++ b/src/main/java/rx/internal/util/UtilityFunctions.java @@ -0,0 +1,163 @@ +/** + * 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. + */ +package rx.internal.util; + +import rx.functions.Func0; +import rx.functions.Func1; +import rx.functions.Func2; +import rx.functions.Func3; +import rx.functions.Func4; +import rx.functions.Func5; +import rx.functions.Func6; +import rx.functions.Func7; +import rx.functions.Func8; +import rx.functions.Func9; +import rx.functions.FuncN; + +/** + * Utility functions for internal use that we don't want part of the public API. + */ +public final class UtilityFunctions { + + /** + * Returns a function that always returns {@code true}. + * + * @return a {@link Func1} that accepts an Object and returns the Boolean {@code true} + */ + public static Func1 alwaysTrue() { + return AlwaysTrue.INSTANCE; + } + + /** + * Returns a function that always returns {@code false}. + * + * @return a {@link Func1} that accepts an Object and returns the Boolean {@code false} + */ + public static Func1 alwaysFalse() { + return AlwaysFalse.INSTANCE; + } + + /** + * Returns a function that always returns the Object it is passed. + * + * @return a {@link Func1} that accepts an Object and returns the same Object + */ + public static Func1 identity() { + return new Func1() { + @Override + public T call(T o) { + return o; + } + }; + } + + private enum AlwaysTrue implements Func1 { + INSTANCE; + + @Override + public Boolean call(Object o) { + return true; + } + } + + private enum AlwaysFalse implements Func1 { + INSTANCE; + + @Override + public Boolean call(Object o) { + return false; + } + } + + /** + * Returns a function that merely returns {@code null}, without side effects. + * + * @return a function that returns {@code null} + */ + @SuppressWarnings("unchecked") + public static NullFunction returnNull() { + return NULL_FUNCTION; + } + + @SuppressWarnings("rawtypes") + private static final NullFunction NULL_FUNCTION = new NullFunction(); + + private static final class NullFunction implements + Func0, + Func1, + Func2, + Func3, + Func4, + Func5, + Func6, + Func7, + Func8, + Func9, + FuncN { + @Override + public R call() { + return null; + } + + @Override + public R call(T0 t1) { + return null; + } + + @Override + public R call(T0 t1, T1 t2) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8) { + return null; + } + + @Override + public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8, T8 t9) { + return null; + } + + @Override + public R call(Object... args) { + return null; + } + } + +} diff --git a/src/main/java/rx/observables/BlockingObservable.java b/src/main/java/rx/observables/BlockingObservable.java index b6ce665ef1..989d1a26fe 100644 --- a/src/main/java/rx/observables/BlockingObservable.java +++ b/src/main/java/rx/observables/BlockingObservable.java @@ -31,6 +31,7 @@ import rx.internal.operators.BlockingOperatorNext; import rx.internal.operators.BlockingOperatorToFuture; import rx.internal.operators.BlockingOperatorToIterator; +import rx.internal.util.UtilityFunctions; /** * An extension of {@link Observable} that provides blocking operators. @@ -193,7 +194,7 @@ public T first(Func1 predicate) { * @see MSDN: Observable.FirstOrDefault */ public T firstOrDefault(T defaultValue) { - return blockForSingle(o.map(Functions.identity()).firstOrDefault(defaultValue)); + return blockForSingle(o.map(UtilityFunctions.identity()).firstOrDefault(defaultValue)); } /** @@ -210,7 +211,7 @@ public T firstOrDefault(T defaultValue) { * @see MSDN: Observable.FirstOrDefault */ public T firstOrDefault(T defaultValue, Func1 predicate) { - return blockForSingle(o.filter(predicate).map(Functions.identity()).firstOrDefault(defaultValue)); + return blockForSingle(o.filter(predicate).map(UtilityFunctions.identity()).firstOrDefault(defaultValue)); } /** @@ -261,7 +262,7 @@ public T last(final Func1 predicate) { * @see MSDN: Observable.LastOrDefault */ public T lastOrDefault(T defaultValue) { - return blockForSingle(o.map(Functions.identity()).lastOrDefault(defaultValue)); + return blockForSingle(o.map(UtilityFunctions.identity()).lastOrDefault(defaultValue)); } /** @@ -280,7 +281,7 @@ public T lastOrDefault(T defaultValue) { * @see MSDN: Observable.LastOrDefault */ public T lastOrDefault(T defaultValue, Func1 predicate) { - return blockForSingle(o.filter(predicate).map(Functions.identity()).lastOrDefault(defaultValue)); + return blockForSingle(o.filter(predicate).map(UtilityFunctions.identity()).lastOrDefault(defaultValue)); } /** @@ -379,7 +380,7 @@ public T single(Func1 predicate) { * @see MSDN: Observable.SingleOrDefault */ public T singleOrDefault(T defaultValue) { - return blockForSingle(o.map(Functions.identity()).singleOrDefault(defaultValue)); + return blockForSingle(o.map(UtilityFunctions.identity()).singleOrDefault(defaultValue)); } /** @@ -399,7 +400,7 @@ public T singleOrDefault(T defaultValue) { * @see MSDN: Observable.SingleOrDefault */ public T singleOrDefault(T defaultValue, Func1 predicate) { - return blockForSingle(o.filter(predicate).map(Functions.identity()).singleOrDefault(defaultValue)); + return blockForSingle(o.filter(predicate).map(UtilityFunctions.identity()).singleOrDefault(defaultValue)); } /** diff --git a/src/main/java/rx/subjects/ReplaySubject.java b/src/main/java/rx/subjects/ReplaySubject.java index aa78281704..45693ddb28 100644 --- a/src/main/java/rx/subjects/ReplaySubject.java +++ b/src/main/java/rx/subjects/ReplaySubject.java @@ -28,6 +28,7 @@ import rx.functions.Func1; import rx.functions.Functions; import rx.internal.operators.NotificationLite; +import rx.internal.util.UtilityFunctions; import rx.schedulers.Timestamped; import rx.subjects.ReplaySubject.NodeList.Node; import rx.subjects.SubjectSubscriptionManager.SubjectObserver; @@ -132,8 +133,8 @@ public void call(SubjectObserver o) { /* public */ static ReplaySubject createUnbounded() { final BoundedState state = new BoundedState( new EmptyEvictionPolicy(), - Functions.identity(), - Functions.identity() + UtilityFunctions.identity(), + UtilityFunctions.identity() ); return createWithState(state, new DefaultOnAdd(state)); } @@ -160,8 +161,8 @@ public void call(SubjectObserver o) { public static ReplaySubject createWithSize(int size) { final BoundedState state = new BoundedState( new SizeEvictionPolicy(size), - Functions.identity(), - Functions.identity() + UtilityFunctions.identity(), + UtilityFunctions.identity() ); return createWithState(state, new DefaultOnAdd(state)); } diff --git a/src/test/java/rx/internal/operators/OperatorAnyTest.java b/src/test/java/rx/internal/operators/OperatorAnyTest.java index 700d6370f9..1d3d371fef 100644 --- a/src/test/java/rx/internal/operators/OperatorAnyTest.java +++ b/src/test/java/rx/internal/operators/OperatorAnyTest.java @@ -21,21 +21,22 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import java.util.Arrays; + import org.junit.Test; import rx.Observable; import rx.Observer; import rx.functions.Func1; import rx.functions.Functions; - -import java.util.Arrays; +import rx.internal.util.UtilityFunctions; public class OperatorAnyTest { @Test public void testAnyWithTwoItems() { Observable w = Observable.just(1, 2); - Observable observable = w.exists(Functions.alwaysTrue()); + Observable observable = w.exists(UtilityFunctions.alwaysTrue()); @SuppressWarnings("unchecked") Observer observer = mock(Observer.class); @@ -63,7 +64,7 @@ public void testIsEmptyWithTwoItems() { @Test public void testAnyWithOneItem() { Observable w = Observable.just(1); - Observable observable = w.exists(Functions.alwaysTrue()); + Observable observable = w.exists(UtilityFunctions.alwaysTrue()); @SuppressWarnings("unchecked") Observer observer = mock(Observer.class); @@ -91,7 +92,7 @@ public void testIsEmptyWithOneItem() { @Test public void testAnyWithEmpty() { Observable w = Observable.empty(); - Observable observable = w.exists(Functions.alwaysTrue()); + Observable observable = w.exists(UtilityFunctions.alwaysTrue()); @SuppressWarnings("unchecked") Observer observer = mock(Observer.class); diff --git a/src/test/java/rx/internal/operators/OperatorGroupByTest.java b/src/test/java/rx/internal/operators/OperatorGroupByTest.java index 33512a2c03..432266ea32 100644 --- a/src/test/java/rx/internal/operators/OperatorGroupByTest.java +++ b/src/test/java/rx/internal/operators/OperatorGroupByTest.java @@ -50,7 +50,7 @@ import rx.functions.Action0; import rx.functions.Action1; import rx.functions.Func1; -import rx.functions.Functions; +import rx.internal.util.UtilityFunctions; import rx.observables.GroupedObservable; import rx.observers.TestSubscriber; import rx.schedulers.Schedulers; @@ -1103,7 +1103,7 @@ public Integer call(Integer t1) { return t1 * 2; } }; - Func1 identity = Functions.identity(); + Func1 identity = UtilityFunctions.identity(); @Before public void before() { diff --git a/src/test/java/rx/internal/operators/OperatorReduceTest.java b/src/test/java/rx/internal/operators/OperatorReduceTest.java index da9a476966..c550c835ea 100644 --- a/src/test/java/rx/internal/operators/OperatorReduceTest.java +++ b/src/test/java/rx/internal/operators/OperatorReduceTest.java @@ -32,7 +32,7 @@ import rx.exceptions.TestException; import rx.functions.Func1; import rx.functions.Func2; -import rx.functions.Functions; +import rx.internal.util.UtilityFunctions; public class OperatorReduceTest { @Mock @@ -53,7 +53,7 @@ public Integer call(Integer t1, Integer t2) { @Test public void testAggregateAsIntSum() { - Observable result = Observable.just(1, 2, 3, 4, 5).reduce(0, sum).map(Functions. identity()); + Observable result = Observable.just(1, 2, 3, 4, 5).reduce(0, sum).map(UtilityFunctions. identity()); result.subscribe(observer); @@ -66,7 +66,7 @@ public void testAggregateAsIntSum() { public void testAggregateAsIntSumSourceThrows() { Observable result = Observable.concat(Observable.just(1, 2, 3, 4, 5), Observable. error(new TestException())) - .reduce(0, sum).map(Functions. identity()); + .reduce(0, sum).map(UtilityFunctions. identity()); result.subscribe(observer); @@ -85,7 +85,7 @@ public Integer call(Integer t1, Integer t2) { }; Observable result = Observable.just(1, 2, 3, 4, 5) - .reduce(0, sumErr).map(Functions. identity()); + .reduce(0, sumErr).map(UtilityFunctions. identity()); result.subscribe(observer); diff --git a/src/test/java/rx/internal/operators/OperatorTakeLastTest.java b/src/test/java/rx/internal/operators/OperatorTakeLastTest.java index 14f22fd2bc..0bdc422f02 100644 --- a/src/test/java/rx/internal/operators/OperatorTakeLastTest.java +++ b/src/test/java/rx/internal/operators/OperatorTakeLastTest.java @@ -32,8 +32,8 @@ import rx.Observer; import rx.Subscriber; import rx.functions.Func1; -import rx.functions.Functions; import rx.internal.util.RxRingBuffer; +import rx.internal.util.UtilityFunctions; import rx.observers.TestSubscriber; import rx.schedulers.Schedulers; @@ -156,7 +156,7 @@ public void testIssue1522() { assertEquals(0, Observable .empty() .count() - .filter(Functions.alwaysFalse()) + .filter(UtilityFunctions.alwaysFalse()) .toList() .toBlocking().single().size()); } diff --git a/src/test/java/rx/internal/operators/OperatorToMapTest.java b/src/test/java/rx/internal/operators/OperatorToMapTest.java index dec6d80b31..669b85c234 100644 --- a/src/test/java/rx/internal/operators/OperatorToMapTest.java +++ b/src/test/java/rx/internal/operators/OperatorToMapTest.java @@ -33,7 +33,7 @@ import rx.Observer; import rx.functions.Func0; import rx.functions.Func1; -import rx.functions.Functions; +import rx.internal.util.UtilityFunctions; public class OperatorToMapTest { @Mock @@ -179,7 +179,7 @@ public Integer call(String t1) { return t1.length(); } }; - Observable> mapped = source.toMap(lengthFunc, Functions.identity(), mapFactory); + Observable> mapped = source.toMap(lengthFunc, UtilityFunctions.identity(), mapFactory); Map expected = new LinkedHashMap(); expected.put(2, "bb"); @@ -210,7 +210,7 @@ public Integer call(String t1) { return t1.length(); } }; - Observable> mapped = source.toMap(lengthFunc, Functions.identity(), mapFactory); + Observable> mapped = source.toMap(lengthFunc, UtilityFunctions.identity(), mapFactory); Map expected = new LinkedHashMap(); expected.put(2, "bb"); diff --git a/src/test/java/rx/internal/operators/OperatorToMultimapTest.java b/src/test/java/rx/internal/operators/OperatorToMultimapTest.java index fa040a3644..b8f57f04f6 100644 --- a/src/test/java/rx/internal/operators/OperatorToMultimapTest.java +++ b/src/test/java/rx/internal/operators/OperatorToMultimapTest.java @@ -38,9 +38,9 @@ import rx.Observer; import rx.functions.Func0; import rx.functions.Func1; -import rx.functions.Functions; import rx.internal.operators.OperatorToMultimap.DefaultMultimapCollectionFactory; import rx.internal.operators.OperatorToMultimap.DefaultToMultimapFactory; +import rx.internal.util.UtilityFunctions; public class OperatorToMultimapTest { @Mock @@ -118,7 +118,7 @@ protected boolean removeEldestEntry(Map.Entry> eldes }; Observable>> mapped = source.toMultimap( - lengthFunc, Functions.identity(), + lengthFunc, UtilityFunctions.identity(), mapFactory, new DefaultMultimapCollectionFactory()); Map> expected = new HashMap>(); @@ -149,7 +149,7 @@ public Collection call(Integer t1) { }; Observable>> mapped = source.toMultimap( - lengthFunc, Functions.identity(), + lengthFunc, UtilityFunctions.identity(), new DefaultToMultimapFactory(), collectionFactory); Map> expected = new HashMap>(); @@ -228,7 +228,7 @@ public Map> call() { } }; - Observable>> mapped = source.toMultimap(lengthFunc, Functions.identity(), mapFactory); + Observable>> mapped = source.toMultimap(lengthFunc, UtilityFunctions.identity(), mapFactory); Map> expected = new HashMap>(); expected.put(2, Arrays.asList("cc", "dd")); @@ -257,7 +257,7 @@ public Collection call(Integer t1) { } }; - Observable>> mapped = source.toMultimap(lengthFunc, Functions.identity(), new DefaultToMultimapFactory(), collectionFactory); + Observable>> mapped = source.toMultimap(lengthFunc, UtilityFunctions.identity(), new DefaultToMultimapFactory(), collectionFactory); Map> expected = new HashMap>(); expected.put(2, Arrays.asList("cc", "dd"));