generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMapResult.java
More file actions
81 lines (69 loc) · 3.15 KB
/
MapResult.java
File metadata and controls
81 lines (69 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.model;
import java.util.Collections;
import java.util.List;
/**
* Result container for map operations.
*
* <p>Holds ordered results from a map operation. Each index corresponds to the input item at the same position. Each
* item is represented as a {@link MapResultItem} containing its status, result, and error. Includes the
* {@link ConcurrencyCompletionStatus} indicating why the operation completed.
*
* <p>Errors are stored as {@link MapError} rather than raw Throwable, so they survive serialization across
* checkpoint-and-replay cycles without requiring AWS SDK-specific Jackson modules.
*
* @param items ordered result items from the map operation
* @param completionReason why the operation completed
* @param <T> the result type of each item
*/
public record MapResult<T>(List<MapResultItem<T>> items, ConcurrencyCompletionStatus completionReason) {
/** Compact constructor that applies defensive copy and defaults. */
public MapResult {
items = items != null ? List.copyOf(items) : Collections.emptyList();
completionReason = completionReason != null ? completionReason : ConcurrencyCompletionStatus.ALL_COMPLETED;
}
/** Returns an empty MapResult with no items. */
public static <T> MapResult<T> empty() {
return new MapResult<>(Collections.emptyList(), ConcurrencyCompletionStatus.ALL_COMPLETED);
}
/** Returns the result item at the given index. */
public MapResultItem<T> getItem(int index) {
return items.get(index);
}
/** Returns the result at the given index, or null if that item failed or was not started. */
public T getResult(int index) {
return items.get(index).result();
}
/** Returns the error at the given index, or null if that item succeeded or was not started. */
public MapError getError(int index) {
return items.get(index).error();
}
/** Returns true if all items succeeded (no failures or not-started items). */
public boolean allSucceeded() {
return items.stream().allMatch(item -> item.status() == MapResultItem.Status.SUCCEEDED);
}
/** Returns the number of items in this result. */
public int size() {
return items.size();
}
/** Returns all results as an unmodifiable list (nulls for failed/not-started items). */
public List<T> results() {
return Collections.unmodifiableList(
items.stream().map(MapResultItem::result).toList());
}
/** Returns results from items that succeeded (includes null results from successful items). */
public List<T> succeeded() {
return items.stream()
.filter(item -> item.status() == MapResultItem.Status.SUCCEEDED)
.map(MapResultItem::result)
.toList();
}
/** Returns errors from items that failed. */
public List<MapError> failed() {
return items.stream()
.filter(item -> item.status() == MapResultItem.Status.FAILED)
.map(MapResultItem::error)
.toList();
}
}