import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Iterator;
public class Scheduler { private List todoList = new LinkedList(); public long getTotalDuration() throws TaskException { long total = 0L; for (Iterator i=todoList.iterator(); i.hasNext(); ) { total += ((Task)(i.next())).getDuration(); } return total; }
public void main(String[] args) { Scheduler s = new Scheduler(); s.todoList.add(new DownloadTask("http://google.com")); try { System.out.println(s.getTotalDuration()); } catch (TaskException doh) { System.out.println("Unable to perform task"); doh.printStackTrace(); } } }
class TaskException extends Exception { public TaskException(String msg, Throwable cause) { super(msg, cause); } }
interface Task { long getDuration() throws TaskException; }
class DownloadTask implements Task {
private String url;
public DownloadTask(String u) { url = u; } public long getDuration() throws TaskException { try { // this may throw an IOException } catch (IOException doh) { throw new TaskException("Couldn't download", doh); } } } |