2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.persistence
.validation
;
11 import java
.util
.Collection
;
12 import java
.util
.Iterator
;
13 import java
.util
.concurrent
.ArrayBlockingQueue
;
14 import java
.util
.concurrent
.TimeUnit
;
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.
24 * @author ayco holleman
27 @SuppressWarnings("serial")
28 class EntityValidationTaskQueue
extends ArrayBlockingQueue
<Runnable
> {
30 public EntityValidationTaskQueue(int capacity
)
37 public boolean add(Runnable r
)
45 public boolean offer(Runnable r
, long timeout
, TimeUnit unit
) throws InterruptedException
48 return super.offer(r
, timeout
, unit
);
53 public boolean offer(Runnable r
)
56 return super.offer(r
);
61 public void put(Runnable r
) throws InterruptedException
69 public boolean addAll(Collection
<?
extends Runnable
> c
)
71 throw new RuntimeException("Submitting multiple validation tasks at once not supported");
75 private void cleanup(Runnable runnable
)
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())) {