|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package rx.util.async.operators; |
| 18 | + |
| 19 | +import rx.util.functions.Action0; |
| 20 | +import rx.util.functions.Action1; |
| 21 | + |
| 22 | +/** |
| 23 | + * Utility methods convert between functional interfaces of actions and functions. |
| 24 | + */ |
| 25 | +public final class Functionals { |
| 26 | + private Functionals() { |
| 27 | + throw new IllegalStateException("No instances!"); |
| 28 | + } |
| 29 | + /** |
| 30 | + * Return an action which takes a Throwable and does nothing. |
| 31 | + * <p>(To avoid casting from the generic empty1().) |
| 32 | + * @return the action |
| 33 | + */ |
| 34 | + public static Action1<Throwable> emptyThrowable() { |
| 35 | + return EMPTY_THROWABLE; |
| 36 | + } |
| 37 | + /** |
| 38 | + * An action that takes a Throwable and does nothing. |
| 39 | + */ |
| 40 | + private static final Action1<Throwable> EMPTY_THROWABLE = new EmptyThrowable(); |
| 41 | + /** An empty throwable class. */ |
| 42 | + private static final class EmptyThrowable implements Action1<Throwable> { |
| 43 | + @Override |
| 44 | + public void call(Throwable t1) { |
| 45 | + } |
| 46 | + } |
| 47 | + /** |
| 48 | + * Return an Action0 instance which does nothing. |
| 49 | + * @return an Action0 instance which does nothing |
| 50 | + */ |
| 51 | + public static Action0 empty() { |
| 52 | + return EMPTY; |
| 53 | + } |
| 54 | + /** A single empty instance. */ |
| 55 | + private static final Action0 EMPTY = new EmptyAction(); |
| 56 | + /** An empty action class. */ |
| 57 | + private static final class EmptyAction implements Action0 { |
| 58 | + @Override |
| 59 | + public void call() { |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Converts a runnable instance into an Action0 instance. |
| 65 | + * @param run the Runnable to run when the Action0 is called |
| 66 | + * @return the Action0 wrapping the Runnable |
| 67 | + */ |
| 68 | + public static Action0 fromRunnable(Runnable run) { |
| 69 | + if (run == null) { |
| 70 | + throw new NullPointerException("run"); |
| 71 | + } |
| 72 | + return new ActionWrappingRunnable(run); |
| 73 | + } |
| 74 | + /** An Action0 which wraps and calls a Runnable. */ |
| 75 | + private static final class ActionWrappingRunnable implements Action0 { |
| 76 | + final Runnable run; |
| 77 | + |
| 78 | + public ActionWrappingRunnable(Runnable run) { |
| 79 | + this.run = run; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void call() { |
| 84 | + run.run(); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + /** |
| 89 | + * Converts an Action0 instance into a Runnable instance. |
| 90 | + * @param action the Action0 to call when the Runnable is run |
| 91 | + * @return the Runnable wrapping the Action0 |
| 92 | + */ |
| 93 | + public static Runnable toRunnable(Action0 action) { |
| 94 | + if (action == null) { |
| 95 | + throw new NullPointerException("action"); |
| 96 | + } |
| 97 | + return new RunnableAction(action); |
| 98 | + } |
| 99 | + /** An Action0 which wraps and calls a Runnable. */ |
| 100 | + private static final class RunnableAction implements Runnable { |
| 101 | + final Action0 action; |
| 102 | + |
| 103 | + public RunnableAction(Action0 action) { |
| 104 | + this.action = action; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void run() { |
| 109 | + action.call(); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | +} |
0 commit comments