}
@Override
public ChildReference getChild( NodeKey key,
Context context ) {
ChildReference ref = childReferencesByKey.get(key);
if (ref == null) {
// Not in our list, so check the context for changes ...
if (context != null) {
Changes changes = context.changes();
if (changes != null) {
ref = changes.inserted(key);
if (ref != null) {
// The requested node was inserted, so figure out the SNS index.
// Unfortunately, this would require iteration (to figure out the indexes), so the
// easiest/fastest way is (surprisingly) to just iterate ...
Iterator<ChildReference> iter = iterator(context, ref.getName());
while (iter.hasNext()) {
ChildReference child = iter.next();
if (child.getKey().equals(key)) return child;
}
// Shouldn't really happen ...
}
}
// Not in list and no changes, so return ...
}
} else {
// It's in our list, but if there are changes we need to use the context ...
if (context != null) {
Changes changes = context.changes();
if (changes != null) {
boolean renamed = false;
if (changes.isRenamed(ref)) {
// The node was renamed, so get the new name ...
Name newName = changes.renamed(key);
ref = ref.with(newName, 1);
renamed = true;
}
Iterator<ChildInsertions> insertions = changes.insertions(ref.getName());
if (insertions != null || renamed) {
// We're looking for a node that was not inserted but has the same name as those that are
// (perhaps have renaming). As painful as it is, the easiest and most-efficient way to get
// this is to iterate ...
Iterator<ChildReference> iter = iterator(context, ref.getName());
while (iter.hasNext()) {
ChildReference child = iter.next();
if (child.getKey().equals(key)) return child;
}
}
// check if the node was removed only at the end to that insertions before can be processed first
if (changes.isRemoved(ref)) {