Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      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 com.android.tools.build.apkzlib.utils;
     18 
     19 import java.io.IOException;
     20 import java.io.UncheckedIOException;
     21 import java.util.function.Function;
     22 import javax.annotation.Nonnull;
     23 import javax.annotation.Nullable;
     24 
     25 /**
     26  * Function that can throw an I/O Exception
     27  */
     28 @FunctionalInterface
     29 public interface IOExceptionFunction<F, T> {
     30 
     31     /**
     32      * Applies the function to the given input.
     33      * @param input the input
     34      * @return the function result
     35      */
     36     @Nullable T apply(@Nullable F input) throws IOException;
     37 
     38     /**
     39      * Wraps a function that may throw an IO Exception throwing an {@code UncheckedIOException}.
     40      *
     41      * @param f the function
     42      */
     43     @Nonnull
     44     static <F, T> Function<F, T> asFunction(@Nonnull IOExceptionFunction<F, T> f)  {
     45         return i -> {
     46             try {
     47                 return f.apply(i);
     48             } catch (IOException e) {
     49                 throw new UncheckedIOException(e);
     50             }
     51         };
     52     }
     53 }
     54