Java源码示例:org.springframework.data.util.Streamable

示例1
@PostMapping("/orders/new")
HttpEntity<?> createOrder() {

	Iterable<ProductInfo> infos = productInfos.findAll(Sort.by("createdDate").descending());

	ProductInfo info = Streamable.of(infos).stream() //
			.findFirst() //
			.orElseThrow(() -> new IllegalStateException("No ProductInfo found!"));

	Order order = Order.newOrder();
	order.add(info, 2);

	orders.save(order.complete());

	return ResponseEntity //
			.created(links.linkForSingleResource(Order.class, order.getId()).toUri()) //
			.build();
}
 
示例2
/**
 * Runs the given {@link ConsumerWithException} for each element in the given {@link Iterable} in parallel waiting for
 * all executions to complete before returning. Exceptions being thrown in the {@link ConsumerWithException} will be
 * converted into {@link RuntimeException}s.
 *
 * @param executor must not be {@literal null}.
 * @param streamable must not be {@literal null}.
 * @param consumer must not be {@literal null}.
 */
public static <T> void run(Executor executor, Streamable<T> streamable, ConsumerWithException<T> consumer) {

	Assert.notNull(executor, "Executor must not be null!");
	Assert.notNull(streamable, "Streamable must not be null!");
	Assert.notNull(consumer, "Consumer must not be null!");

	streamable.stream().//
			map(it -> CompletableFuture.runAsync(() -> {
				try {
					consumer.accept(it);
				} catch (Exception o_O) {
					log.error(o_O.getMessage(), o_O);
					throw new RuntimeException(o_O);
				}
			}, executor)).collect(Collectors.toList()).forEach(CompletableFuture::join);
}
 
示例3
@Override
public Streamable<T> findBy(UserAccount userAccount, Interval interval) {

	Assert.notNull(userAccount, "UserAccount must not be null");
	Assert.notNull(interval, "Interval must not be null!");

	return orderRepository.findByUserAccountAndDateCreatedBetween(userAccount, interval.getStart(), interval.getEnd());
}
 
示例4
@Test
void periodSetTest() {

	Interval interval = Interval.from(from).to(to);

	Map<Interval, Streamable<AccountancyEntry>> m = a.find(interval, Duration.ofMillis(200));
	for (Entry<Interval, Streamable<AccountancyEntry>> e : m.entrySet()) {
		for (AccountancyEntry p : e.getValue()) {}
	}
}
 
示例5
@Test // #232
void findsByAllCategories() {

	Cookie first = createCookie();

	Cookie second = createCookie();
	second.addCategory("special");

	Streamable<Product> result = catalog.findByAllCategories("chocolate", "special");

	assertThat(result) //
			.containsExactly(second) //
			.doesNotContain(first);
}
 
示例6
@Query("MATCH (me:User {userId: {0}})-[:FRIEND]-(friends),\n" +
        "\t(nonFriend:User)-[:FRIEND]-(friends)\n" +
        "WHERE NOT (me)-[:FRIEND]-(nonFriend)\n" +
        "WITH nonFriend, count(nonFriend) as mutualFriends\n" +
        "RETURN nonFriend as User, mutualFriends as Weight\n" +
        "ORDER BY Weight DESC")
Streamable<RankedUser> recommendedFriends(Long userId);
 
示例7
public static <T, S, R> R runAndReturn(Executor executor, Streamable<T> streamable, Function<T, S> function,
		Collector<? super S, ?, R> collector) {

	Assert.notNull(streamable, "Iterable must not be null!");
	Assert.notNull(function, "Function must not be null!");

	return streamable.stream().//
			map(it -> CompletableFuture.supplyAsync(() -> function.apply(it), executor)).//
			filter(Objects::nonNull).//
			collect(Collectors.toList()).//
			stream().//
			map(CompletableFuture::join).//
			collect(collector);
}
 
示例8
Map<Project, MaintainedVersions> findVersions(List<Train> trains) {

		Assert.notNull(trains, "Trains must not be null!");

		return ExecutionUtils.runAndReturn(executor, Streamable.of(trains), train -> {
			return ExecutionUtils.runAndReturn(executor,
					Streamable.of(() -> train.stream().filter(module -> !TO_FILTER.contains(module.getProject()))), module -> {
						return getLatestVersion(module, train);
					});
		}).stream().flatMap(Collection::stream).collect(
				Collectors.groupingBy(MaintainedVersion::getProject, ListWrapperCollector.collectInto(MaintainedVersions::of)));
	}
 
示例9
/**
 * 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 cntQryPlaceHolder the placeholder for the count clause, must not be {@literal null}.
 * @param idAttrs           the id attributes for the entity, must not be {@literal null}.
 * @return the exists query string
 */
public static String getExistsQueryString(String entityName,
    String cntQryPlaceHolder,
    Iterable<String> idAttrs) {
    String whereClause = Streamable.of(idAttrs).stream() //
        .map(idAttribute -> String.format(EQUALS_CONDITION_STRING, "x", idAttribute,
            idAttribute)) //
        .collect(Collectors.joining(" AND ", " WHERE ", ""));

    return String.format(COUNT_QUERY_STRING, cntQryPlaceHolder, entityName) + whereClause;
}
 
示例10
/**
 * 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 cntQryPlaceHolder the placeholder for the count clause, must not be {@literal null}.
 * @param idAttrs           the id attributes for the entity, must not be {@literal null}.
 * @return the exists query string
 */
public static String getExistsQueryString(String entityName,
    String cntQryPlaceHolder,
    Iterable<String> idAttrs) {
    String whereClause = Streamable.of(idAttrs).stream() //
        .map(idAttribute -> String.format(EQUALS_CONDITION_STRING, "x", idAttribute,
            idAttribute)) //
        .collect(Collectors.joining(" AND ", " WHERE ", ""));

    return String.format(COUNT_QUERY_STRING, cntQryPlaceHolder, entityName) + whereClause;
}
 
示例11
@Test // #230
void createsRollingBackEntryOnOrderCancellation() {

	listener.on(OrderPaid.of(order));
	listener.on(OrderCancelled.of(order, "Testing"));

	Streamable<AccountancyEntry> entries = accountancy.findAll();

	assertThat(entries).hasSize(2);
	assertThat(entries.stream() //
			.map(AccountancyEntry::getValue) //
			.reduce(ZERO_EURO, MonetaryAmount::add)//
	).isEqualTo(ZERO_EURO);
}
 
示例12
@Override
public final Map<Interval, Streamable<AccountancyEntry>> find(Interval interval, TemporalAmount duration) {

	Assert.notNull(interval, "Interval must not be null");
	Assert.notNull(duration, "TemporalAmount must not be null");

	return Intervals.divide(interval, duration).stream() //
			.collect(toMap(identity(), this::find));
}
 
示例13
/**
 * Sums up the prices of all given {@link Priced} instances.
 * 
 * @param priced must not be {@literal null}.
 * @return
 */
static MonetaryAmount sumUp(Iterable<? extends Priced> priced) {

	Assert.notNull(priced, "Iterable must not be null!");

	return Streamable.of(priced).stream()//
			.map(Priced::getPrice)//
			.reduce((left, right) -> left.add(right)) //
			.orElse(Currencies.ZERO_EURO);
}
 
示例14
/**
 * 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))));
}
 
示例15
/**
 * Returns all {@link AttachedChargeLine}s for the given {@link OrderLine}.
 *
 * @param orderLine must not be {@literal null}.
 * @return
 * @since 7.1
 */
public Totalable<AttachedChargeLine> getChargeLines(OrderLine orderLine) {

	List<AttachedChargeLine> foo = attachedChargeLines;

	return Totalable.of(Streamable.of(() -> foo.stream() //
			.filter(it -> it.belongsTo(orderLine))));
}
 
示例16
/**
 * Creates a new {@link OrderCompletionReport} for the given {@link Order} and {@link OrderLineCompletion}s.
 * 
 * @param order must not be {@literal null}.
 * @param completions must not be {@literal null}.
 * @return will never be {@literal null}.
 */
static OrderCompletionReport forCompletions(Order order, Streamable<OrderLineCompletion> completions) {

	return new OrderCompletionReport(order, completions.stream().anyMatch(OrderLineCompletion::isFailure) //
			? CompletionStatus.FAILED //
			: CompletionStatus.SUCCEEDED //
			, completions);
}
 
示例17
@Override
public Streamable<T> findBy(Interval interval) {
	return orderRepository.findByDateCreatedBetween(interval.getStart(), interval.getEnd());
}
 
示例18
public Streamable<Extension> findExtensions(Namespace namespace) {
    return extensionRepo.findByNamespaceOrderByNameAsc(namespace);
}
 
示例19
public Streamable<Extension> findAllExtensions() {
    return extensionRepo.findAll();
}
 
示例20
public Streamable<ExtensionVersion> findVersions(Extension extension) {
     return extensionVersionRepo.findByExtension(extension);
}
 
示例21
public Streamable<ExtensionVersion> findVersions(Extension extension, boolean preview) {
     return extensionVersionRepo.findByExtensionAndPreview(extension, preview);
}
 
示例22
public Streamable<ExtensionVersion> findBundledExtensionsReference(Extension extension) {
    return extensionVersionRepo.findByBundledExtensions(extension);
}
 
示例23
public Streamable<ExtensionVersion> findDependenciesReference(Extension extension) {
    return extensionVersionRepo.findByDependencies(extension);
}
 
示例24
public Streamable<ExtensionVersion> findAllExtensionVersions() {
    return extensionVersionRepo.findAll();
}
 
示例25
public Streamable<FileResource> findFiles(ExtensionVersion extVersion) {
    return fileResourceRepo.findByExtension(extVersion);
}
 
示例26
public Streamable<ExtensionReview> findAllReviews(Extension extension) {
    return extensionReviewRepo.findByExtension(extension);
}
 
示例27
public Streamable<ExtensionReview> findActiveReviews(Extension extension, UserData user) {
    return extensionReviewRepo.findByExtensionAndUserAndActiveTrue(extension, user);
}
 
示例28
public Streamable<UserData> findUsersByLoginNameStartingWith(String loginNameStart) {
    return userDataRepo.findByLoginNameStartingWith(loginNameStart);
}
 
示例29
public Streamable<NamespaceMembership> findMemberships(Namespace namespace, String role) {
    return membershipRepo.findByNamespaceAndRoleIgnoreCase(namespace, role);
}
 
示例30
public Streamable<NamespaceMembership> findMemberships(UserData user, String role) {
    return membershipRepo.findByUserAndRoleIgnoreCaseOrderByNamespaceName(user, role);
}