01: /*
02: * $Id: BatchConstraint.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transaction.constraints;
12:
13: import org.mule.api.MuleEvent;
14:
15: /**
16: * <code>BatchConstraint</code> is a filter that counts on every execution and
17: * returns true when the batch size value equals the execution count.
18: */
19: // @ThreadSafe
20: public class BatchConstraint extends ConstraintFilter {
21: // @GuardedBy(this)
22: private int batchSize = 1;
23: // @GuardedBy(this)
24: private int batchCount = 0;
25:
26: public boolean accept(MuleEvent event) {
27: synchronized (this ) {
28: batchCount++;
29: return batchCount == batchSize;
30: }
31: }
32:
33: public int getBatchSize() {
34: synchronized (this ) {
35: return batchSize;
36: }
37: }
38:
39: public synchronized void setBatchSize(int batchSize) {
40: synchronized (this ) {
41: this .batchSize = batchSize;
42: }
43: }
44:
45: public Object clone() throws CloneNotSupportedException {
46: synchronized (this ) {
47: BatchConstraint clone = (BatchConstraint) super .clone();
48: clone.setBatchSize(batchSize);
49: for (int i = 0; i < batchCount; i++) {
50: clone.accept(null);
51: }
52: return clone;
53: }
54: }
55:
56: }
|