/** * Returns the query string to execute an exists query for the given id attributes. * * @param entityName the name of the entity to create the query for, must not be {@literal null}. * @param countQueryPlaceHolder the placeholder for the count clause, must not be {@literal null}. * @param idAttributes the id attributes for the entity, must not be {@literal null}. */ public static String getExistsQueryString(String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes) { String whereClause = Streamable.of(idAttributes).stream() // .map(idAttribute -> String.format(EQUALS_CONDITION_STRING, "x", idAttribute, idAttribute)) // .collect(Collectors.joining(" AND ", " WHERE ", "")); return String.format(COUNT_QUERY_STRING, countQueryPlaceHolder, entityName) + whereClause; }
/** * Invokes the given {@link Supplier}s for {@link Optional} results one by one and returns the first non-empty one. * * @param suppliers must not be {@literal null}. * @return */ public static <T> Optional<T> firstNonEmpty(Iterable<Supplier<Optional<T>>> suppliers) { Assert.notNull(suppliers, "Suppliers must not be null!"); return Streamable.of(suppliers).stream()// .map(Supplier::get)// .filter(Optional::isPresent)// .findFirst().orElse(Optional.empty()); }
/** * Returns the {@link PersistentEntity} identified by the given type. * * @param type * @return * @throws IllegalStateException if the entity cannot be detected uniquely as multiple ones might share the same * identifier. */ @Nullable private PersistentEntity<?, ?> getEntityIdentifiedBy(TypeInformation<?> type) { Collection<PersistentEntity<?, ?>> entities = contexts.stream() // .flatMap(it -> it.getPersistentEntities().stream()) // .map(it -> it.getIdProperty()) // .filter(it -> it != null && type.equals(it.getTypeInformation().getActualType())) // .map(it -> it.getOwner()) // .collect(Collectors.toList()); if (entities.size() > 1) { String message = "Found multiple entities identified by " + type.getType() + ": "; message += entities.stream().map(it -> it.getType().getName()).collect(Collectors.joining(", ")); message += "! Introduce dedciated unique identifier types or explicitly define the target type in @Reference!"; throw new IllegalStateException(message); } return entities.isEmpty() ? null : entities.iterator().next(); } }
@Override public final Map<Interval, MonetaryAmount> salesVolume(Interval interval, TemporalAmount duration) { Assert.notNull(interval, "Interval must not be null"); Assert.notNull(duration, "TemporalAmount must not be null"); return find(interval, duration).entrySet().stream().// collect(toMap(Entry::getKey, entry -> entry.getValue().stream().// map(AccountancyEntry::getValue).// reduce(Money.of(0, Currencies.EURO), MonetaryAmount::add))); } }
private Map<Method, RepositoryQuery> mapMethodsToQuery(RepositoryInformation repositoryInformation, QueryLookupStrategy lookupStrategy, ProjectionFactory projectionFactory) { return repositoryInformation.getQueryMethods().stream() // .map(method -> lookupQuery(method, repositoryInformation, lookupStrategy, projectionFactory)) // .peek(pair -> invokeListeners(pair.getSecond())) // .collect(Pair.toMap()); }
/** * Returns a new {@link Streamable} that will apply the given filter {@link Predicate} to the current one. * * @param predicate must not be {@literal null}. * @return * @see Stream#filter(Predicate) */ default Streamable<T> filter(Predicate<? super T> predicate) { Assert.notNull(predicate, "Filter predicate must not be null!"); return Streamable.of(() -> stream().filter(predicate)); }
public Streamable<String> getBasePackages() { String[] value = attributes.getStringArray("value"); String[] basePackages = attributes.getStringArray(BASE_PACKAGES); Class<?>[] basePackageClasses = attributes.getClassArray(BASE_PACKAGE_CLASSES); // Default configuration - return package of annotated class if (value.length == 0 && basePackages.length == 0 && basePackageClasses.length == 0) { String className = configMetadata.getClassName(); return Streamable.of(ClassUtils.getPackageName(className)); } Set<String> packages = new HashSet<>(); packages.addAll(Arrays.asList(value)); packages.addAll(Arrays.asList(basePackages)); Arrays.stream(basePackageClasses)// .map(ClassUtils::getPackageName)// .forEach(it -> packages.add(it)); return Streamable.of(packages); }
/** * Exposes variables for all named parameters for the given arguments. Also exposes non-bindable parameters under the * names of their types. * * @param parameters must not be {@literal null}. * @param arguments must not be {@literal null}. * @return */ private static Map<String, Object> collectVariables(Streamable<? extends Parameter> parameters, Object[] arguments) { Map<String, Object> variables = new HashMap<>(); parameters.stream()// .filter(Parameter::isSpecialParameter)// .forEach(it -> variables.put(// StringUtils.uncapitalize(it.getType().getSimpleName()), // arguments[it.getIndex()])); parameters.stream()// .filter(Parameter::isNamedParameter)// .forEach(it -> variables.put(// it.getName().orElseThrow(() -> new IllegalStateException("Should never occur!")), // arguments[it.getIndex()])); return variables; }
/** * Returns the {@link PersistentEntity} for the given type. Will consider all {@link MappingContext}s registered but * return {@literal Optional#empty()} in case none of the registered ones already have a {@link PersistentEntity} * registered for the given type. * * @param type can be {@literal null}. * @return */ public Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> getPersistentEntity(Class<?> type) { return contexts.stream()// .filter(it -> it.hasPersistentEntityFor(type))// .findFirst().map(it -> it.getRequiredPersistentEntity(type)); }
private Set<BeanDefinition> findCandidateBeanDefinitions(ImplementationDetectionConfiguration config) { String postfix = config.getImplementationPostfix(); ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment); provider.setResourceLoader(resourceLoader); provider.setResourcePattern(String.format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, postfix)); provider.setMetadataReaderFactory(config.getMetadataReaderFactory()); provider.addIncludeFilter((reader, factory) -> true); config.getExcludeFilters().forEach(it -> provider.addExcludeFilter(it)); return config.getBasePackages().stream()// .flatMap(it -> provider.findCandidateComponents(it).stream())// .collect(Collectors.toSet()); }
/** * Creates a counter {@link ProductPaymentEntry} for the order that is cancelled if there's a revenue entry for the * given order already, i.e. the order has been paid before. * * @param event must not be {@literal null}. * @since 7.1 */ @EventListener public void on(OrderCancelled event) { Order order = event.getOrder(); if (accountancy.findAll().stream() // .map(ProductPaymentEntry.class::cast) // .anyMatch(it -> it.belongsTo(order) && it.isRevenue())) { accountancy.add(ProductPaymentEntry.rollback(order, String.format("Order %s cancelled! Reason: %s.", order.getId(), event.getReason()))); } } }
/** * Returns all {@link OrderLine} instances that refer to the given {@link Product}. * * @param product must not be {@literal null}. * @return * @since 7.1 */ public Totalable<OrderLine> getOrderLines(Product product) { Assert.notNull(product, "Product must not be null!"); return Totalable.of(Streamable.of(() -> orderLines.stream() // .filter(it -> it.refersTo(product)))); }
/** * Creates an {@link OrderCompletionReport} representing a successful verification of the given {@link Order}. * * @param order must not be {@literal null}. * @return will never be {@literal null}. */ public static OrderCompletionReport success(Order order) { return OrderCompletionReport.forCompletions(order, Streamable.of(() -> order.getOrderLines().stream().map(it -> OrderLineCompletion.success(it)))); }
/** * Creates a new {@link CustomConversions} instance registering the given converters. * * @param storeConversions must not be {@literal null}. * @param converters must not be {@literal null}. */ public CustomConversions(StoreConversions storeConversions, Collection<?> converters) { Assert.notNull(storeConversions, "StoreConversions must not be null!"); Assert.notNull(converters, "List of converters must not be null!"); List<Object> toRegister = new ArrayList<>(); // Add user provided converters to make sure they can override the defaults toRegister.addAll(converters); toRegister.addAll(storeConversions.getStoreConverters()); toRegister.addAll(DEFAULT_CONVERTERS); toRegister.stream()// .flatMap(it -> storeConversions.getRegistrationsFor(it).stream())// .forEach(this::register); Collections.reverse(toRegister); this.converters = Collections.unmodifiableList(toRegister); this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, storeConversions.getStoreTypeHolder()); }