Refine search
/** * 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; } }
/** * Returns the {@link Resource}'s content type. * * @return never {@literal null}. * @throws com.mongodb.MongoGridFSException in case no content type declared on {@link GridFSFile#getMetadata()} nor * provided via {@link GridFSFile#getContentType()}. * @throws IllegalStateException if the file does not {@link #exists()}. */ @SuppressWarnings("deprecation") public String getContentType() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return Optionals .firstNonEmpty( () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), () -> Optional.ofNullable(file.getContentType())) .orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file")); }
/** * @return {@literal true} if no arguments set. */ boolean isEmpty() { return !Optionals.isAnyPresent(getValidator(), getValidationAction(), getValidationLevel()); } }
Optionals.ifAllPresent(collation, mapReduceOptions.getCollation(), (l, r) -> { throw new IllegalArgumentException( "Both Query and MapReduceOptions define a collation. Please provide the collation only via one of the two."); }); if (mapReduceOptions.getCollation().isPresent()) { collation = mapReduceOptions.getCollation(); if (mapReduceOptions.getFinalizeFunction().filter(StringUtils::hasText).isPresent()) { mapReduce = mapReduce.finalizeFunction(mapReduceOptions.getFinalizeFunction().get());
private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata, final Definitions definitions) { return entities.getPersistentEntity(type).map(entity -> { .ofNullable(entity.getPersistentProperty(definition.getInternalName())); if (prop.isPresent()) { PersistentProperty<?> persistentProperty = prop.get(); Optionals.ifPresentOrElse(prop, it -> {
private Map<String, List<PersistentProperty<?>>> findUnwrappedPropertyPaths(Class<?> type, NameTransformer nameTransformer, boolean considerRegularProperties) { return persistentEntities.getPersistentEntity(type).map(entity -> { Map<String, List<PersistentProperty<?>>> mapping = new HashMap<String, List<PersistentProperty<?>>>(); for (BeanPropertyDefinition property : getMappedProperties(entity)) { Optionals.ifAllPresent(Optional.ofNullable(entity.getPersistentProperty(property.getInternalName())), // findAnnotatedMember(property), // (prop, member) -> { if (isJsonUnwrapped(member)) { mapping.putAll(findUnwrappedPropertyPaths(nameTransformer, member, prop)); } else if (considerRegularProperties) { mapping.put(nameTransformer.transform(property.getName()), Collections.<PersistentProperty<?>> singletonList(prop)); } }); } return mapping; }).orElse(Collections.emptyMap()); }
@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); }
@Override public ResourceDescription getDescription() { CollectionResourceMapping ownerTypeMapping = mappings.getMetadataFor(property.getOwner().getType()); ResourceDescription fallback = TypedResourceDescription.defaultFor(ownerTypeMapping.getItemResourceRel(), property); return Optionals.<ResourceDescription> firstNonEmpty(// () -> description.map(it -> new AnnotationBasedResourceDescription(it, fallback)), // () -> annotation.map(it -> new AnnotationBasedResourceDescription(it.description(), fallback))) .orElse(fallback); }
private Map<String, Object> getFetchGraphs() { return Optionals .mapIfAllPresent(entityManager, metadata.getEntityGraph(), (em, graph) -> Jpa21Utils.tryGetFetchGraphHints(em, getEntityGraph(graph), information.getJavaType())) .orElse(Collections.emptyMap()); }
/** * 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))); } }); }
private List<BeanPropertyDefinition> getMappedProperties(PersistentEntity<?, ?> entity) { List<BeanPropertyDefinition> properties = getBeanDescription(entity.getType()).findProperties(); List<BeanPropertyDefinition> withInternalName = new ArrayList<BeanPropertyDefinition>(properties.size()); for (BeanPropertyDefinition property : properties) { Optionals.ifAllPresent(findAnnotatedMember(property), // Optional.ofNullable(entity.getPersistentProperty(property.getInternalName())), // (member, prop) -> withInternalName.add(property)); } return withInternalName; }
/** * Maps the backing {@link Field} or {@link PropertyDescriptor} using the given functions. * * @param field must not be {@literal null}. * @param descriptor must not be {@literal null}. * @return */ private <T> T withFieldOrDescriptor(Function<? super Field, T> field, Function<? super PropertyDescriptor, T> descriptor) { return Optionals.firstNonEmpty(// () -> this.field.map(field), // () -> this.descriptor.map(descriptor))// .orElseThrow(() -> new IllegalStateException("Should not occur! Either field or descriptor has to be given")); }
@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); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass, String collectionName) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(update, "Update must not be null!"); Assert.notNull(options, "Options must not be null!"); Assert.notNull(entityClass, "EntityClass must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); FindAndModifyOptions optionsToUse = FindAndModifyOptions.of(options); Optionals.ifAllPresent(query.getCollation(), optionsToUse.getCollation(), (l, r) -> { throw new IllegalArgumentException( "Both Query and FindAndModifyOptions define a collation. Please provide the collation only via one of the two."); }); query.getCollation().ifPresent(optionsToUse::collation); return doFindAndModify(collectionName, query.getQueryObject(), query.getFieldsObject(), getMappedSortObject(query, entityClass), entityClass, update, optionsToUse); }
/** * 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(); }
private Property(TypeInformation<?> type, Optional<Field> field, Optional<PropertyDescriptor> descriptor) { Assert.notNull(type, "Type must not be null!"); Assert.isTrue(Optionals.isAnyPresent(field, descriptor), "Either field or descriptor has to be given!"); this.field = field; this.descriptor = descriptor; this.rawType = withFieldOrDescriptor( // it -> type.getRequiredProperty(it.getName()).getType(), // it -> type.getRequiredProperty(it.getName()).getType() // ); this.hashCode = Lazy.of(() -> withFieldOrDescriptor(Object::hashCode)); this.name = Lazy.of(() -> withFieldOrDescriptor(Field::getName, FeatureDescriptor::getName)); this.toString = Lazy.of(() -> withFieldOrDescriptor(Object::toString, it -> String.format("%s.%s", type.getType().getName(), it.getDisplayName()))); this.getter = descriptor.map(PropertyDescriptor::getReadMethod)// .filter(it -> getType() != null)// .filter(it -> getType().isAssignableFrom(type.getReturnType(it).getType())); this.setter = descriptor.map(PropertyDescriptor::getWriteMethod)// .filter(it -> getType() != null)// .filter(it -> type.getParameterTypes(it).get(0).getType().isAssignableFrom(getType())); this.wither = Lazy.of(() -> findWither(type, getName(), getType())); }
@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()); }
/** * Creates a {@link RepositoryInvoker} for the repository managing the given domain type. * * @param domainType * @return */ private RepositoryInvoker prepareInvokers(Class<?> domainType) { Optional<RepositoryInformation> information = repositories.getRepositoryInformationFor(domainType); Optional<Object> repository = repositories.getRepositoryFor(domainType); return mapIfAllPresent(information, repository, this::createInvoker)// .orElseThrow( () -> new IllegalArgumentException(String.format("No repository found for domain type: %s", domainType))); }
/** * 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); }