1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.IllegalAddException; 13 import org.dom4j.Node; 14 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 28 public class BackedList extends ArrayList { 29 30 31 private List branchContent; 32 33 34 private AbstractBranch branch; 35 36 37 public BackedList(AbstractBranch branch, List branchContent) { 38 this(branch, branchContent, branchContent.size()); 39 } 40 41 public BackedList(AbstractBranch branch, List branchContent, int capacity) { 42 super(capacity); 43 this.branch = branch; 44 this.branchContent = branchContent; 45 } 46 47 public BackedList(AbstractBranch branch, List branchContent, List initialContent) { 48 super(initialContent); 49 this.branch = branch; 50 this.branchContent = branchContent; 51 } 52 53 public boolean add(Object object) { 54 branch.addNode(asNode(object)); 55 return super.add(object); 56 } 57 58 public void add(int index, Object object) { 59 int size = branchContent.size(); 60 if (index < 0) { 61 throw new IndexOutOfBoundsException ("Index value: " + index + " is less than zero"); 62 } else if (index > size) { 63 throw new IndexOutOfBoundsException ("Index value: " + index + " cannot be greater than the size: " + size); 64 } 65 int realIndex = size; 66 if (index < realIndex) { 67 realIndex = branchContent.indexOf(get(index)); 68 } 69 if (realIndex < 0) { 70 realIndex = (index == 0) ? 0 : Integer.MAX_VALUE; 71 } 72 if (realIndex < size) { 73 branch.addNode(realIndex, asNode(object)); 74 } else { 75 branch.addNode(asNode(object)); 76 } 77 super.add(index, object); 78 } 79 80 public Object set(int index, Object object) { 81 int realIndex = branchContent.indexOf(get(index)); 82 if (realIndex < 0) { 83 realIndex = (index == 0) ? 0 : Integer.MAX_VALUE; 84 } 85 if (realIndex < branchContent.size()) { 86 branch.removeNode(asNode(get(index))); 87 branch.addNode(realIndex, asNode(object)); 88 } else { 89 branch.removeNode(asNode(get(index))); 90 branch.addNode(asNode(object)); 91 } 92 branch.childAdded(asNode(object)); 93 return super.set(index, object); 94 } 95 96 public boolean remove(Object object) { 97 branch.removeNode(asNode(object)); 98 return super.remove(object); 99 } 100 101 public Object remove(int index) { 102 Object object = super.remove(index); 103 if (object != null) { 104 branch.removeNode(asNode(object)); 105 } 106 return object; 107 } 108 109 public boolean addAll(Collection collection) { 110 ensureCapacity(size() + collection.size()); 111 112 int count = size(); 113 for (Iterator iter = collection.iterator(); iter.hasNext(); count--) { 114 add(iter.next()); 115 } 116 return count != 0; 117 } 118 119 public boolean addAll(int index, Collection collection) { 120 ensureCapacity(size() + collection.size()); 121 122 int count = size(); 123 for (Iterator iter = collection.iterator(); iter.hasNext(); count--) { 124 add(index++, iter.next()); 125 } 126 return count != 0; 127 } 128 129 public void clear() { 130 for (Iterator iter = iterator(); iter.hasNext();) { 131 Object object = iter.next(); 132 branchContent.remove(object); 133 branch.childRemoved(asNode(object)); 134 } 135 super.clear(); 136 } 137 138 141 public void addLocal(Object object) { 142 super.add(object); 143 } 144 145 protected Node asNode(Object object) { 146 if (object instanceof Node) { 147 return (Node) object; 148 } else { 149 throw new IllegalAddException("This list must contain instances of Node. Invalid type: " + object); 150 } 151 } 152 } 153 154 155 199 | Popular Tags |