private <T extends ModelProperty> Element getElement(EnrichableModel enrichableModel, Class<T> modelPropertyClass, Function<T, Optional<? extends Element>> elementFunction) { return enrichableModel.getModelProperty(modelPropertyClass) .flatMap(elementFunction::apply) .orElse(null); } }
private static <T, P extends ModelProperty> T fromModelProperty(EnrichableModel model, Class<P> modelPropertyType, Function<P, T> map, Supplier<? extends RuntimeException> exceptionSupplier) { return model.getModelProperty(modelPropertyType).map(map).orElseThrow(exceptionSupplier); }
private Optional<List<String>> parameterNamesRequiredForMetadataCacheId(Object model) { if (model instanceof EnrichableModel) { return ((EnrichableModel) model).getModelProperty(RequiredForMetadataModelProperty.class) .map(RequiredForMetadataModelProperty::getRequiredParameters); } return empty(); }
public static String getMemberName(EnrichableModel enrichableModel, String defaultName) { return enrichableModel.getModelProperty(DeclaringMemberModelProperty.class).map(p -> p.getDeclaringField().getName()) .orElse(defaultName); }
/** * Sets the {@code encoding} value into the field of the {@code target} annotated {@link DefaultEncoding} if the {@code model} * contains the {@link DeclaringMemberModelProperty} property and the value is not {@code null}. * * @param model enriched with {@link DefaultEncodingModelProperty} * @param target object in which the fields are going to be set * @param encoding to be injected into the {@link String} field annotated with {@link DefaultEncoding} */ public static void injectDefaultEncoding(EnrichableModel model, Object target, String encoding) { injectFieldFromModelProperty(target, encoding, model.getModelProperty(DefaultEncodingModelProperty.class), DefaultEncoding.class); }
/** * Tests the given {@code model} for a {@link MetadataResolverFactoryModelProperty} and if present it returns the contained * {@link MetadataResolverFactory}. If no such property is found, then a {@link NullMetadataResolverFactory} is returned * * @param model an enriched model * @return a {@link MetadataResolverFactory} */ public static MetadataResolverFactory getMetadataResolverFactory(EnrichableModel model) { return model.getModelProperty(MetadataResolverFactoryModelProperty.class) .map(MetadataResolverFactoryModelProperty::getMetadataResolverFactory) .orElse(new NullMetadataResolverFactory()); }
public static Optional<Field> getMemberField(EnrichableModel enrichableModel) { return enrichableModel.getModelProperty(DeclaringMemberModelProperty.class).map(p -> p.getDeclaringField()); }
private <T> Optional<Type> getConnectionType(EnrichableModel model) { Optional<ConnectivityModelProperty> connectivityProperty = model.getModelProperty(ConnectivityModelProperty.class); if (!connectivityProperty.isPresent() && model instanceof ParameterizedModel) { connectivityProperty = ((ParameterizedModel) model).getAllParameterModels().stream() .map(p -> p.getModelProperty(ConnectivityModelProperty.class).orElse(null)) .filter(Objects::nonNull) .findFirst(); } return connectivityProperty.map(ConnectivityModelProperty::getConnectionType); }
/** * Creates a new {@link List} of {@link Interceptor interceptors} using the factories returned by * {@link InterceptorsModelProperty} (if present). * * @param model the model on which {@link InterceptorsModelProperty} is to be invoked * @return an immutable {@link List} with instances of {@link Interceptor} */ public static List<Interceptor> createInterceptors(EnrichableModel model) { return model.getModelProperty(InterceptorsModelProperty.class) .map(p -> createInterceptors(p.getInterceptorFactories())) .orElse(ImmutableList.of()); }
public AbstractMetadataResolutionTypeInformation(DslElementModel<?> component, Function<TypeResolversInformationModelProperty, Optional<ResolverInformation>> getResolverInformationFromModelProperty) { if (component.getModel() instanceof EnrichableModel) { Optional<TypeResolversInformationModelProperty> typeResolversInformationModelProperty = ((EnrichableModel) component.getModel()).getModelProperty(TypeResolversInformationModelProperty.class); if (typeResolversInformationModelProperty.isPresent()) { resolverName = getResolverInformationFromModelProperty.apply(typeResolversInformationModelProperty.get()) .map(resolverInformation -> resolverInformation.getResolverName()).orElse(null); resolverCategory = typeResolversInformationModelProperty.get().getCategoryName(); } } }
private Optional<ExceptionHandler> findExceptionHandler(EnrichableModel model) { return model.getModelProperty(ExceptionHandlerModelProperty.class) .map(p -> p.getExceptionHandlerFactory().createHandler()); }
private void mockModelProperties(EnrichableModel model) { when(model.getModelProperty(ConfigTypeModelProperty.class)).thenReturn(empty()); when(model.getModelProperty(ConnectivityModelProperty.class)).thenReturn(empty()); when(model.getModelProperty(PagedOperationModelProperty.class)).thenReturn(empty()); }
/** * Sets the {@code configName} into the field of the {@code target} annotated {@link RefName} (if it's present) and does the * same for the {@code encoding} and the field annotated with {@link DefaultEncoding} if the {@code model} contains the * {@link DeclaringMemberModelProperty} * * @param model enriched with {@link InjectedFieldModelProperty} * @param target object in which the fields are going to be set * @param configName to be injected into the {@link String} field annotated with {@link RefName} * @param encoding to be injected into the {@link String} field annotated with {@link DefaultEncoding} */ public static void injectFields(EnrichableModel model, Object target, String configName, String encoding) { injectFieldFromModelProperty(target, configName, model.getModelProperty(RequireNameField.class), RefName.class); injectDefaultEncoding(model, target, encoding); }
public static void setRequires(EnrichableModel model, boolean requiresConfig, boolean requiresConnection) { if (requiresConfig) { when(model.getModelProperty(ConfigTypeModelProperty.class)).thenReturn(of(mock(ConfigTypeModelProperty.class))); } if (requiresConnection) { when(model.getModelProperty(ConnectivityModelProperty.class)).thenReturn(of(mock(ConnectivityModelProperty.class))); } }
public static void mockMetadataResolverFactory(EnrichableModel model, MetadataResolverFactory factory) { Optional<MetadataResolverFactoryModelProperty> property = factory != null ? of(new MetadataResolverFactoryModelProperty(() -> factory)) : empty(); when(model.getModelProperty(MetadataResolverFactoryModelProperty.class)).thenReturn(property); }
public static void mockExceptionEnricher(EnrichableModel enrichableModel, ExceptionHandlerFactory exceptionHandlerFactory) { Optional<ExceptionHandlerModelProperty> property = exceptionHandlerFactory != null ? of(new ExceptionHandlerModelProperty(exceptionHandlerFactory)) : empty(); when(enrichableModel.getModelProperty(ExceptionHandlerModelProperty.class)).thenReturn(property); }
public static void mockInterceptors(EnrichableModel enrichableModel, List<InterceptorFactory> interceptorFactories) { if (interceptorFactories == null) { interceptorFactories = ImmutableList.of(); } when(enrichableModel.getModelProperty(InterceptorsModelProperty.class)) .thenReturn(of(new InterceptorsModelProperty(interceptorFactories))); }
public static void mockImplementingType(EnrichableModel model, Class<?> type) { when(model.getModelProperty(ImplementingTypeModelProperty.class)) .thenReturn(java.util.Optional.of(new ImplementingTypeModelProperty(type))); } }
/** * Sets the {@code encoding} value into the field of the {@code target} annotated {@link DefaultEncoding} if the {@code model} * contains the {@link DeclaringMemberModelProperty} property and the value is not {@code null}. * * @param model enriched with {@link DefaultEncodingModelProperty} * @param target object in which the fields are going to be set * @param encoding to be injected into the {@link String} field annotated with {@link DefaultEncoding} */ public static void injectDefaultEncoding(EnrichableModel model, Object target, String encoding) { injectFieldFromModelProperty(target, encoding, model.getModelProperty(DefaultEncodingModelProperty.class), DefaultEncoding.class); }
private <T> Optional<Type> getConnectionType(EnrichableModel model) { Optional<ConnectivityModelProperty> connectivityProperty = model.getModelProperty(ConnectivityModelProperty.class); if (!connectivityProperty.isPresent() && model instanceof ParameterizedModel) { connectivityProperty = ((ParameterizedModel) model).getAllParameterModels().stream() .map(p -> p.getModelProperty(ConnectivityModelProperty.class).orElse(null)) .filter(Objects::nonNull) .findFirst(); } return connectivityProperty.map(ConnectivityModelProperty::getConnectionType); }