View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transaction.constraints;
8   
9   import org.mule.api.MuleEvent;
10  
11  /**
12   * <code>BatchConstraint</code> is a filter that counts on every execution and
13   * returns true when the batch size value equals the execution count.
14   */
15  // @ThreadSafe
16  public class BatchConstraint extends ConstraintFilter
17  {
18      // @GuardedBy(this)
19      private int batchSize = 1;
20      // @GuardedBy(this)
21      private int batchCount = 0;
22  
23      public boolean accept(MuleEvent event)
24      {
25          synchronized (this)
26          {
27              batchCount++;
28              return batchCount == batchSize;
29          }
30      }
31  
32      public int getBatchSize()
33      {
34          synchronized (this)
35          {
36              return batchSize;
37          }
38      }
39  
40      public synchronized void setBatchSize(int batchSize)
41      {
42          synchronized (this)
43          {
44              this.batchSize = batchSize;
45          }
46      }
47  
48      public Object clone() throws CloneNotSupportedException
49      {
50          synchronized (this)
51          {
52              BatchConstraint clone = (BatchConstraint) super.clone();
53              clone.setBatchSize(batchSize);
54              for (int i = 0; i < batchCount; i++)
55              {
56                  clone.accept(null);
57              }
58              return clone;
59          }
60      }
61  
62  }