| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.os.RemoteException; |
| 21 |
import android.util.ExceptionUtils; |
| 22 |
|
| 23 |
import java.util.function.BiConsumer; |
| 24 |
import java.util.function.BiFunction; |
| 25 |
import java.util.function.Consumer; |
| 26 |
import java.util.function.Function; |
| 27 |
import java.util.function.Supplier; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class FunctionalUtils { |
| 33 |
private FunctionalUtils() {} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static <T> Consumer<T> uncheckExceptions(ThrowingConsumer<T> action) { |
| 40 |
return action; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static <I, O> Function<I, O> uncheckExceptions(ThrowingFunction<I, O> action) { |
| 47 |
return action; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static Runnable uncheckExceptions(ThrowingRunnable action) { |
| 54 |
return action; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static <A, B> BiConsumer<A, B> uncheckExceptions(ThrowingBiConsumer<A, B> action) { |
| 61 |
return action; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static <T> Supplier<T> uncheckExceptions(ThrowingSupplier<T> action) { |
| 68 |
return action; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static <T> Consumer<T> ignoreRemoteException(RemoteExceptionIgnoringConsumer<T> action) { |
| 75 |
return action; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static Runnable handleExceptions(ThrowingRunnable r, Consumer<Throwable> handler) { |
| 83 |
return () -> { |
| 84 |
try { |
| 85 |
r.run(); |
| 86 |
} catch (Throwable t) { |
| 87 |
handler.accept(t); |
| 88 |
} |
| 89 |
}; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@FunctionalInterface |
| 99 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 100 |
public interface ThrowingRunnable extends Runnable { |
| 101 |
void runOrThrow() throws Exception; |
| 102 |
|
| 103 |
@Override |
| 104 |
default void run() { |
| 105 |
try { |
| 106 |
runOrThrow(); |
| 107 |
} catch (Exception ex) { |
| 108 |
throw ExceptionUtils.propagate(ex); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@FunctionalInterface |
| 120 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 121 |
public interface ThrowingSupplier<T> extends Supplier<T> { |
| 122 |
T getOrThrow() throws Exception; |
| 123 |
|
| 124 |
@Override |
| 125 |
default T get() { |
| 126 |
try { |
| 127 |
return getOrThrow(); |
| 128 |
} catch (Exception ex) { |
| 129 |
throw ExceptionUtils.propagate(ex); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@FunctionalInterface |
| 140 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 141 |
public interface ThrowingConsumer<T> extends Consumer<T> { |
| 142 |
void acceptOrThrow(T t) throws Exception; |
| 143 |
|
| 144 |
@Override |
| 145 |
default void accept(T t) { |
| 146 |
try { |
| 147 |
acceptOrThrow(t); |
| 148 |
} catch (Exception ex) { |
| 149 |
throw ExceptionUtils.propagate(ex); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@FunctionalInterface |
| 160 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 161 |
public interface RemoteExceptionIgnoringConsumer<T> extends Consumer<T> { |
| 162 |
void acceptOrThrow(T t) throws RemoteException; |
| 163 |
|
| 164 |
@Override |
| 165 |
default void accept(T t) { |
| 166 |
try { |
| 167 |
acceptOrThrow(t); |
| 168 |
} catch (RemoteException ex) { |
| 169 |
|
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@FunctionalInterface |
| 184 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 185 |
public interface ThrowingFunction<T, R> extends Function<T, R> { |
| 186 |
|
| 187 |
R applyOrThrow(T t) throws Exception; |
| 188 |
|
| 189 |
@Override |
| 190 |
default R apply(T t) { |
| 191 |
try { |
| 192 |
return applyOrThrow(t); |
| 193 |
} catch (Exception ex) { |
| 194 |
throw ExceptionUtils.propagate(ex); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@FunctionalInterface |
| 210 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 211 |
public interface ThrowingBiFunction<T, U, R> extends BiFunction<T, U, R> { |
| 212 |
|
| 213 |
R applyOrThrow(T t, U u) throws Exception; |
| 214 |
|
| 215 |
@Override |
| 216 |
default R apply(T t, U u) { |
| 217 |
try { |
| 218 |
return applyOrThrow(t, u); |
| 219 |
} catch (Exception ex) { |
| 220 |
throw ExceptionUtils.propagate(ex); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
@FunctionalInterface |
| 235 |
@SuppressWarnings("FunctionalInterfaceMethodChanged") |
| 236 |
public interface ThrowingBiConsumer<A, B> extends BiConsumer<A, B> { |
| 237 |
|
| 238 |
void acceptOrThrow(A a, B b) throws Exception; |
| 239 |
|
| 240 |
@Override |
| 241 |
default void accept(A a, B b) { |
| 242 |
try { |
| 243 |
acceptOrThrow(a, b); |
| 244 |
} catch (Exception ex) { |
| 245 |
throw ExceptionUtils.propagate(ex); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
@FunctionalInterface |
| 259 |
public interface ThrowingCheckedConsumer<Input, ExceptionType extends Exception> { |
| 260 |
void accept(Input input) throws ExceptionType; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
@FunctionalInterface |
| 273 |
public interface ThrowingChecked2Consumer<Input, ExceptionOne extends Exception, |
| 274 |
ExceptionTwo extends Exception> { |
| 275 |
void accept(Input input) throws ExceptionOne, ExceptionTwo; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
@FunctionalInterface |
| 288 |
public interface ThrowingCheckedFunction<Input, Output, ExceptionType extends Exception> { |
| 289 |
Output apply(Input input) throws ExceptionType; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
@NonNull |
| 297 |
public static String getLambdaName(@NonNull Object function) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
final String fullFunction = function.toString(); |
| 305 |
|
| 306 |
final int endPkgIdx = fullFunction.indexOf("-$$"); |
| 307 |
if (endPkgIdx == -1) return fullFunction; |
| 308 |
|
| 309 |
|
| 310 |
final int firstDollarIdx = fullFunction.indexOf('$', endPkgIdx + 3); |
| 311 |
if (firstDollarIdx == -1) return fullFunction; |
| 312 |
|
| 313 |
final int endClassIdx = fullFunction.indexOf('$', firstDollarIdx + 1); |
| 314 |
if (endClassIdx == -1) { |
| 315 |
|
| 316 |
return fullFunction.substring(0, endPkgIdx - 1) + "$Lambda"; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
return fullFunction.substring(0, endPkgIdx) |
| 321 |
+ fullFunction.substring(firstDollarIdx + 1, endClassIdx) |
| 322 |
+ "$Lambda"; |
| 323 |
} |
| 324 |
} |