Skip navigation links


com.fatwire.services.beans.response
Class MessageCollector<K,V>

java.lang.Object
  extended by com.fatwire.services.beans.response.MessageCollector<K,V>

Type Parameters:
K - the type of the key in a message collector which will use an underlying mapping.
V - the type of the elements in the collection underlying the message collector, or the collection which is a value in the mapping underlying the message collector. It is necessary for the purposes of homogeneity that the collection and the values in the mapping have the same generic type. Where there may be mixed types, Object can be used.
K - type of keys supported by this message collector.
V - type of values supported by this message collector.

public class MessageCollector<K,V>
extends java.lang.Object

Implements a collector. The collector can be implemented to use a collection, a mapping, or both as required by the use case.

An example for using a collection is a simple error message collector where the underlying message collector only contains a collection of strings.

An example for using a mapping is a message collector for a service method where the underlying message collector provides feedback for each input entity. For example, if a list of asset identifiers is used as the input, the asset id can be the key, and the value can be a collection of Reason objects providing the feedback.
Note: The mapping in a message collector does not allow null keys.

An example of using both a collection and a mapping is a complex feedback collector where the collection is fed with checked exception messages and the mapping contains feedback for each input. For example, if such a message collector is used for a service method which provides a list of asset identifiers as input, the mapping can hold the reason why a specific asset could not be processed as expected, whereas the collection can hold a message due to any other exception (an illegal argument, for example).

See Also:
MessageCollectors, Reason

Constructor Summary
MessageCollector()
          Default constructor.

 

Method Summary
 void add(K key, java.util.Collection<V> value)
          Adds all elements of a value collection against the specified key in the underlying map in the collector.
 void add(K key, V value)
          Adds a value against the specified key in the underlying map in the collector.
 boolean add(V e)
           
 boolean addAll(java.util.Collection<? extends V> c)
           
 void addAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
          Adds mapping from an existing message mapping into this message collector's map.
 void clear()
          Clears all messages collected into this message collector.
 boolean contains(K o)
           
 boolean containsKey(K key)
           
 java.util.Collection<V> get(K key)
          Returns the messages collected against the specified key if the underlying collector is a map.
 V getFirst()
          Retrieves the first value from the generic messages collection.
 V getFirst(K key)
          Retrieves the first value from the message collector.
 V getFirstMessage()
          Returns the first message from the collection or the first message from the map iff there is only one entry in the map.
 java.util.Map<K,java.util.Collection<V>> getMessageMap()
          Returns a read-only view of the messages collected as a mapping.
 java.util.Collection<V> getMessages()
          Returns a read-only view of the messages collected as a collection.
 boolean isEmpty()
          Checks if the message collector contains any messages.
 java.util.Set<K> keySet()
           
protected  java.util.Collection<V> newMessages()
          Returns the underlying Collection instance to be wrapped by this message collector.
protected  java.util.Map<K,java.util.Collection<V>> newMessagesMap()
          Returns the underlying Map instance to be wrapped by this message collector.
 boolean remove(int index)
           
 java.util.Collection<V> remove(K key)
           
 boolean removeAll(java.util.Collection<?> c)
           
 boolean removeAll(K key, java.util.Collection<?> c)
          Removes all of this collector's elements that are also contained in the specified collection.
 boolean retainAll(java.util.Collection<?> c)
           
 boolean retainAll(K key, java.util.Collection<?> c)
          Retains only the elements in this collector that are contained in the specified collection.

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Constructor Detail

MessageCollector

public MessageCollector()
Default constructor. Instantiates the collection and map to be used by this collector.

Method Detail

newMessages

protected java.util.Collection<V> newMessages()
Returns the underlying Collection instance to be wrapped by this message collector. The same instantiation is used for the collection objects to be used as values in the mapping.
Returns:
a Collection object.

newMessagesMap

protected java.util.Map<K,java.util.Collection<V>> newMessagesMap()
Returns the underlying Map instance to be wrapped by this message collector.
Returns:
a Map object.

isEmpty

public final boolean isEmpty()
Checks if the message collector contains any messages. If the underlying collector is a collection, this method will return true if the collection is null or empty as per Collection.isEmpty(). If the underlying collector is a map, this method will return true if the map is null or empty as per Map.isEmpty().
Returns:
true if the underlying collector is null or empty, false otherwise.

get

public final java.util.Collection<V> get(K key)
Returns the messages collected against the specified key if the underlying collector is a map. If the underlying collector is a collection, this will return null. If key is null, this returns the underlying collector as if it was a collection.
Parameters:
key -
Returns:

add

public final void add(K key,
                      java.util.Collection<V> value)
Adds all elements of a value collection against the specified key in the underlying map in the collector. The addition to the map is governed by the map instantiated for the collector. If the key is null, adds the elements of the value collection to the underlying collection, instead of adding it to the map. The addition to the collection is governed by the type of collection instantiated for the collector.
Parameters:
key - the key to set.
value - the value to set.

add

public final void add(K key,
                      V value)
Adds a value against the specified key in the underlying map in the collector. The addition to the map is governed by the map instantiated for the collector. If the key is null, adds the value to the underlying collection, instead of adding it to the map. The addition to the collection is governed by the type of collection instantiated for the collector.
Parameters:
key - the key to set.
value - the value to set.

getMessageMap

public final java.util.Map<K,java.util.Collection<V>> getMessageMap()
Returns a read-only view of the messages collected as a mapping. The map is ensured to be null-key safe, i.e., it will not support null as a key. If no messages have been collected into the map, this returns an empty map.
Returns:
the underlying map in the message collector.

getMessages

public final java.util.Collection<V> getMessages()
Returns a read-only view of the messages collected as a collection. If no messages have been collected into the collection, this returns an empty collection.
Returns:
the underlying collector in the message collector.

addAll

public final void addAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
Adds mapping from an existing message mapping into this message collector's map. The addition of values happens as per the specifications of the #add(Object, List) method.
Parameters:
m - the input map.

clear

public final void clear()
Clears all messages collected into this message collector. Clears out both the underlying map and collection.

removeAll

public final boolean removeAll(K key,
                               java.util.Collection<?> c)
Removes all of this collector's elements that are also contained in the specified collection. The match is made against the specified key in the underlying mapping or in the underlying collection for this collector. The removal logic is governed by the implementation of the underlying collection for this collector. If the key is null, the removal happens only from the underlying collection and not from the map.
Parameters:
key - the key used to retrieve the collection value from the underlying map.
c - collection containing elements to be removed from this collector.
Returns:

retainAll

public final boolean retainAll(K key,
                               java.util.Collection<?> c)
Retains only the elements in this collector that are contained in the specified collection.
Parameters:
key - the key used to retrieve the collection value from the underlying map.
c - the collection which is to be added as a value in the underlying map.
Returns:
true if this collector changed as a result of the call.

getFirst

public final V getFirst()
Retrieves the first value from the generic messages collection.
Returns:
the first value.

getFirst

public final V getFirst(K key)
Retrieves the first value from the message collector. If key is null, retrieves the message from the underlying collection. This is equivalent to calling MessageCollector.getFirst(). Otherwise, reads the first value from the underlying map.
Parameters:
key - the key against which the values are read.
Returns:
the first value.

getFirstMessage

public V getFirstMessage()
Returns the first message from the collection or the first message from the map iff there is only one entry in the map.
Returns:
the first message.

containsKey

public final boolean containsKey(K key)

remove

public final java.util.Collection<V> remove(K key)

keySet

public final java.util.Set<K> keySet()

contains

public final boolean contains(K o)

add

public final boolean add(V e)

addAll

public final boolean addAll(java.util.Collection<? extends V> c)

removeAll

public final boolean removeAll(java.util.Collection<?> c)

retainAll

public final boolean retainAll(java.util.Collection<?> c)

remove

public final boolean remove(int index)

Skip navigation links


Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.