/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @return The conversion context */ static <T> ArgumentConversionContext<T> of(Argument<T> argument) { return of(argument, null, null); }
/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @param locale The locale * @return The conversion context */ static <T> ArgumentConversionContext of(Argument<T> argument, @Nullable Locale locale) { return of(argument, locale, null); }
/** * Get the value of the {@code value} member of the annotation. * * @param argument The argument * @param <T> The type * @return The result */ public final <T> Optional<T> getValue(Argument<T> argument) { return getValue(ConversionContext.of(argument)); }
/** * Get the value of the {@code value} member of the annotation. * * @param member The member * @param type The type * @param <T> The type * @throws IllegalStateException If no member is available that conforms to the given name and type * @return The result */ public @Nonnull final <T> T getRequiredValue(String member, Class<T> type) { return get(member, ConversionContext.of(type)).orElseThrow(() -> new IllegalStateException("No value available for annotation member @" + annotationName + "[" + member + "] of type: " + type)); }
/** * Returns a submap for all the keys with the given prefix. * * @param prefix The prefix * @param valueType The value type * @return The submap */ @SuppressWarnings("unchecked") default Map<String, V> subMap(String prefix, Argument<V> valueType) { return subMap(prefix, ConversionContext.of(valueType)); }
/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @return The conversion context */ static <T> ArgumentConversionContext<T> of(Class<T> argument) { return of(Argument.of(argument), null, null); }
/** * Resolve the given property for the given name. * * @param name The name * @param requiredType The required type * @param <T> The concrete type * @return An optional containing the property value if it exists and is able to be converted */ default <T> Optional<T> get(K name, Argument<T> requiredType) { return get(name, ConversionContext.of(requiredType)); }
/** * Get the value of the {@code value} member of the annotation. * * @param type The type * @param <T> The type * @return The result */ public final <T> Optional<T> getValue(Class<T> type) { return getValue(ConversionContext.of(type)); }
/** * <p>Resolve the given property for the given name, type and generic type arguments.</p> * <p> * <p>Implementers can choose to implement more intelligent type conversion by analyzing the typeArgument.</p> * * @param name The name * @param argument The required type * @param <T> The concrete type * @return An optional containing the property value if it exists */ default <T> Optional<T> getProperty(String name, Argument<T> argument) { return getProperty(name, ConversionContext.of(argument)); }
/** * 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))); }
@Override public <T> Optional<T> getBody(Argument<T> type) { return getBody().flatMap(b -> ConversionService.SHARED.convert(b, ConversionContext.of(type))); }
private Object doConvertInput(ConversionService<?> conversionService, Argument<?> arg, Object object) { ArgumentConversionContext conversionContext = ConversionContext.of(arg); Optional<?> convert = conversionService.convert(object, conversionContext); if (convert.isPresent()) { return convert.get(); } else { Optional<ConversionError> lastError = conversionContext.getLastError(); if (lastError.isPresent()) { throw new ConversionErrorException(arg, lastError.get()); } } return null; } }
/** * Resolve the given property for the given name. * * @param name The name * @param requiredType The required type * @param <T> The concrete type * @return An optional containing the property value if it exists and is able to be converted */ default <T> Optional<T> get(K name, Class<T> requiredType) { return get(name, ConversionContext.of(Argument.of(requiredType))); }
/** * 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)))); }
/** * Find a header and convert it to the given type. * * @param name The name of the header * @param requiredType The required type * @param <T> The generic type * @return If the header is presented and can be converted an optional of the value otherwise {@link Optional#empty()} */ default <T> Optional<T> getFirst(CharSequence name, Argument<T> requiredType) { V v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, ConversionContext.of(requiredType)); } return Optional.empty(); }
@SuppressWarnings("unchecked") @Override public <T> Optional<T> getBody(Argument<T> type) { return convertedBodies.computeIfAbsent(type.getType(), aClass -> getBody().flatMap(b -> { ArgumentConversionContext<T> context = ConversionContext.of(type); if (b instanceof ByteBuffer) { return conversionService.convert(((ByteBuffer) b).asNativeBuffer(), context); } return conversionService.convert(b, context); })); }
@SuppressWarnings("unchecked") @Override public BindingResult<Observable> bind(ArgumentConversionContext<Observable> context, HttpRequest<?> source) { Collection<Argument<?>> typeVariables = context.getArgument().getTypeVariables().values(); BindingResult<Publisher> result = publisherBodyBinder.bind( ConversionContext.of(Argument.of(Publisher.class, (Argument[]) typeVariables.toArray(new Argument[0]))), source ); if (result.isPresentAndSatisfied()) { return () -> Optional.of(Observable.fromPublisher(result.get())); } return BindingResult.EMPTY; } }
@SuppressWarnings("unchecked") @Override public BindingResult<Single> bind(ArgumentConversionContext<Single> context, HttpRequest<?> source) { Collection<Argument<?>> typeVariables = context.getArgument().getTypeVariables().values(); BindingResult<Publisher> result = publisherBodyBinder.bind( ConversionContext.of(Argument.of(Publisher.class, (Argument[]) typeVariables.toArray(new Argument[0]))), source ); if (result.isPresentAndSatisfied()) { return () -> Optional.of(Single.fromPublisher(result.get())); } return BindingResult.EMPTY; } }
default <T> ArgumentConversionContext<T> with(Argument<T> argument) { ConversionContext childContext = ConversionContext.of(argument); ConversionContext thisContext = this; return new DefaultArgumentConversionContext(argument, thisContext.getLocale(), thisContext.getCharset()) {