All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.gridgears.held.internal.parser.ParseUtils Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package at.gridgears.held.internal.parser;

import javax.xml.bind.JAXBElement;
import javax.xml.datatype.XMLGregorianCalendar;
import java.time.Instant;
import java.util.List;
import java.util.Optional;

final class ParseUtils {
    private ParseUtils() {
        //must not be instantiated
    }

    @SuppressWarnings("unchecked")
    static  Optional getValue(Object element, Class expectedClass) {
        T result = null;
        if (element instanceof JAXBElement) {
            Object value = ((JAXBElement) element).getValue();
            if (expectedClass.isAssignableFrom(value.getClass())) {
                result = (T) value;
            }
        }

        return Optional.ofNullable(result);
    }

    static  Optional getValue(List elements, Class expectedClass) {
        T result = null;
        for (Object element : elements) {
            Optional optional = getValue(element, expectedClass);
            if (optional.isPresent()) {
                result = optional.get();
                break;
            }
        }

        return Optional.ofNullable(result);
    }

    static  T first(List list) {
        return list.isEmpty() ? null : list.get(0);
    }

    static Instant toInstant(XMLGregorianCalendar xmlGregorianCalendar) {
        return xmlGregorianCalendar.toGregorianCalendar().getTime().toInstant();
    }

}