Avoid multiple unnecessary calls to numberOfNodes() when adding child nodes

and synchronize adding, removing to make sure we never use an out-of-bound index.
This commit is contained in:
hns 2004-04-28 18:10:41 +00:00
parent a410c358e5
commit 833ca87ae4

View file

@ -791,7 +791,7 @@ public final class Node implements INode, Serializable {
* INode-related * INode-related
*/ */
public INode addNode(INode elem) { public INode addNode(INode elem) {
return addNode(elem, numberOfNodes()); return addNode(elem, -1);
} }
/** /**
@ -866,22 +866,31 @@ public final class Node implements INode, Serializable {
} }
} }
if ((where < 0) || (where > numberOfNodes())) {
where = numberOfNodes();
}
NodeHandle nhandle = node.getHandle(); NodeHandle nhandle = node.getHandle();
if ((subnodes != null) && subnodes.contains(nhandle)) { if ((subnodes != null) && subnodes.contains(nhandle)) {
// Node is already subnode of this - just move to new position // Node is already subnode of this - just move to new position
subnodes.remove(nhandle); synchronized (subnodes) {
where = Math.min(where, numberOfNodes()); subnodes.remove(nhandle);
subnodes.add(where, nhandle); // check if index is out of bounds when adding
if (where < 0 || where > subnodes.size()) {
subnodes.add(nhandle);
} else {
subnodes.add(where, nhandle);
}
}
} else { } else {
if (subnodes == null) { if (subnodes == null) {
subnodes = new ExternalizableVector(); subnodes = new ExternalizableVector();
} }
subnodes.add(where, nhandle); synchronized (subnodes) {
// check if index is out of bounds when adding
if (where < 0 || where > subnodes.size()) {
subnodes.add(nhandle);
} else {
subnodes.add(where, nhandle);
}
}
// check if subnode accessname is set // check if subnode accessname is set
if ((dbmap != null) && (node.dbmap != null)) { if ((dbmap != null) && (node.dbmap != null)) {
@ -941,7 +950,7 @@ public final class Node implements INode, Serializable {
*/ */
public INode createNode() { public INode createNode() {
// create new node at end of subnode array // create new node at end of subnode array
return createNode(null, numberOfNodes()); return createNode(null, -1);
} }
/** /**
@ -964,7 +973,7 @@ public final class Node implements INode, Serializable {
*/ */
public INode createNode(String nm) { public INode createNode(String nm) {
// parameter where is ignored if nm != null so we try to avoid calling numberOfNodes() // parameter where is ignored if nm != null so we try to avoid calling numberOfNodes()
return createNode(nm, (nm == null) ? numberOfNodes() : 0); return createNode(nm, -1);
} }
/** /**
@ -976,7 +985,7 @@ public final class Node implements INode, Serializable {
* @return ... * @return ...
*/ */
public INode createNode(String nm, int where) { public INode createNode(String nm, int where) {
checkWriteLock(); // checkWriteLock();
boolean anon = false; boolean anon = false;
@ -1245,7 +1254,9 @@ public final class Node implements INode, Serializable {
boolean removed = false; boolean removed = false;
if (subnodes != null) { if (subnodes != null) {
removed = subnodes.remove(node.getHandle()); synchronized (subnodes) {
removed = subnodes.remove(node.getHandle());
}
} }
if (removed) if (removed)