Skip to main content

jimmer-core(Old)

tip

This article is copied from the part-i of the Feature list, and the content is exactly the same.

If you have already read the first part-i of Feature list, you can skip this article and go straight to the more in-depth documentation.

Powerful immutable data model

We port the popular JavaScript project immer to Java/Kotlin. You can manipulate immutable objects naturally and intuitively the same way you manipulate mutable objects, you can have all the well-known advantages of immutable objects without any notorious overhead. This is the most powerful solution for immutable objects.

Quick view
// Step 1, create data from scratch
TreeNode treeNode = TreeNodeDraft.$.produce(root -> {
root.setName("Root").addIntoChildNodes(food -> {
food
.setName("Food")
.addIntoChildNodes(drink -> {
drink
.setName("Drink")
.addIntoChildNodes(cococola -> {
cococola.setName("Cococola");
})
.addIntoChildNodes(fanta -> {
fanta.setName("Fanta");
});
;
});
;
});
});

// Step 2, make some "changes" based on the
// existing data to create new data.
TreeNode newTreeNode = TreeNodeDraft.$.produce(
treeNode, // existing data
root -> {
root
.childNodes(false).get(0) // Food
.childNodes(false).get(0) // Drink
.childNodes(false).get(0) // Cococola
.setName("Cococola plus");
}
);

System.out.println("treeNode:" + treeNode);
System.out.println("newTreeNode:" + newTreeNode);
Output
treeNode: {
"name":"Root",
"childNodes":[
{
"name":"Food",
"childNodes":[
{
"name":"Drink",
"childNodes":[
{"name":"Cococola"},
{"name":"Fanta"}
]
}
]
}
]
}
newTreeNode: {
"name":"Root",
"childNodes":[
{
"name":"Food",
"childNodes":[
{
"name":"Drink",
"childNodes":[
{"name":"Cococola plus"},
{"name":"Fanta"}
]
}
]
}
]
}

Jimmer can be used to replace java records(or kotlin data classes) in any context where immutable data structures are preferred. We use very effective mechanisms to detect changes and eliminate unnecessary replication overhead. In general, any change of an object would create a new object reference, that is, the object is immutable in the sense of any specific reference. The unchanged parts would be shared among all versions of the data in memory to avoid naive copying and achieve the best performance.

Jimmer could help you:

  1. Detect unexpected mutation and throw appropriate errors;
  2. Throw away the tedious boilerplate code for manipulating the deep structure of immutable objects, avoid manual replication and save the overhead of redundant copy construction;
  3. Make changes to draft objects that record and trace the modifications, and create any necessary copies automatically with the original intact.

With Jimmer, you don't need to learn specialized APIs or data structures to benefit from immutability.

To support ORM, Jimmer improves the dynamic features of objects. Any property of an object is allowed to be missing.

  • Missing properties cause exceptions when accessed directly with code;
  • Missing properties are automatically ignored during Jackson serialization and would not cause exceptions.