Skip to content

Commit 38f63f6

Browse files
committed
make getAllEntities stateType param functional with TypedEntityQueryPageable
1 parent 5a5bb01 commit 38f63f6

File tree

2 files changed

+87
-13
lines changed

2 files changed

+87
-13
lines changed

client/src/main/java/com/microsoft/durabletask/DurableEntityClient.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,29 @@ public EntityQueryPageable getAllEntities() {
183183

184184
/**
185185
* Returns an auto-paginating iterable over entity instances matching the specified filter criteria,
186-
* with state included for typed access.
186+
* with typed state access.
187187
* <p>
188-
* This convenience overload ensures that entity state is fetched, matching the .NET SDK's
189-
* {@code GetAllEntitiesAsync<T>()} pattern. Use {@link EntityMetadata#readStateAs(Class)} on
190-
* each result to access the typed state.
188+
* This mirrors the .NET SDK's {@code GetAllEntitiesAsync<T>()} pattern. Entity state is always
189+
* included in the results and eagerly deserialized into the specified type. Each item is a
190+
* {@link TypedEntityMetadata} with a {@link TypedEntityMetadata#getState()} accessor.
191191
* <p>
192-
* Note: The provided query's {@code includeState} setting is preserved. A copy of the query
193-
* is made with {@code includeState} set to {@code true} so the original query is not modified.
192+
* Note: A copy of the query is made with {@code includeState} set to {@code true} so the
193+
* original query is not modified.
194194
*
195195
* <pre>{@code
196196
* EntityQuery query = new EntityQuery().setInstanceIdStartsWith("counter");
197-
* for (EntityMetadata entity : client.getEntities().getAllEntities(query, Integer.class)) {
198-
* Integer state = entity.readStateAs(Integer.class);
197+
* for (TypedEntityMetadata<Integer> entity : client.getEntities().getAllEntities(query, Integer.class)) {
198+
* Integer state = entity.getState();
199199
* System.out.println("Counter value: " + state);
200200
* }
201201
* }</pre>
202202
*
203203
* @param query the query filter criteria
204-
* @param stateType the expected type of the entity's state, used with
205-
* {@link EntityMetadata#readStateAs(Class)} for deserialization
204+
* @param stateType the class to deserialize each entity's state into
206205
* @param <T> the entity state type
207-
* @return a pageable iterable over all matching entities with state included
206+
* @return a pageable iterable over all matching entities with typed state
208207
*/
209-
public <T> EntityQueryPageable getAllEntities(EntityQuery query, Class<T> stateType) {
208+
public <T> TypedEntityQueryPageable<T> getAllEntities(EntityQuery query, Class<T> stateType) {
210209
// Create a copy with includeState=true so we don't mutate the caller's query
211210
EntityQuery typedQuery = new EntityQuery()
212211
.setInstanceIdStartsWith(query.getInstanceIdStartsWith())
@@ -216,7 +215,8 @@ public <T> EntityQueryPageable getAllEntities(EntityQuery query, Class<T> stateT
216215
.setIncludeTransient(query.isIncludeTransient())
217216
.setPageSize(query.getPageSize())
218217
.setContinuationToken(query.getContinuationToken());
219-
return new EntityQueryPageable(typedQuery, this::queryEntities);
218+
EntityQueryPageable inner = new EntityQueryPageable(typedQuery, this::queryEntities);
219+
return new TypedEntityQueryPageable<>(inner, stateType);
220220
}
221221

222222
/**
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.microsoft.durabletask;
4+
5+
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
7+
8+
/**
9+
* An auto-paginating iterable over entity query results with typed state access.
10+
* <p>
11+
* This class wraps an {@link EntityQueryPageable} and yields {@link TypedEntityMetadata} items
12+
* with eagerly deserialized state, mirroring the .NET SDK's {@code AsyncPageable<EntityMetadata<TState>>}
13+
* returned by {@code GetAllEntitiesAsync<T>()}.
14+
* <p>
15+
* Use {@link DurableEntityClient#getAllEntities(EntityQuery, Class)} to obtain an instance.
16+
*
17+
* <h3>Example:</h3>
18+
* <pre>{@code
19+
* EntityQuery query = new EntityQuery().setInstanceIdStartsWith("counter");
20+
* for (TypedEntityMetadata<Integer> entity : client.getEntities().getAllEntities(query, Integer.class)) {
21+
* Integer state = entity.getState();
22+
* System.out.println("Counter value: " + state);
23+
* }
24+
* }</pre>
25+
*
26+
* @param <T> the entity state type
27+
*/
28+
public final class TypedEntityQueryPageable<T> implements Iterable<TypedEntityMetadata<T>> {
29+
private final EntityQueryPageable inner;
30+
private final Class<T> stateType;
31+
32+
/**
33+
* Creates a new {@code TypedEntityQueryPageable}.
34+
*
35+
* @param inner the underlying pageable that fetches raw entity metadata
36+
* @param stateType the class to deserialize each entity's state into
37+
*/
38+
TypedEntityQueryPageable(EntityQueryPageable inner, Class<T> stateType) {
39+
this.inner = inner;
40+
this.stateType = stateType;
41+
}
42+
43+
/**
44+
* Returns an iterator over individual {@link TypedEntityMetadata} items with eagerly
45+
* deserialized state, automatically fetching subsequent pages as needed.
46+
*
47+
* @return an iterator over all matching entities with typed state
48+
*/
49+
@Override
50+
public Iterator<TypedEntityMetadata<T>> iterator() {
51+
return new TypedEntityItemIterator(inner.iterator());
52+
}
53+
54+
private class TypedEntityItemIterator implements Iterator<TypedEntityMetadata<T>> {
55+
private final Iterator<EntityMetadata> delegate;
56+
57+
TypedEntityItemIterator(Iterator<EntityMetadata> delegate) {
58+
this.delegate = delegate;
59+
}
60+
61+
@Override
62+
public boolean hasNext() {
63+
return delegate.hasNext();
64+
}
65+
66+
@Override
67+
public TypedEntityMetadata<T> next() {
68+
if (!hasNext()) {
69+
throw new NoSuchElementException();
70+
}
71+
return new TypedEntityMetadata<>(delegate.next(), stateType);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)