1 |
e02ff7c5
|
Andreas Müller
|
/**
|
2 |
782d9cc1
|
Andreas Müller
|
* Copyright (C) 2009 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
6 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
e02ff7c5
|
Andreas Müller
|
package eu.etaxonomy.cdm.persistence.validation;
|
10 |
|
|
|
11 |
|
|
import java.util.Collection;
|
12 |
|
|
import java.util.Iterator;
|
13 |
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
14 |
|
|
import java.util.concurrent.TimeUnit;
|
15 |
|
|
|
16 |
|
|
/**
|
17 |
782d9cc1
|
Andreas Müller
|
* A job queue catering to the needs of the entity validation process. If an
|
18 |
|
|
* entity validation task is submitted to the queue, and the queue already
|
19 |
|
|
* contains tasks validating the exact same entity, those tasks should be
|
20 |
|
|
* removed first, because they are validating a state of the entity that is no
|
21 |
|
|
* longer actual. Note that it is not fatal to validate the entity in those
|
22 |
|
|
* intermediary states, because in the end the final state of the entity does
|
23 |
|
|
* get validated as well. It's just useless and may lead to unnecessary
|
24 |
|
|
* contention of the queue.
|
25 |
|
|
*
|
26 |
|
|
* @author ayco_holleman
|
27 |
|
|
*
|
28 |
e02ff7c5
|
Andreas Müller
|
*/
|
29 |
|
|
@SuppressWarnings("serial")
|
30 |
|
|
class EntityValidationTaskQueue extends ArrayBlockingQueue<Runnable> {
|
31 |
|
|
|
32 |
782d9cc1
|
Andreas Müller
|
public EntityValidationTaskQueue(int capacity) {
|
33 |
|
|
super(capacity);
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
@Override
|
37 |
|
|
public boolean add(Runnable r) {
|
38 |
|
|
cleanup(r);
|
39 |
|
|
return super.add(r);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
@Override
|
43 |
|
|
public boolean offer(Runnable r, long timeout, TimeUnit unit) throws InterruptedException {
|
44 |
|
|
cleanup(r);
|
45 |
|
|
return super.offer(r, timeout, unit);
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
@Override
|
49 |
|
|
public boolean offer(Runnable r) {
|
50 |
|
|
cleanup(r);
|
51 |
|
|
return super.offer(r);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
@Override
|
55 |
|
|
public void put(Runnable r) throws InterruptedException {
|
56 |
|
|
cleanup(r);
|
57 |
|
|
super.put(r);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
@Override
|
61 |
|
|
public boolean addAll(Collection<? extends Runnable> c) {
|
62 |
|
|
throw new RuntimeException("Submitting multiple validation tasks at once not supported");
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
private void cleanup(Runnable runnable) {
|
66 |
|
|
EntityValidationTaskBase newTask = (EntityValidationTaskBase) runnable;
|
67 |
|
|
Iterator<Runnable> iterator = this.iterator();
|
68 |
|
|
while (iterator.hasNext()) {
|
69 |
|
|
EntityValidationTaskBase oldTask = (EntityValidationTaskBase) iterator.next();
|
70 |
|
|
if (oldTask.equals(newTask)) {
|
71 |
|
|
iterator.remove();
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
e02ff7c5
|
Andreas Müller
|
|
76 |
|
|
}
|