/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param targetType The target type * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, Class<T> targetType) { return convert(object, targetType, ConversionContext.DEFAULT); }
@Override public <T> Optional<T> getProperty(String name, ArgumentConversionContext<T> conversionContext) { Object value = map.get(name); return conversionService.convert(value, conversionContext); } }
/** * @param interval The deregisterCriticalServiceAfter as a string * @return The {@link NewCheck} instance */ public NewCheck deregisterCriticalServiceAfter(String interval) { this.deregisterCriticalServiceAfter = ConversionService.SHARED.convert(interval, Duration.class).orElseThrow(() -> new IllegalArgumentException("Invalid deregisterCriticalServiceAfter Specified")); return this; }
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { V value = map.get(name); if (value != null) { return conversionService.convert(value, conversionContext); } return Optional.empty(); }
/** * Sets the interval. * * @param interval The interval */ protected void setInterval(String interval) { this.interval = ConversionService.SHARED.convert(interval, Duration.class).orElseThrow(() -> new IllegalArgumentException("Invalid Duration Specified")); }
/** * @param deregisterCriticalServiceAfter Service to de-regsiter after */ protected void setDeregisterCriticalServiceAfter(String deregisterCriticalServiceAfter) { this.deregisterCriticalServiceAfter = ConversionService.SHARED.convert(deregisterCriticalServiceAfter, Duration.class).orElseThrow(() -> new IllegalArgumentException("Invalid deregisterCriticalServiceAfter Specified")); }
/** * @param interval The interval as a string * @return The {@link HTTPCheck} instance */ public HTTPCheck interval(String interval) { this.interval = ConversionService.SHARED.convert(interval, Duration.class).orElseThrow(() -> new IllegalArgumentException("Invalid Duration Specified")); return this; }
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { String value = nettyHeaders.get(name); if (value != null) { return conversionService.convert(value, conversionContext); } return Optional.empty(); }
/** * @param ttl The TTL */ @JsonProperty("TTL") void setTtl(String ttl) { this.ttl = ConversionService.SHARED.convert(ttl, Duration.class).orElseThrow(() -> new IllegalArgumentException("Invalid TTL Returned")); }
@Override @SuppressWarnings("unchecked") public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { String value = headers.get(name.toString()); if (value != null) { return conversionService.convert(value, conversionContext); } return Optional.empty(); }
@SuppressWarnings("unchecked") @Override public <T1> Optional<T1> getBody(Class<T1> type) { Optional<T> body = getBody(); return body.flatMap(t -> convertedBodies.computeIfAbsent(type, aClass -> conversionService.convert(t, aClass))); }
@Override public <T> Optional<T> get(CharSequence member, ArgumentConversionContext<T> conversionContext) { Optional<T> result = convertibleValues.get(member, conversionContext); if (!result.isPresent()) { Object dv = defaultValues.get(member.toString()); if (dv != null) { return ConversionService.SHARED.convert(dv, conversionContext); } } return result; }
@Override public <T> Optional<T> get(K name, ArgumentConversionContext<T> conversionContext) { Object v = map.get(name); if (v == null) { return Optional.empty(); } Argument<T> argument = conversionContext.getArgument(); if (argument.getType().isInstance(v)) { return Optional.of((T) v); } return ConversionService.SHARED.convert(v, conversionContext); }
/** * Return the body as the given type. * * @param type The type of the body * @param <T> The generic type * @return An {@link Optional} of the type or {@link Optional#empty()} if the body cannot be returned as the given type */ default @Nonnull <T> Optional<T> getBody(@Nonnull Argument<T> type) { ArgumentUtils.requireNonNull("type", type); return getBody().flatMap(b -> ConversionService.SHARED.convert(b, ConversionContext.of(type))); }
/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param context The {@link ArgumentConversionContext} * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, ArgumentConversionContext<T> context) { return convert(object, context.getArgument().getType(), context); } }
/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param targetType The target type * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, Argument<T> targetType) { return convert(object, targetType.getType(), ConversionContext.of(targetType)); }
@SuppressWarnings("unchecked") @Override public <T1> Optional<T1> getBody(Argument<T1> type) { Optional<T> body = getBody(); return body.flatMap(t -> convertedBodies.computeIfAbsent(type.getType(), aClass -> conversionService.convert(t, ConversionContext.of(type)))); }
@Override public <T> Optional<T> get(CharSequence name, Class<T> requiredType) { if (requiredType == Cookie.class || requiredType == Object.class) { //noinspection unchecked return (Optional<T>) findCookie(name); } else { return findCookie(name).flatMap((cookie -> conversionService.convert(cookie.getValue(), requiredType))); } }
private Object resolveValue(ArgumentConversionContext<T> context, ConvertibleValues<?> values, String annotationValue) { Argument<T> argument = context.getArgument(); if (StringUtils.isEmpty(annotationValue)) { annotationValue = argument.getName(); } return values.get(annotationValue, context).orElseGet(() -> conversionService.convert(argument.getAnnotationMetadata().getValue(Bindable.class, DEFAULT_VALUE_MEMBER, String.class).orElse(null), context).orElse(null) ); }