/** * Reset the underlying shared Connection, to be reinitialized on next access. */ public void resetConnection() { Optionals.toStream(Optional.ofNullable(connection), Optional.ofNullable(reactiveConnection)) .forEach(SharedConnection::resetConnection); synchronized (this.connectionMonitor) { this.connection = null; this.reactiveConnection = null; } }
/** * Reset the underlying shared Connection, to be reinitialized on next access. */ public void resetConnection() { Optionals.toStream(Optional.ofNullable(connection), Optional.ofNullable(reactiveConnection)) .forEach(SharedConnection::resetConnection); synchronized (this.connectionMonitor) { this.connection = null; this.reactiveConnection = null; } }
/** * Reset the underlying shared Connection, to be reinitialized on next access. */ public void resetConnection() { Optionals.toStream(Optional.ofNullable(connection), Optional.ofNullable(reactiveConnection)) .forEach(SharedConnection::resetConnection); synchronized (this.connectionMonitor) { this.connection = null; this.reactiveConnection = null; } }
@Nullable @Override public MethodExecutor resolve(EvaluationContext context, @Nullable Object target, final String name, List<TypeDescriptor> argumentTypes) { if (target instanceof EvaluationContextExtensionAdapter) { return getMethodExecutor((EvaluationContextExtensionAdapter) target, name, argumentTypes).orElse(null); } return adapters.stream()// .flatMap(it -> Optionals.toStream(getMethodExecutor(it, name, argumentTypes)))// .findFirst().orElse(null); }
private boolean hasReferencedUserType(CqlIdentifier identifier) { return getPersistentEntities().stream().flatMap(entity -> StreamSupport.stream(entity.spliterator(), false)) .flatMap(it -> Optionals.toStream(Optional.ofNullable(it.findAnnotation(CassandraType.class)))) .map(CassandraType::userTypeName) .filter(StringUtils::hasText) .map(CqlIdentifier::of) .anyMatch(identifier::equals); }
@Override public String toString() { if (annotationCache.isEmpty()) { populateAnnotationCache(getProperty()); } String builder = annotationCache.values().stream() // .flatMap(it -> Optionals.toStream(it)) // .map(Object::toString) // .collect(Collectors.joining(" ")); return builder + super.toString(); }
@Override public Set<GenericConverter> getConverters() { return Optionals// .toStream(getOptionalReadingConverter(), getOptionalWritingConverter())// .collect(Collectors.toSet()); }
/** * Looks up the most specific method for the given method and type and returns an accessible version of discovered * {@link Method} if found. * * @param method * @param type * @see ClassUtils#getMostSpecificMethod(Method, Class) * @return */ private static Optional<Method> getMostSpecificMethod(Method method, Class<?> type) { return Optionals.toStream(Optional.ofNullable(ClassUtils.getMostSpecificMethod(method, type)))// .map(it -> BridgeMethodResolver.findBridgedMethod(it))// .peek(it -> ReflectionUtils.makeAccessible(it))// .findFirst(); }
/** * Retrieve all classes which are involved in property/getter/setter declarations as these elements may be * distributed across the type hierarchy. */ @SuppressWarnings("null") private static List<Class<?>> getPropertyDeclaratingClasses(List<PersistentProperty<?>> persistentProperties) { return persistentProperties.stream().flatMap(property -> { return Optionals .toStream(Optional.ofNullable(property.getField()), Optional.ofNullable(property.getGetter()), Optional.ofNullable(property.getSetter())) // keep it a lambda to infer the correct types, preventing // LambdaConversionException: Invalid receiver type class java.lang.reflect.AccessibleObject; not a subtype // of implementation type interface java.lang.reflect.Member .map(it -> it.getDeclaringClass()); }).collect(Collectors.collectingAndThen(Collectors.toSet(), it -> new ArrayList<>(it))); }
@Override public AnnotatedType findAnnotatedType(Class<? extends Annotation> annotationType) { return Optionals.toStream(Optional.ofNullable(getField()).map(Field::getAnnotatedType), Optional.ofNullable(getGetter()).map(Method::getAnnotatedReturnType), Optional.ofNullable(getSetter()).map(it -> it.getParameters()[0].getAnnotatedType())) .filter(it -> hasAnnotation(it, annotationType, getTypeInformation())).findFirst().orElse(null); }
private Stream<? extends AnnotatedElement> getAccessors() { return Optionals.toStream(Optional.ofNullable(getGetter()), Optional.ofNullable(getSetter()), Optional.ofNullable(getField())); } }
/** * Finds the {@link PropertyDescriptor} for the property with the given name on the given type. * * @param type must not be {@literal null}. * @param fieldname must not be {@literal null} or empty. * @return */ private static Optional<PropertyDescriptor> findPropertyDescriptor(Class<?> type, String fieldname) { PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(type, fieldname); if (descriptor != null) { return Optional.of(descriptor); } List<Class<?>> superTypes = new ArrayList<>(); superTypes.addAll(Arrays.asList(type.getInterfaces())); superTypes.add(type.getSuperclass()); return Streamable.of(type.getInterfaces()).stream()// .flatMap(it -> Optionals.toStream(findPropertyDescriptor(it, fieldname)))// .findFirst(); }
/** * The most suitable save method is selected as follows: We prefer * <ol> * <li>a {@link RepositoryMetadata#getDomainType()} as first parameter over</li> * <li>an {@link Object} as first parameter.</li> * </ol> * * @param metadata must not be {@literal null}. * @return the most suitable method or {@literal null} if no method could be found. */ private static Optional<Method> selectMostSuitableSaveMethod(RepositoryMetadata metadata) { return asList(metadata.getDomainType(), Object.class).stream()// .flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), SAVE, it)))// .flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))// .findFirst(); }
/** * The most suitable {@code findById} method is selected as follows: We prefer * <ol> * <li>a {@link RepositoryMetadata#getIdType()} as first parameter over</li> * <li>a {@link Object} as first parameter</li> * </ol> * * @param metadata must not be {@literal null}. * @return the most suitable method or {@literal null} if no method could be found. */ private static Optional<Method> selectMostSuitableFindOneMethod(RepositoryMetadata metadata) { return asList(metadata.getIdType(), Object.class).stream()// .flatMap(it -> toStream(findMethod(metadata.getRepositoryInterface(), FIND_ONE, it)))// .flatMap(it -> toStream(getMostSpecificMethod(it, metadata.getRepositoryInterface())))// .findFirst(); }
/** * The most suitable findAll method is selected as follows: We prefer * <ol> * <li>a {@link Pageable} as first parameter over</li> * <li>a {@link Sort} as first parameter over</li> * <li>no parameters.</li> * </ol> * * @param metadata must not be {@literal null}. * @return the most suitable method or {@literal null} if no method could be found. */ private static Optional<Method> selectMostSuitableFindAllMethod(RepositoryMetadata metadata) { Class<?> repositoryInterface = metadata.getRepositoryInterface(); Supplier<Optional<Method>> withPageableOrSort = () -> Stream.of(Pageable.class, Sort.class)// .flatMap(it -> toStream(findMethod(repositoryInterface, FIND_ALL, it)))// .flatMap(it -> toStream(getMostSpecificMethod(it, repositoryInterface)))// .findFirst(); Supplier<Optional<Method>> withoutParameter = () -> findMethod(repositoryInterface, FIND_ALL)// .flatMap(it -> getMostSpecificMethod(it, repositoryInterface)); return firstNonEmpty(withPageableOrSort, withoutParameter); }
@SuppressWarnings("unchecked") private List<RepositoryFragment<?>> findRepositoryFragments(Class<T> repositoryType, CdiRepositoryConfiguration cdiRepositoryConfiguration) { Stream<RepositoryFragmentConfiguration> fragmentConfigurations = context .getRepositoryFragments(cdiRepositoryConfiguration, repositoryType); return fragmentConfigurations.flatMap(it -> { Class<Object> interfaceClass = (Class<Object>) lookupFragmentInterface(repositoryType, it.getInterfaceName()); Class<?> implementationClass = context.loadClass(it.getClassName()); Optional<Bean<?>> bean = getBean(implementationClass, beanManager, qualifiers); return Optionals.toStream(bean.map(this::getDependencyInstance) // .map(implementation -> RepositoryFragment.implemented(interfaceClass, implementation))); // }).collect(Collectors.toList()); }
/** * Populates the annotation cache by eagerly accessing the annotations directly annotated to the accessors (if * available) and the backing field. Annotations override annotations found on field. * * @param property * @throws MappingException in case we find an ambiguous mapping on the accessor methods */ private void populateAnnotationCache(Property property) { Optionals.toStream(property.getGetter(), property.getSetter()).forEach(it -> { for (Annotation annotation : it.getAnnotations()) { Class<? extends Annotation> annotationType = annotation.annotationType(); validateAnnotation(annotation, "Ambiguous mapping! Annotation %s configured " + "multiple times on accessor methods of property %s in class %s!", annotationType.getSimpleName(), getName(), getOwner().getType().getSimpleName()); annotationCache.put(annotationType, Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType))); } }); property.getField().ifPresent(it -> { for (Annotation annotation : it.getAnnotations()) { Class<? extends Annotation> annotationType = annotation.annotationType(); validateAnnotation(annotation, "Ambiguous mapping! Annotation %s configured " + "on field %s and one of its accessor methods in class %s!", annotationType.getSimpleName(), it.getName(), getOwner().getType().getSimpleName()); annotationCache.put(annotationType, Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType))); } }); }
/** * The most suitable delete method is selected as follows: We prefer * <ol> * <li>a {@link RepositoryMetadata#getDomainType()} as first parameter over</li> * <li>a {@link RepositoryMetadata#getIdType()} as first parameter over</li> * <li>a {@link Object} as first parameter over</li> * <li>an {@link Iterable} as first parameter.</li> * </ol> * * @param metadata must not be {@literal null}. * @return the most suitable method or {@literal null} if no method could be found. */ private static Optional<Method> selectMostSuitableDeleteMethod(RepositoryMetadata metadata) { Stream<Pair<String, Class<?>>> source = Stream.of(// Pair.of(DELETE, metadata.getDomainType()), // Pair.of(DELETE_BY_ID, metadata.getIdType()), // Pair.of(DELETE, Object.class), // Pair.of(DELETE_BY_ID, Object.class), // Pair.of(DELETE, Iterable.class)); Class<?> repositoryInterface = metadata.getRepositoryInterface(); return source// .flatMap(it -> toStream(findMethod(repositoryInterface, it.getFirst(), it.getSecond())))// .flatMap(it -> toStream(getMostSpecificMethod(it, repositoryInterface)))// .findFirst(); }