User:TLULbot/jBot/Core.java

/* * jBot - Java bot framework, for editing a wiki powered by MediaWiki. * Copyright (c) 2009-2010 The last username left and others. * * This program 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. * * This program 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 this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. * * This framework was based on AmauriceBot, created by Maurice Abraham. * * Contributors: * The last username left - Task framework, GUI * Maurice Abraham - Core utilities *--*/

package jBot;

import java.io.IOException; import java.util.*;

/** * Static core of jBot. Allows the bot to be defined statically. * Used for the default GUI for jBot. * @author The last username left */ public abstract class Core {   public abstract static class Task //Task setup {       public abstract void setWikiSession(WikiSession wiki); public abstract Runnable getRunnable; public abstract String getTaskCommand; public abstract String getTaskName; protected abstract int getPrinterVersion; protected abstract int getMainVersion; public boolean isCompatible {           return Core.isCompatible(getMainVersion); }   }    private static boolean isCompatible(int version) {       if (version &lt;= Core.version) return true; return false; }   public static interface Printer //Allows GUI to create components which communicate with Main {       public void errString(String str); public void outString(String str); public void setProg(int prog); public void setProgString(String str); }   public static class jBotException extends Exception {       private static final long serialVersionUID = -4495635167940297769L; //Just to identify the exceptions as coming from inside TLULbot, and therefore as internal error messages public jBotException(String string) { super(string); }   }    public static final int version = 2; //Version of jBot - tasks requiring a greater version number will not be added.

private static List&lt;Task&gt; tasks = new LinkedList&lt;Task&gt;; public static List&lt;Task&gt; getTasks {       return tasks; }   public static void addTask(Task t)    { if (t.isCompatible) tasks.add(t); else System.err.println("Task \"" + t.getTaskName + "\" requires version " + t.getMainVersion + " or higher of TLULbot."); }   public static Runnable getTaskRunnable(String taskCommand) throws IOException, jBotException {       for(Task t : tasks) {           if (taskCommand == t.getTaskCommand) return t.getRunnable; }       throw new jBotException("Unknown task command: " + taskCommand); } }