Home | History | Annotate | Download | only in work
      1 /*
      2  * Copyright 2018 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 androidx.work;
     18 
     19 import android.support.annotation.NonNull;
     20 import android.support.annotation.RestrictTo;
     21 import android.util.Log;
     22 
     23 import java.util.List;
     24 
     25 /**
     26  * An abstract class that allows the user to define how to merge a list of inputs to a Worker.
     27  */
     28 
     29 public abstract class InputMerger {
     30 
     31     private static final String TAG = "InputMerger";
     32 
     33     /**
     34      * Merges a list of {@link Data} and outputs a single Data object.
     35      *
     36      * @param inputs A list of {@link Data} from previous Workers or the WorkRequest.Builder
     37      * @return The merged output
     38      */
     39     public abstract @NonNull Data merge(@NonNull List<Data> inputs);
     40 
     41     /**
     42      * Instantiates an {@link InputMerger} from its class name.
     43      *
     44      * @param className The name of the {@link InputMerger} class
     45      * @return The instantiated {@link InputMerger}, or {@code null} if it could not be instantiated
     46      *
     47      * @hide
     48      */
     49     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     50     @SuppressWarnings("ClassNewInstance")
     51     public static InputMerger fromClassName(String className) {
     52         try {
     53             Class<?> clazz = Class.forName(className);
     54             return (InputMerger) clazz.newInstance();
     55         } catch (Exception e) {
     56             Log.e(TAG, "Trouble instantiating + " + className, e);
     57         }
     58         return null;
     59     }
     60 }
     61