Project

General

Profile

Download (1.99 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.persistence.validation;
2

    
3
import java.util.Collection;
4
import java.util.Iterator;
5
import java.util.concurrent.ArrayBlockingQueue;
6
import java.util.concurrent.TimeUnit;
7

    
8
/**
9
 * A job queue catering to the needs of the entity validation process. If an entity validation
10
 * task is submitted to the queue, and the queue already contains tasks validating the exact
11
 * same entity, those tasks should be removed first, because they are validating a state of the
12
 * entity that is no longer actual. Note that it is not fatal to validate the entity in those
13
 * intermediary states, because in the end the final state of the entity does get validated as
14
 * well. It's just useless and may lead to unnecessary contention of the queue.
15
 * 
16
 * @author ayco holleman
17
 * 
18
 */
19
@SuppressWarnings("serial")
20
class EntityValidationTaskQueue extends ArrayBlockingQueue<Runnable> {
21

    
22
	public EntityValidationTaskQueue(int capacity)
23
	{
24
		super(capacity);
25
	}
26

    
27

    
28
	@Override
29
	public boolean add(Runnable r)
30
	{
31
		checkQueue(r);
32
		return super.add(r);
33
	}
34

    
35

    
36
	@Override
37
	public boolean offer(Runnable r, long timeout, TimeUnit unit) throws InterruptedException
38
	{
39
		checkQueue(r);
40
		return super.offer(r, timeout, unit);
41
	}
42

    
43

    
44
	@Override
45
	public boolean offer(Runnable r)
46
	{
47
		checkQueue(r);
48
		return super.offer(r);
49
	}
50

    
51

    
52
	@Override
53
	public void put(Runnable r) throws InterruptedException
54
	{
55
		checkQueue(r);
56
		super.put(r);
57
	}
58

    
59

    
60
	@Override
61
	public boolean addAll(Collection<? extends Runnable> c)
62
	{
63
		throw new RuntimeException("Submitting multiple validation tasks at once not supported");
64
	}
65

    
66

    
67
	private void checkQueue(Runnable runnable)
68
	{
69
		EntityValidationTask newTask = (EntityValidationTask) runnable;
70
		Iterator<Runnable> iterator = this.iterator();
71
		while (iterator.hasNext()) {
72
			EntityValidationTask oldTask = (EntityValidationTask) iterator.next();
73
			if (oldTask.getEntity().equals(newTask.getEntity())) {
74
				iterator.remove();
75
			}
76
		}
77
	}
78

    
79
}
(2-2/7)