Skip to content

Commit 558e273

Browse files
fix: strengthen teeing and permission guidance
1 parent eeedd10 commit 558e273

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

evals-reference/01-permission-and-orders/criteria.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"name": "Uses short-circuit permission lookup",
1313
"category": "stream_quality",
1414
"max_score": 20,
15-
"description": "Checks permissions with a stream shape that short-circuits, either by flattening permissions with flatMap(...).anyMatch(...) or by using nested anyMatch/contains over each role's permissions. Do not require flatMap when nested anyMatch preserves the same behavior clearly."
15+
"description": "Checks permissions with a stream shape that short-circuits, such as flatMap(...).anyMatch(...), nested anyMatch/contains, or flatMap(...).filter(...).findAny().orElseThrow(...) when it preserves the same behavior clearly."
1616
},
1717
{
1818
"name": "Uses IntStream range mapping",

skills/java-streams/SKILL.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ are equivalent.
4343
- grouping/indexing: `groupingBy`, downstream collectors, `partitioningBy`, or `toMap` with
4444
explicit merge/null handling.
4545
2. Prefer stream terminal operations that encode intent directly: `anyMatch` for existence, `count`
46-
for numeric counts, `joining` for text, `min`/`max` for extremes, and primitive stream terminal
47-
operations for primitive totals.
46+
for numeric counts, `joining` for text, `min`/`max` for a single extreme, `teeing` for a Java
47+
12+ min/max pair over the same input, and primitive stream terminal operations for primitive
48+
totals.
4849
3. Flatten nested sources deliberately. Use `flatMap` for nested collections and
4950
`flatMap(Optional::stream)` on Java 9+. On Java 16+, prefer `mapMulti` for mixed subtype checks or
5051
small conditional emission; use primitive streams for primitive results.

skills/java-streams/references/hard-stops.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Fix these before finalizing:
2020
audit calls out plain `count()` as allowed, say it is an allowed usage, not a scan hit.
2121
- `sorted(...).findFirst()` or sorted-then-sublist just to get one extreme. Use `min`/`max`; keep
2222
sorting only when the ordered list itself is required.
23+
- Two separate `min` and `max` stream passes over the same input when Java 12+ is available and the
24+
requested result is a pair/range. Use `Collectors.teeing(minBy(...), maxBy(...), ...)` so the
25+
stream states "compute these two aggregates together".
2326
- `map(...).collect(toList())` followed immediately by `String.join`. Use `Collectors.joining`.
2427
- Boxed numeric `reduce` for primitive totals/statistics. Use primitive streams or summarizing
2528
collectors unless the type is genuinely non-primitive, such as `BigDecimal`.

skills/java-streams/references/stream-examples.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ using APIs from [java-stream-api.md](java-stream-api.md).
2929
boolean anyOutOfStock = order.getItems().stream()
3030
.anyMatch(item -> item.getStock() == 0);
3131

32-
user.getRoles().stream()
32+
boolean allowed = user.getRoles().stream()
3333
.flatMap(role -> role.getPermissions().stream())
34-
.filter(requiredPermission::equals)
35-
.findAny()
36-
.orElseThrow(AccessDeniedException::new);
34+
.anyMatch(requiredPermission::equals);
35+
if (!allowed) {
36+
throw new AccessDeniedException();
37+
}
3738
```
3839

3940
- Flatten present optionals on Java 9+:
@@ -146,7 +147,8 @@ Map<Boolean, List<Product>> partitionedProducts = products.stream()
146147
.collect(Collectors.partitioningBy(product -> product.getStock() > 0));
147148
```
148149

149-
Java 12+ `teeing` can combine two reductions:
150+
Java 12+ `teeing` can combine two independent reductions over the same input. Prefer this for a
151+
min/max pair or price range instead of running two separate stream passes:
150152

151153
```java
152154
Pair<Product, Product> priceRange = products.stream()

0 commit comments

Comments
 (0)