--- /dev/null
+package ro.ieval.fonbot;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+/*
+ * Copyright © 2013 Marius Gavrilescu
+ *
+ * This file is part of FonBot.
+ *
+ * FonBot is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * FonBot is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Slim alternative to AsyncTask.<p>
+ *
+ * Differences from AsyncTask:
+ * <ul>
+ * <li>No onPreExecute or onPostExecute
+ * <li>No progress support
+ * <li>Can be retried if necessary
+ * </ul>
+ *
+ * @author Marius Gavrilescu <marius@ieval.ro>
+ */
+abstract class ExecutableRunnable implements Runnable {
+ /** Executor used to execute instances of this class */
+ private static final Executor executor = Executors.newSingleThreadExecutor();
+ /** Queue containing <code>ExecutableRunnable</code>s that should be retried */
+ private static final Queue<ExecutableRunnable> retryPendingTasks = new LinkedList<ExecutableRunnable>();
+
+ /** Run all tasks that should be retried */
+ public static final void retryTasks(){
+ synchronized(retryPendingTasks){
+ for(ExecutableRunnable task : retryPendingTasks)
+ task.execute();
+ retryPendingTasks.clear();
+ }
+ }
+
+ /** Execute this <code>ExecutableRunnable</code> */
+ public final void execute(){
+ retryTasks();
+ executor.execute(this);
+ }
+
+ /** Mark this <code>ExecutableRunnable</code> as needing to be retried later */
+ protected final void retry(){
+ synchronized(retryPendingTasks){
+ retryPendingTasks.add(this);
+ }
+ }
+}