Skip to content

Commit 835c74b

Browse files
LazyList: update class and Query docs.
1 parent e68da00 commit 835c74b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

objectbox-java/src/main/java/io/objectbox/query/LazyList.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,33 @@
2222
import java.util.ListIterator;
2323
import java.util.NoSuchElementException;
2424

25+
import javax.annotation.Nullable;
26+
2527
import io.objectbox.Box;
2628
import io.objectbox.exception.DbException;
2729

2830
/**
29-
* A thread-safe, unmodifiable list that reads entities lazily once they are accessed.
30-
* A lazy list can be cached or not.
31-
* Cached lazy lists store the previously accessed objects to avoid loading entities more than once.
32-
* Some features of the list are limited to cached lists (e.g. features that require the entire list).
31+
* A thread-safe, unmodifiable {@link List} that gets Objects from their Box not until they are accessed.
32+
* Internally the list is backed by an array of Object IDs.
33+
* <p>
34+
* If the list is set to not cache retrieved Objects, each operation will get the latest version of an Object
35+
* from its Box. However, in this mode only a limited set of {@link List} operations,
36+
* like get or iterator are supported.
3337
* <p>
34-
* Note: this list gives an semiconsitent view on the data at the moment it was created.
35-
* If you remove objects from their object box after this list was created, this list will null instead of an object.
36-
* However, if you add objects to their object box after this list was created, this list will not be extended.
38+
* If the list is set to cache retrieved Objects, operations will return a previously fetched version of an Object,
39+
* which might not equal the latest version in its Box. However, in this mode almost all {@link List}
40+
* operations are supported. Note that operations that require the whole list, like contains, will fetch all
41+
* Objects in this list from the Box at once.
42+
* <p>
43+
* Note: as Objects are fetched on demand, this list returns a null Object if the Object was removed from its Box
44+
* after this list was created.
3745
*
3846
* @param <E> Object type (entity).
39-
* @author Markus
4047
*/
41-
// Threading note: locking is tailored to ArrayList assuming that concurrent positional gets/sets are OK.
42-
// To enable this, the internal ArrayList is prepopulated with null.
48+
/*
49+
Threading note: locking is tailored to ArrayList assuming that concurrent positional gets/sets are OK.
50+
To enable this, the internal ArrayList is prepopulated with null.
51+
*/
4352
public class LazyList<E> implements List<E> {
4453
protected class LazyIterator implements ListIterator<E> {
4554
private int index;
@@ -206,9 +215,11 @@ public boolean containsAll(Collection<?> collection) {
206215
}
207216

208217
/**
209-
* @return An object for the given ID, or null if the object was already removed from its box
210-
* (and was not cached before).
218+
* Gets and returns the Object at the specified position in this list from its Box. Returns null if the
219+
* Object was removed from its Box. If this list is caching retrieved Objects, returns the previously
220+
* fetched version.
211221
*/
222+
@Nullable
212223
@Override
213224
public E get(int location) {
214225
if (location < 0 || location > size) {

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,27 @@ public long[] findIds(final long offset, final long limit) {
250250
}
251251

252252
/**
253-
* Find all Objects matching the query without actually loading the Objects. See @{@link LazyList} for details.
253+
* Like {@link #findIds()}, but wraps the Object IDs in an unmodifiable {@link LazyList}
254+
* so Objects can be retrieved on demand. The LazyList does not cache retrieved Objects, so only basic
255+
* {@link List} operations like getting or iterating list items are supported. See {@link LazyList} for details.
254256
*/
257+
@Nonnull
255258
public LazyList<T> findLazy() {
256259
ensureNoFilterNoComparator();
257260
return new LazyList<>(box, findIds(), false);
258261
}
259262

263+
/**
264+
* Like {@link #findIds()}, but wraps the Object IDs in an unmodifiable, caching {@link LazyList}
265+
* so Objects can be retrieved on demand. The LazyList caches retrieved Objects supporting almost
266+
* all {@link List} operations, at the expense of used memory. See {@link LazyList} for details.
267+
*/
268+
@Nonnull
269+
public LazyList<T> findLazyCached() {
270+
ensureNoFilterNoComparator();
271+
return new LazyList<>(box, findIds(), true);
272+
}
273+
260274
/**
261275
* Creates a {@link PropertyQuery} for the given property.
262276
* <p>
@@ -309,15 +323,6 @@ public void forEach(final QueryConsumer<T> consumer) {
309323
});
310324
}
311325

312-
/**
313-
* Find all Objects matching the query without actually loading the Objects. See @{@link LazyList} for details.
314-
*/
315-
@Nonnull
316-
public LazyList<T> findLazyCached() {
317-
ensureNoFilterNoComparator();
318-
return new LazyList<>(box, findIds(), true);
319-
}
320-
321326
void resolveEagerRelations(List<T> entities) {
322327
if (eagerRelations != null) {
323328
int entityIndex = 0;

0 commit comments

Comments
 (0)