Home | History | Annotate | Download | only in tasks
      1 /*
      2  * Copyright (C) 2011 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 vogar.tasks;
     18 
     19 import java.util.ArrayList;
     20 import java.util.Collection;
     21 import java.util.List;
     22 import vogar.Console;
     23 import vogar.Result;
     24 
     25 /**
     26  * A task necessary to accomplish the user's requested actions. Tasks have
     27  * prerequisites; a task must not be run until it reports that it is runnable.
     28  * Tasks may be run at most once; running a task produces a result.
     29  */
     30 public abstract class Task {
     31     private final String name;
     32     final List<Task> tasksThatMustFinishFirst = new ArrayList<Task>();
     33     final List<Task> tasksThatMustFinishSuccessfullyFirst = new ArrayList<Task>();
     34     volatile Result result;
     35     Exception thrown;
     36 
     37     protected Task(String name) {
     38         this.name = name;
     39     }
     40 
     41     /**
     42      * Returns true if this task is an action task. The queue imposes limits
     43      * on how many actions may be run concurrently.
     44      */
     45     public boolean isAction() {
     46         return false;
     47     }
     48 
     49     public Task after(Task prerequisite) {
     50         tasksThatMustFinishFirst.add(prerequisite);
     51         return this;
     52     }
     53 
     54     public Task after(Collection<Task> prerequisites) {
     55         tasksThatMustFinishFirst.addAll(prerequisites);
     56         return this;
     57     }
     58 
     59     public Task afterSuccess(Task prerequisite) {
     60         tasksThatMustFinishSuccessfullyFirst.add(prerequisite);
     61         return this;
     62     }
     63 
     64     public Task afterSuccess(Collection<Task> prerequisitess) {
     65         tasksThatMustFinishSuccessfullyFirst.addAll(prerequisitess);
     66         return this;
     67     }
     68 
     69     public final boolean isRunnable() {
     70         for (Task task : tasksThatMustFinishFirst) {
     71             if (task.result == null) {
     72                 return false;
     73             }
     74         }
     75         for (Task task : tasksThatMustFinishSuccessfullyFirst) {
     76             if (task.result != Result.SUCCESS) {
     77                 return false;
     78             }
     79         }
     80         return true;
     81     }
     82 
     83     protected abstract Result execute() throws Exception;
     84 
     85     final void run(Console console) {
     86         if (result != null) {
     87             throw new IllegalStateException();
     88         }
     89         try {
     90             console.verbose("running " + this);
     91             result = execute();
     92         } catch (Exception e) {
     93             thrown = e;
     94             result = Result.ERROR;
     95         }
     96 
     97         if (result != Result.SUCCESS) {
     98             console.verbose("warning " + this + " " + result);
     99         }
    100     }
    101 
    102     @Override public final String toString() {
    103         return name;
    104     }
    105 }
    106