class MessageUtil extends AbstractCaller {

    @Description("Finds a message within the list, matching the specified field.<br/>" +
            "<b>fieldName</b> - the field name for the matching group.<br/>" +
            "<b>fieldValue</b> - the field value for the matching group.<br/>" +
            "<b>return</b> - a message matched by the field name and value.<br/>" +
            "Considering the above said, the final syntax is:<br/>" +
            "#{FindGroup(groups, fieldName, fieldValue)}")
    @UtilityMethod
    public IMessage FindGroup(List<IMessage> groups, String fieldName, Object fieldValue) {
        if (groups != null && !groups.isEmpty()) {
            fieldName = fieldName != null ? fieldName.trim() : null;
            if (fieldName != null && !fieldName.isEmpty()) {
                for (IMessage msg : groups) {
                    if (msg.isFieldSet(fieldName) && msg.getField(fieldName).equals(fieldValue)
                            || !msg.isFieldSet(fieldName) && fieldValue == null) {
                        return msg;
                    }
                }
                throw new EPSCommonException("Group with " + fieldName + " == " + fieldValue + " not found");
            }
            throw new EPSCommonException("Field name is null or empty");
        }
        throw new EPSCommonException("List groups is null or empty");
    }

}