001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MemoryTaskoptions.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.taskoptionmanagers;
009:
010: import com.uwyn.rife.pcj.map.IntKeyOpenHashMap;
011: import com.uwyn.rife.scheduler.Scheduler;
012: import com.uwyn.rife.scheduler.Taskoption;
013: import com.uwyn.rife.scheduler.TaskoptionManager;
014: import com.uwyn.rife.scheduler.exceptions.TaskManagerException;
015: import com.uwyn.rife.scheduler.exceptions.TaskoptionManagerException;
016: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.AddTaskoptionErrorException;
017: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.DuplicateTaskoptionException;
018: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.InexistentTaskIdException;
019: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.UpdateTaskoptionErrorException;
020: import java.util.ArrayList;
021: import java.util.Collection;
022:
023: public class MemoryTaskoptions implements TaskoptionManager {
024: private Scheduler mScheduler = null;
025: private IntKeyOpenHashMap<ArrayList<Taskoption>> mTaskoptionsMapping = null;
026:
027: public MemoryTaskoptions() {
028: mTaskoptionsMapping = new IntKeyOpenHashMap<ArrayList<Taskoption>>();
029: }
030:
031: public void setScheduler(Scheduler scheduler) {
032: mScheduler = scheduler;
033: }
034:
035: public Scheduler getScheduler() {
036: return mScheduler;
037: }
038:
039: public boolean addTaskoption(Taskoption taskoption)
040: throws TaskoptionManagerException {
041: if (null == taskoption)
042: throw new IllegalArgumentException(
043: "taskoption can't be null.");
044: if (taskoption.getTaskId() < 0)
045: throw new IllegalArgumentException(
046: "the task id is required.");
047:
048: synchronized (this ) {
049: Taskoption cloned_taskoption = null;
050: try {
051: cloned_taskoption = taskoption.clone();
052: } catch (CloneNotSupportedException e) {
053: throw new AddTaskoptionErrorException(
054: cloned_taskoption, e);
055: }
056:
057: // check if the task id exists
058: try {
059: if (null == getScheduler().getTaskManager().getTask(
060: cloned_taskoption.getTaskId())) {
061: throw new InexistentTaskIdException(
062: cloned_taskoption.getTaskId());
063: }
064: } catch (TaskManagerException e) {
065: throw new AddTaskoptionErrorException(
066: cloned_taskoption, e);
067: }
068:
069: // get the taskoptions list for the same task id
070: int task_id = cloned_taskoption.getTaskId();
071: ArrayList<Taskoption> taskoptions = mTaskoptionsMapping
072: .get(task_id);
073: if (null == taskoptions) {
074: // no list exists, create one
075: taskoptions = new ArrayList<Taskoption>();
076: mTaskoptionsMapping.put(cloned_taskoption.getTaskId(),
077: taskoptions);
078: } else {
079: // list exists, check if the same taskoption isn't already present
080: // and throw an exception if that is the case
081: for (Taskoption taskoption_to_check : taskoptions) {
082: if (taskoption_to_check.getName().equals(
083: cloned_taskoption.getName())) {
084: throw new DuplicateTaskoptionException(
085: cloned_taskoption.getTaskId(),
086: cloned_taskoption.getName());
087: }
088: }
089: }
090:
091: taskoptions.add(cloned_taskoption);
092:
093: return true;
094: }
095: }
096:
097: public boolean updateTaskoption(Taskoption taskoption)
098: throws TaskoptionManagerException {
099: if (null == taskoption)
100: throw new IllegalArgumentException(
101: "taskoption can't be null.");
102: if (taskoption.getTaskId() < 0)
103: throw new IllegalArgumentException(
104: "the task id is required.");
105:
106: synchronized (this ) {
107: Taskoption cloned_taskoption = null;
108: try {
109: cloned_taskoption = taskoption.clone();
110: } catch (CloneNotSupportedException e) {
111: throw new AddTaskoptionErrorException(
112: cloned_taskoption, e);
113: }
114:
115: // check if the task id exists
116: try {
117: if (null == getScheduler().getTaskManager().getTask(
118: cloned_taskoption.getTaskId())) {
119: throw new InexistentTaskIdException(
120: cloned_taskoption.getTaskId());
121: }
122: } catch (TaskManagerException e) {
123: throw new UpdateTaskoptionErrorException(
124: cloned_taskoption, e);
125: }
126:
127: // get the taskoptions for the same task id
128: int task_id = cloned_taskoption.getTaskId();
129: ArrayList<Taskoption> taskoptions = mTaskoptionsMapping
130: .get(task_id);
131: if (null == taskoptions) {
132: return false;
133: }
134: // obtain the taskoption with same name
135: Taskoption taskoption_to_remove = null;
136: for (Taskoption taskoption_to_check : taskoptions) {
137: if (taskoption_to_check.getName().equals(
138: cloned_taskoption.getName())) {
139: taskoption_to_remove = taskoption_to_check;
140: break;
141: }
142: }
143:
144: // no match was found
145: if (null == taskoption_to_remove) {
146: return false;
147: }
148:
149: // remove the old taskoption and store the new one
150: taskoptions.remove(taskoption_to_remove);
151: taskoptions.add(cloned_taskoption);
152:
153: return true;
154: }
155: }
156:
157: public Taskoption getTaskoption(int taskid, String name)
158: throws TaskoptionManagerException {
159: if (taskid < 0)
160: throw new IllegalArgumentException(
161: "taskid can't be negative.");
162: if (null == name)
163: throw new IllegalArgumentException("name can't be null.");
164: if (0 == name.length())
165: throw new IllegalArgumentException("name can't be empty.");
166:
167: synchronized (this ) {
168: // get the taskoptions for the same task id
169: ArrayList<Taskoption> taskoptions = mTaskoptionsMapping
170: .get(taskid);
171: if (null == taskoptions) {
172: return null;
173: }
174: // obtain the taskoption with same name
175: for (Taskoption taskoption : taskoptions) {
176: if (taskoption.getName().equals(name)) {
177: return taskoption;
178: }
179: }
180: }
181:
182: return null;
183: }
184:
185: public Collection<Taskoption> getTaskoptions(int taskid)
186: throws TaskoptionManagerException {
187: if (taskid < 0)
188: throw new IllegalArgumentException(
189: "taskid can't be negative.");
190:
191: return mTaskoptionsMapping.get(taskid);
192: }
193:
194: public boolean removeTaskoption(Taskoption taskoption)
195: throws TaskoptionManagerException {
196: if (null == taskoption)
197: throw new IllegalArgumentException(
198: "taskoption can't be null.");
199:
200: return removeTaskoption(taskoption.getTaskId(), taskoption
201: .getName());
202: }
203:
204: public boolean removeTaskoption(int taskid, String name)
205: throws TaskoptionManagerException {
206: if (taskid < 0)
207: throw new IllegalArgumentException(
208: "taskid can't be negative.");
209: if (null == name)
210: throw new IllegalArgumentException("name can't be null.");
211: if (0 == name.length())
212: throw new IllegalArgumentException("name can't be empty.");
213:
214: synchronized (this ) {
215: // get the taskoptions for the same task id
216: ArrayList<Taskoption> taskoptions = mTaskoptionsMapping
217: .get(taskid);
218: if (null == taskoptions) {
219: return false;
220: }
221: // obtain the taskoption with same name
222: Taskoption taskoption_to_remove = null;
223: for (Taskoption taskoption : taskoptions) {
224: if (taskoption.getName().equals(name)) {
225: taskoption_to_remove = taskoption;
226: break;
227: }
228: }
229:
230: if (null == taskoption_to_remove) {
231: return false;
232: }
233:
234: taskoptions.remove(taskoption_to_remove);
235: }
236:
237: return true;
238: }
239: }
|