1 /* 2 * Copyright (C) 2009 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.cooliris.media; 18 19 import java.util.concurrent.CancellationException; 20 import java.util.concurrent.ExecutionException; 21 22 /** 23 * An abstract class for the interface <code>Cancelable</code>. Subclass can 24 * simply override the <code>execute()</code> function to provide an 25 * implementation of <code>Cancelable</code>. 26 */ 27 public abstract class BaseCancelable<T> implements Cancelable<T> { 28 29 /** 30 * The state of the task, possible transitions are: 31 * 32 * <pre> 33 * INITIAL -> CANCELED 34 * EXECUTING -> COMPLETE, CANCELING, ERROR, CANCELED 35 * CANCELING -> CANCELED 36 * </pre> 37 * 38 * When the task stop, it must be end with one of the following states: 39 * COMPLETE, CANCELED, or ERROR; 40 */ 41 private static final int STATE_INITIAL = (1 << 0); 42 private static final int STATE_EXECUTING = (1 << 1); 43 private static final int STATE_CANCELING = (1 << 2); 44 private static final int STATE_CANCELED = (1 << 3); 45 private static final int STATE_ERROR = (1 << 4); 46 private static final int STATE_COMPLETE = (1 << 5); 47 48 private int mState = STATE_INITIAL; 49 50 private Throwable mError; 51 private T mResult; 52 private Cancelable<?> mCurrentTask; 53 54 protected abstract T execute() throws Exception; 55 56 /** 57 * Frees the result (which is not null) when the task has been canceled. 58 */ 59 protected void freeCanceledResult(T result) { 60 // Do nothing by default; 61 } 62 63 private boolean isInStates(int states) { 64 return (states & mState) != 0; 65 } 66 67 private T handleTerminalStates() throws ExecutionException { 68 if (mState == STATE_CANCELED) { 69 throw new CancellationException(); 70 } 71 if (mState == STATE_ERROR) { 72 throw new ExecutionException(mError); 73 } 74 if (mState == STATE_COMPLETE) 75 return mResult; 76 throw new IllegalStateException(); 77 } 78 79 public synchronized void await() throws InterruptedException { 80 while (!isInStates(STATE_COMPLETE | STATE_CANCELED | STATE_ERROR)) { 81 wait(); 82 } 83 } 84 85 public final T get() throws InterruptedException, ExecutionException { 86 synchronized (this) { 87 if (mState != STATE_INITIAL) { 88 await(); 89 return handleTerminalStates(); 90 } 91 mState = STATE_EXECUTING; 92 } 93 try { 94 mResult = execute(); 95 } catch (CancellationException e) { 96 mState = STATE_CANCELED; 97 } catch (InterruptedException e) { 98 mState = STATE_CANCELED; 99 } catch (Throwable error) { 100 synchronized (this) { 101 if (mState != STATE_CANCELING) { 102 mError = error; 103 mState = STATE_ERROR; 104 } 105 } 106 } 107 synchronized (this) { 108 if (mState == STATE_CANCELING) 109 mState = STATE_CANCELED; 110 if (mState == STATE_EXECUTING) 111 mState = STATE_COMPLETE; 112 notifyAll(); 113 if (mState == STATE_CANCELED && mResult != null) { 114 freeCanceledResult(mResult); 115 } 116 return handleTerminalStates(); 117 } 118 } 119 120 /** 121 * Requests the task to be canceled. 122 * 123 * @return true if the task is running and has not been canceled; false 124 * otherwise 125 */ 126 127 public synchronized boolean requestCancel() { 128 if (mState == STATE_INITIAL) { 129 mState = STATE_CANCELED; 130 notifyAll(); 131 return false; 132 } 133 if (mState == STATE_EXECUTING) { 134 if (mCurrentTask != null) 135 mCurrentTask.requestCancel(); 136 mState = STATE_CANCELING; 137 return true; 138 } 139 return false; 140 } 141 } 142