Permalink
Browse files

Fixes #91 - remove double calls to indexOfDuplicate(Attribute)

  • Loading branch information...
1 parent 2dfdd9b commit 4085484d611c098a5ba6d784cd5ed54e6bc49999 @rolfl rolfl committed Sep 1, 2012
Showing with 21 additions and 4 deletions.
  1. +21 −4 core/src/java/org/jdom2/AttributeList.java
@@ -125,12 +125,29 @@ final void uncheckedAddAttribute(final Attribute a) {
*/
@Override
public boolean add(final Attribute attribute) {
+ if (attribute.getParent() != null) {
+ throw new IllegalAddException(
+ "The attribute already has an existing parent \""
+ + attribute.getParent().getQualifiedName() + "\"");
+ }
+
+ if (Verifier.checkNamespaceCollision(attribute, parent) != null) {
+ throw new IllegalAddException(parent, attribute,
+ Verifier.checkNamespaceCollision(attribute, parent));
+ }
+
+ // returns -1 if not exist
final int duplicate = indexOfDuplicate(attribute);
if (duplicate < 0) {
- add(size(), attribute);
- }
- else {
- set(duplicate, attribute);
+ attribute.setParent(parent);
+ ensureCapacity(size + 1);
+ attributeData[size++] = attribute;
+ modCount++;
+ } else {
+ final Attribute old = attributeData[duplicate];
+ old.setParent(null);
+ attributeData[duplicate] = attribute;
+ attribute.setParent(parent);
}
return true;
}

0 comments on commit 4085484

Please sign in to comment.