Tool Methods
Jimmer provides a series of static tool methods for operating on dynamic objects or their Drafts.
For brevity, this article only lists the strongly typed usages.
There are also weakly typed versions of these refactoring methods.
Operating on Immutable Objects
isLoaded
- Purpose: Determine if a property of a dynamic object is loaded
- Java
- Kotlin
if (ImmutableObjects.isLoaded(book, BookProps.AUTHORS)) {
...
}
if (isLoaded(book, Book::authors)) {
...
}
get
-
Purpose: Dynamically get a property of an object
-
Exception: The property of the object is in an unloaded state
- Java
- Kotlin
List<Author> authors =
ImmutableObjects.get(book, BookProps.AUTHORS);
val authors = get(book, Book::authors)
isIdOnly
-
Purpose: Check if a dynamic object is only set with an id property
-
Precondition: The object type is annotated with
@Entity
and is an ORM entity with an id
- Java
- Kotlin
if (ImmutableObjects.isIdOnly(book)) {
...
}
if (isIdOnly(book)) {
...
}
makeIdOnly
-
Purpose: Construct an object of the given type and set its id property
-
Precondition: The object type is annotated with
@Entity
and is an ORM entity with an id
- Java
- Kotlin
Book book = ImmutableObjects.makeIdOnly(Book.class, 1L);
val book = makeIdOnly(Book::class, 1L)
-
For Kotlin, the id parameter must be non-null, and the return value is also non-null.
To accept a possibly null id and directly return null when id is null, call
makeNullableIdOnly
-
Java's
makeIdOnly
is actually equivalent to Kotlin'smakeNullableIdOnly
.
isLonely
-
Purpose: Check if an object is only set with an id property. I.e. no associated properties are set to non-null (including unset and set to null).
infoIf the object is an ORM entity, it is an exception if a one-to-one or many-to-one property based directly on a foreign key is set to an associated object with only an id.
- Java
- Kotlin
if (ImmutableObjects.isLonely(book)) {
...
}
if (isLonely(book)) {
...
}
toLonely
-
Purpose: Create a new object based on an existing object. The new object copies all non-associated properties from the old object, but all associated properties remain unset.
infoFor ORM entities, special handling is performed for one-to-one and many-to-one properties based directly on foreign keys. These are set to associated objects with only ids or null.
- Java
- Kotlin
Book lonelyBook = ImmutableObjects.toLonely(book);
val lonelyBook = toLonely(book)
toIdOnly
-
Purpose: Create a new object based on an existing object. The new object only copies the id property from the old object.
-
Precondition: The object type is annotated with
@Entity
and is an ORM entity with an id
- Java
- Kotlin
Book lonelyBook = ImmutableObjects.toIdOnly(book);
val lonelyBook = toIdOnly(book)
Or
- Java
- Kotlin
List<Book> lonelyBooks = ImmutableObjects.toIdOnly(books);
val lonelyBooks = toIdOnly(books)
fromString
- Purpose: Shortcut for JSON deserialization
- Java
- Kotlin
Book book = ImmutableObjects.fromObject(
Book.class,
"{\"id\":1,\"name\":\"Learning GraphQL\",\"authorIds\":[2,1]}"
);
val book = fromString(
Book::class,
"""{"id":1,"name":"Learning GraphQL","authorIds":[2,1]}"""
)
Operating on Mutable Drafts
set
- Purpose: Dynamically set Draft properties
- Java
- Kotlin
Book newBook = Immutables.createBook(book, draft -> {
DraftObjects.set(draft, BookProps.AUTHOR_IDS, Arrays.asList(1L, 3L));
});
val newBook = Book(book) {
set(draft, Book::authorIds, listOf(1L, 3L))
}
unload
- Purpose: Unload a Draft property, i.e. mark a property as unloaded
- Java
- Kotlin
Book newBook = Immutables.createBook(book, draft -> {
DraftObjects.unload(draft, BookProps.AUTHOR_IDS);
});
val newBook = Book(book) {
unload(draft, Book::authorIds)
}
show
- Purpose: Show a property
- Java
- Kotlin
Book newBook = Immutables.createBook(book, draft -> {
DraftObjects.show(draft, BookProps.AUTHOR_IDS);
});
val newBook = Book(book) {
show(draft, Book::authorIds)
}
hide
- Purpose: Hide a property
- Java
- Kotlin
Book newBook = Immutables.createBook(book, draft -> {
DraftObjects.hide(draft, BookProps.AUTHOR_IDS);
});
val newBook = Book(book) {
hide(draft, Book::authorIds)
}