@SuppressWarnings("unchecked") // Manually verified with isAssignableFrom()
    private <T extends KeeperRecordField> T getField(List<KeeperRecordField> fields,
            Class<T> fieldClass, Pattern labelPattern) {

        // There are no fields if no List was provided at all
        if (fields == null)
            return null;

        T foundField = null;
        for (KeeperRecordField field : fields) {

            // Ignore fields of wrong class
            if (!fieldClass.isAssignableFrom(field.getClass()))
                continue;

            // Match against provided pattern, if any
            if (labelPattern != null) {

                // Ignore fields without labels if a label match is requested
                String label = field.getLabel();
                if (label == null)
                    continue;

                // Ignore fields whose labels do not match
                Matcher labelMatcher = labelPattern.matcher(label);
                if (!labelMatcher.matches())
                    continue;

            }

            // Ignore ambiguous fields
            if (foundField != null)
                return null;

            // Tentative match found - we can use this as long as no other
            // field matches the criteria
            foundField = (T) field;

        }

        return foundField;

    }
Returns the instance of the only field that has the given type and
matches the given label pattern. If there are no such fields, or
multiple such fields, null is returned.

@param <T>
    The type of field to return.

@param fields
    The list of fields to retrieve the field from. For convenience, this
    may be null. A null list will be considered equivalent to an empty
    list.

@param fieldClass
    The class representing the type of field to return.

@param labelPattern
    The pattern to match against the desired field's label, or null if
    no label pattern match should be performed.

@return
    The field having the given type and matching the given label
    pattern, or null if there is not exactly one such field.
extensions/guacamole-vault/modules/guacamole-vault-ksm/src/main/java/org/apache/guacamole/vault/ksm/secret/KsmRecordService.java