Mir goes GPL
[mir.git] / source / mircoders / global / JobQueue.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.global;
33
34 import java.util.*;
35
36 // important: objects passed as data must not be altered once put into a job
37
38 public class JobQueue {
39   private Vector jobs;
40   private Map dataToJob;
41
42   public static final int STATUS_PENDING = 0;
43   public static final int STATUS_PROCESSING = 1;
44   public static final int STATUS_PROCESSED = 2;
45
46   public JobQueue() {
47     jobs = new Vector();
48     dataToJob = new HashMap();
49   }
50
51   public void appendJob(Object aData) {
52     synchronized (jobs) {
53       Job job = new Job(aData);
54       jobs.add(job);
55       dataToJob.put(aData, job);
56     }
57   }
58
59   public Object acquirePendingJob() {
60     synchronized (jobs) {
61       Iterator i = jobs.iterator();
62
63       while (i.hasNext()) {
64         Job job = (Job) i.next();
65
66         if (job.setProcessing()) {
67           return job.getData();
68         }
69       }
70     }
71
72     return null;
73   }
74
75   public void flagOffJob(Object aData) {
76     synchronized (jobs) {
77       Job job = (Job) dataToJob.get(aData);
78
79       if (job!=null) {
80         job.setProcessed();
81       }
82     }
83   }
84
85   public void cleanupJobs() {
86     synchronized (jobs) {
87       Iterator i = jobs.iterator();
88
89       while (i.hasNext()) {
90         Job job = (Job) i.next();
91
92         if (job.hasBeenProcessed()) {
93           i.remove();
94         }
95       }
96     }
97   }
98
99   public List makeJobListSnapshot() {
100     synchronized (jobs) {
101       return (List) jobs.clone();
102     }
103   }
104
105   public class Job implements Cloneable {
106     private Object data;
107     private int status;
108
109     public Job(Object aData, int aStatus) {
110       data = aData;
111       status = aStatus;
112     }
113
114     public Job(Object aData) {
115       this(aData, STATUS_PENDING);
116     }
117
118     public Object getData() {
119       return data;
120     }
121
122     public int getStatus() {
123       synchronized(this) {
124         return status;
125       }
126     }
127
128     protected boolean setProcessing() {
129       return setStatus(STATUS_PENDING, STATUS_PROCESSING);
130     }
131
132     protected void setProcessed() {
133       setStatus(STATUS_PROCESSING, STATUS_PROCESSED);
134     }
135
136     public boolean hasBeenProcessed() {
137       return getStatus() == STATUS_PROCESSED;
138     }
139
140     public boolean isProcessing() {
141       return getStatus() == STATUS_PROCESSING;
142     }
143
144     public boolean isPending() {
145       return getStatus() == STATUS_PENDING;
146     }
147
148     private boolean setStatus(int anOldStatus, int aNewStatus) {
149       synchronized(this) {
150         if (status == anOldStatus) {
151           status = aNewStatus;
152           return true;
153         }
154         else {
155           return false;
156         }
157       }
158     }
159
160     protected Object clone() {
161       synchronized(this) {
162         return new Job(data, status);
163       }
164     }
165   }
166 }
167