Skip to content

Commit 09c289e

Browse files
JakeWhartonakarnokd
authored andcommitted
Make identity function a singleton. (#4676)
1 parent e5b449d commit 09c289e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/main/java/rx/internal/util/UtilityFunctions.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,9 @@ public static <T> Func1<? super T, Boolean> alwaysFalse() {
5050
* @param <T> the input and output value type
5151
* @return a {@link Func1} that accepts an Object and returns the same Object
5252
*/
53+
@SuppressWarnings("unchecked")
5354
public static <T> Func1<T, T> identity() {
54-
return new Func1<T, T>() {
55-
@Override
56-
public T call(T o) {
57-
return o;
58-
}
59-
};
55+
return (Func1<T, T>) Identity.INSTANCE;
6056
}
6157

6258
enum AlwaysTrue implements Func1<Object, Boolean> {
@@ -76,4 +72,13 @@ public Boolean call(Object o) {
7672
return false;
7773
}
7874
}
75+
76+
enum Identity implements Func1<Object, Object> {
77+
INSTANCE;
78+
79+
@Override
80+
public Object call(Object o) {
81+
return o;
82+
}
83+
}
7984
}

0 commit comments

Comments
 (0)