/** * Returns the first marshaler (in iteration order) that is capable of marshaling the given * object. * * @param object object that the returned marshaler must be capable of handling * @param marshalers collection of marshalers * @return the marshaler * @throws MarshalingException if no marshaler is capable */ public static Marshaler<?> findMarshaler( Object object, Collection<? extends Marshaler<?>> marshalers) throws MarshalingException { Objects.requireNonNull(object); Objects.requireNonNull(marshalers); return marshalers .stream() .filter(marshaler -> marshaler.canHandle(object)) .findFirst() .orElseThrow( () -> new MarshalingException( String.format( "None of %s is capable of marshaling instance: %s.", marshalers, object))); }