com.trendmicro.grid.acl.l0
Class BatchCollection<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by com.trendmicro.grid.acl.l0.BatchCollection<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>

public class BatchCollection<E>
extends AbstractCollection<E>
implements Serializable

Defines a collection that fails if more than Limits.MAX_INCOMING_REQUEST_BATCH_SIZE elements are added.

Version:
1.0
Author:
Juergen_Kellerer, 2011-02-22
See Also:
Serialized Form

Nested Class Summary
static interface BatchCollection.Invoker<E,R>
          Defines an invoker that may be used to handle chunks of batch collections.
 
Constructor Summary
BatchCollection()
          Creates a new modifiable instance.
BatchCollection(int batchSize)
          Creates a new modifiable instance with the given batch size limit.
 
Method Summary
 boolean add(E e)
          
 boolean addAll(Collection<? extends E> c)
          
static
<E> List<BatchCollection<E>>
chunksOf(List<? extends E> elements)
          Creates chunks of batch collections that honor the built-in batch limit using the given elements.
static
<E> List<BatchCollection<E>>
chunksOf(List<? extends E> elements, int batchSize)
          Creates chunks of batch collections that honor the given batch limit using the given elements.
 boolean contains(Object o)
          
 boolean containsAll(Collection<?> c)
          
 boolean equals(Object o)
          
static int getDefaultInvocationBatchSize()
          Returns the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).
 int hashCode()
          
static
<E,R,T extends Collection<R>>
T
invokeOnChunksOf(Collection<? extends E> elements, BatchCollection.Invoker<E,T> invoker)
          Is a helper method that can be used to invoke a certain call on all batch chunks of a larger input list.
 boolean isEmpty()
          
 Iterator<E> iterator()
          
static
<E> BatchCollection<E>
of(Collection<? extends E> elements)
          Creates a new final batch collection using the given elements.
static
<E> BatchCollection<E>
of(E... elements)
          Creates a new final batch collection using the given elements.
static
<E> BatchCollection<E>
of(E element)
          Creates a new final singleton batch collection using the given element.
static void setDefaultInvocationBatchSize(int defaultInvocationBatchSize)
          Sets the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).
 int size()
          
 Object[] toArray()
          
<T> T[]
toArray(T[] a)
          
 
Methods inherited from class java.util.AbstractCollection
clear, remove, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BatchCollection

public BatchCollection()
Creates a new modifiable instance.


BatchCollection

public BatchCollection(int batchSize)
Creates a new modifiable instance with the given batch size limit.

Parameters:
batchSize - The non-default batch size limit to use.
Method Detail

of

public static <E> BatchCollection<E> of(E element)
Creates a new final singleton batch collection using the given element.

Parameters:
element - the element to add.
Returns:
a new singleton batch collection using the given element.

of

public static <E> BatchCollection<E> of(E... elements)
Creates a new final batch collection using the given elements.

Parameters:
elements - the elements to add.
Returns:
a new batch collection using the given elements.
Throws:
BatchSizeExceededException - If more elements than allowed are specified.

of

public static <E> BatchCollection<E> of(Collection<? extends E> elements)
Creates a new final batch collection using the given elements.

Parameters:
elements - the elements to add.
Returns:
a new batch collection using the given elements.
Throws:
BatchSizeExceededException - If more elements than allowed are specified.

chunksOf

public static <E> List<BatchCollection<E>> chunksOf(List<? extends E> elements)
Creates chunks of batch collections that honor the built-in batch limit using the given elements.

Parameters:
elements - the elements to add (may be larger than the actual batch size).
Returns:
chunks of batch collections using the given elements.

chunksOf

public static <E> List<BatchCollection<E>> chunksOf(List<? extends E> elements,
                                                    int batchSize)
Creates chunks of batch collections that honor the given batch limit using the given elements.

Parameters:
elements - the elements to add (may be larger than the actual batch size).
batchSize - Sets a custom limit for the batch size of a chunk.
Returns:
chunks of batch collections using the given elements.

invokeOnChunksOf

public static <E,R,T extends Collection<R>> T invokeOnChunksOf(Collection<? extends E> elements,
                                                               BatchCollection.Invoker<E,T> invoker)
                                                throws WebException
Is a helper method that can be used to invoke a certain call on all batch chunks of a larger input list.

Using this method is safe with large lists even if the batch size is unknown as remote exceptions are automatically caught and a request is retried with adjusted batch sizes.

Example:

 Collection<FileIdentifier> identifiers = ...
 Collection<FileDetails> details = BatchCollection.invokeOnChunksOf(identifiers,
   new BatchCollection.Invoker<FileIdentifier, Collection<FileDetails>>() {
     public Collection<FileDetails> invoke(BatchCollection<FileIdentifier> chunk) {
       return fileService.getFileDetailsList(BatchCollection.of(chunk));
          }
   });

Type Parameters:
E - The type of the input collection elements.
R - The return type of collection or list elements.
T - The type of collection or list implementation that is returned by the invoked method.
Parameters:
elements - The elements to convert to batch chunks.
invoker - The invoker implemented as anonymous inner class that walks all batch chunks to produce a final result.
Returns:
The combined results of all single invocations.
Throws:
WebException - May be thrown inside the invoker.

getDefaultInvocationBatchSize

public static int getDefaultInvocationBatchSize()
Returns the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).

Returns:
the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).

setDefaultInvocationBatchSize

public static void setDefaultInvocationBatchSize(int defaultInvocationBatchSize)
Sets the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).

Parameters:
defaultInvocationBatchSize - the batch size that is used to build chunks within the method invokeOnChunksOf(java.util.Collection, com.trendmicro.grid.acl.l0.BatchCollection.Invoker).

add

public boolean add(E e)

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractCollection<E>

addAll

public boolean addAll(Collection<? extends E> c)

Specified by:
addAll in interface Collection<E>
Overrides:
addAll in class AbstractCollection<E>

iterator

public Iterator<E> iterator()

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>

size

public int size()

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface Collection<E>
Overrides:
isEmpty in class AbstractCollection<E>

contains

public boolean contains(Object o)

Specified by:
contains in interface Collection<E>
Overrides:
contains in class AbstractCollection<E>

toArray

public Object[] toArray()

Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class AbstractCollection<E>

toArray

public <T> T[] toArray(T[] a)

Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class AbstractCollection<E>

containsAll

public boolean containsAll(Collection<?> c)

Specified by:
containsAll in interface Collection<E>
Overrides:
containsAll in class AbstractCollection<E>

equals

public boolean equals(Object o)

Specified by:
equals in interface Collection<E>
Overrides:
equals in class Object

hashCode

public int hashCode()

Specified by:
hashCode in interface Collection<E>
Overrides:
hashCode in class Object