rename abstract class
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / validation / EntityValidationTaskQueue.java
1 /**
2 * 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 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 * A job queue catering to the needs of the entity validation process. If an entity validation
18 * task is submitted to the queue, and the queue already contains tasks validating the exact
19 * same entity, those tasks should be removed first, because they are validating a state of the
20 * entity that is no longer actual. Note that it is not fatal to validate the entity in those
21 * intermediary states, because in the end the final state of the entity does get validated as
22 * well. It's just useless and may lead to unnecessary contention of the queue.
23 *
24 * @author ayco holleman
25 *
26 */
27 @SuppressWarnings("serial")
28 class EntityValidationTaskQueue extends ArrayBlockingQueue<Runnable> {
29
30 public EntityValidationTaskQueue(int capacity)
31 {
32 super(capacity);
33 }
34
35
36 @Override
37 public boolean add(Runnable r)
38 {
39 cleanup(r);
40 return super.add(r);
41 }
42
43
44 @Override
45 public boolean offer(Runnable r, long timeout, TimeUnit unit) throws InterruptedException
46 {
47 cleanup(r);
48 return super.offer(r, timeout, unit);
49 }
50
51
52 @Override
53 public boolean offer(Runnable r)
54 {
55 cleanup(r);
56 return super.offer(r);
57 }
58
59
60 @Override
61 public void put(Runnable r) throws InterruptedException
62 {
63 cleanup(r);
64 super.put(r);
65 }
66
67
68 @Override
69 public boolean addAll(Collection<? extends Runnable> c)
70 {
71 throw new RuntimeException("Submitting multiple validation tasks at once not supported");
72 }
73
74
75 private void cleanup(Runnable runnable)
76 {
77 EntityValidationTaskBase newTask = (EntityValidationTaskBase) runnable;
78 Iterator<Runnable> iterator = this.iterator();
79 while (iterator.hasNext()) {
80 EntityValidationTaskBase oldTask = (EntityValidationTaskBase) iterator.next();
81 if (oldTask.getEntity().equals(newTask.getEntity())) {
82 iterator.remove();
83 }
84 }
85 }
86
87 }