Skip to content

Collection Entries

inno-juanal edited this page Feb 27, 2017 · 12 revisions

When an entry contains a collection as a value, you may use the collectionEntry method or the *sEntry convenience methods in Document.

The following example is the implementation of an Integration Server transformer. It takes an optional list of BigDecimal and returns the highest value:

public static final void getMaxNum(IData pipeline) throws ServiceException {
    Document pipeDoc = Documents.wrap(pipeline);
    CollectionEntry<BigDecimal> listEntry = pipeDoc.bigDecimalsEntry("list");

    if (listEntry.isAssigned()) {
        BigDecimal max = null;
			  
        for(BigDecimal number: listEntry.getValOrEmpty()) {
            if (max != null) {
                if (number != null && number.compareTo(max) > 0) {
                    max = number;
                }
            }
            else {
                max = number; 
            }
        }
	  
        pipeDoc.bigDecimalEntry("max").put(max); 
    }

}

Through the isAssigned check we support this behaviour: if the list is not provided, nothing is returned. This is typically a good approach for the implementation of transformer services.

Notice that we can simply call listEntry.getValOrEmpty and directly iterate over the list. We get an empty list if the entry doesn't exist or the entry value is null. An alternative is to use NullValHandling.FAIL to throw an exception when the entry is null. There is also the option to use NullValHandling.RETURN_NULL to return null - as opposed to an empty list.

In the example null values in the list are allowed. A decision was made to return null if all values were null - we could've not assigned max otherwise.

We could've simplified the code if had used Java 8 features but since the library is compatible with older Java versions, we show examples that also work in those versions.