We transform incoming data (in JSON, but it could be XML or anything) into immutable Java objects using the simplest method:
// pseudocode
Npc[] npcs;
for (jsonNPC : jsonNPCs) {
Quest[] quests;
for (jsonQuest : jsonNPC.quests) {
quests.add(new Quest(jsonQuest.get(foo), jsonQuest.get(bar), ...));
}
npcs.add(new Npc(jsonNPC.get(name), quests, ...));
}
This works fine, but it gets more complex. We now want to make quests refer to other Quest instances as their "prerequisite quests".
The problem is, a referred quest might only be parsed after the referring quest has been constructed (the input data is not ordered, nor could it be, since there may be bidirectional references between NPCs). That means we can't attach it at construction time. But because Quest is immutable, we have to.
How is such a problem usually solved?
(Note: There are no circular references. That should help, I think)