📄 contentlist.java
字号:
*
* @param collection The collection to use.
*/
void clearAndSet(Collection collection) {
Content[] old = elementData;
int oldSize = size;
elementData = null;
size = 0;
if ((collection != null) && (collection.size() != 0)) {
ensureCapacity(collection.size());
try {
addAll(0, collection);
}
catch (RuntimeException exception) {
elementData = old;
size = oldSize;
throw exception;
}
}
if (old != null) {
for (int i = 0; i < oldSize; i++) {
removeParent(old[i]);
}
}
modCount++;
}
/**
* Increases the capacity of this <code>ContentList</code> instance,
* if necessary, to ensure that it can hold at least the number of
* items specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity.
*/
void ensureCapacity(int minCapacity) {
if( elementData==null ) {
elementData = new Content[Math.max(minCapacity, INITIAL_ARRAY_SIZE)];
} else {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
elementData = new Content[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, size);
}
}
}
/**
* Return the object at the specified offset.
*
* @param index The offset of the object.
* @return The Object which was returned.
*/
public Object get(int index) {
if (index<0 || index>=size) {
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
}
return elementData[index];
}
/**
* Return a view of this list based on the given filter.
*
* @param filter <code>Filter</code> for this view.
* @return a list representing the rules of the <code>Filter</code>.
*/
List getView(Filter filter) {
return new FilterList(filter);
}
/**
* Return the index of the first Element in the list. If the parent
* is a <code>Document</code> then the element is the root element.
* If the list contains no Elements, it returns -1.
*
* @return index of first element, or -1 if one doesn't exist
*/
int indexOfFirstElement() {
if( elementData!=null ) {
for (int i = 0; i < size; i++) {
if (elementData[i] instanceof Element) {
return i;
}
}
}
return -1;
}
/**
* Return the index of the DocType element in the list. If the list contains
* no DocType, it returns -1.
*
* @return index of the DocType, or -1 if it doesn't
* exist
*/
int indexOfDocType() {
if (elementData != null) {
for (int i = 0; i < size; i++) {
if (elementData[i] instanceof DocType) {
return i;
}
}
}
return -1;
}
/**
* Remove the object at the specified offset.
*
* @param index The offset of the object.
* @return The Object which was removed.
*/
public Object remove(int index) {
if (index<0 || index>=size)
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
Content old = elementData[index];
removeParent(old);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,numMoved);
elementData[--size] = null; // Let gc do its work
modCount++;
return old;
}
/** Remove the parent of a Object */
private static void removeParent(Content c) {
c.setParent(null);
}
/**
* Set the object at the specified location to the supplied
* object.
*
* @param index The location to set the value to.
* @param obj The location to set the value to.
* @return The object which was replaced.
* throws IndexOutOfBoundsException if index < 0 || index >= size()
*/
public Object set(int index, Object obj) {
if (index<0 || index>=size)
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
if ((obj instanceof Element) && (parent instanceof Document)) {
int root = indexOfFirstElement();
if ((root >= 0) && (root != index)) {
throw new IllegalAddException(
"Cannot add a second root element, only one is allowed");
}
}
if ((obj instanceof DocType) && (parent instanceof Document)) {
int docTypeIndex = indexOfDocType();
if ((docTypeIndex >= 0) && (docTypeIndex != index)) {
throw new IllegalAddException(
"Cannot add a second doctype, only one is allowed");
}
}
Object old = remove(index);
try {
add(index, obj);
}
catch (RuntimeException exception) {
add(index, old);
throw exception;
}
return old;
}
/**
* Return the number of items in this list
*
* @return The number of items in this list.
*/
public int size() {
return size;
}
/**
* Return this list as a <code>String</code>
*
* @return The number of items in this list.
*/
public String toString() {
return super.toString();
}
/** Give access of ContentList.modCount to FilterList */
private int getModCount() {
return modCount;
}
/* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
/**
* <code>FilterList</code> represents legal JDOM content, including content
* for <code>Document</code>s or <code>Element</code>s.
*/
class FilterList extends AbstractList implements java.io.Serializable {
/** The Filter */
Filter filter;
/** Current number of items in this view */
int count = 0;
/** Expected modCount in our backing list */
int expected = -1;
// Implementation Note: Directly after size() is called, expected
// is sync'd with ContentList.modCount and count provides
// the true size of this view. Before the first call to
// size() or if the backing list is modified outside this
// FilterList, both might contain bogus values and should
// not be used without first calling size();
/**
* Create a new instance of the FilterList with the specified Filter.
*/
FilterList(Filter filter) {
this.filter = filter;
}
/**
* Inserts the specified object at the specified position in this list.
* Shifts the object currently at that position (if any) and any
* subsequent objects to the right (adds one to their indices).
*
* @param index The location to set the value to.
* @param obj The object to insert into the list.
* throws IndexOutOfBoundsException if index < 0 || index > size()
*/
public void add(int index, Object obj) {
if (filter.matches(obj)) {
int adjusted = getAdjustedIndex(index);
ContentList.this.add(adjusted, obj);
expected++;
count++;
}
else throw new IllegalAddException("Filter won't allow the " +
obj.getClass().getName() +
" '" + obj + "' to be added to the list");
}
/**
* Return the object at the specified offset.
*
* @param index The offset of the object.
* @return The Object which was returned.
*/
public Object get(int index) {
int adjusted = getAdjustedIndex(index);
return ContentList.this.get(adjusted);
}
public Iterator iterator() {
return new FilterListIterator(filter, 0);
}
public ListIterator listIterator() {
return new FilterListIterator(filter, 0);
}
public ListIterator listIterator(int index) {
return new FilterListIterator(filter, index);
}
/**
* Remove the object at the specified offset.
*
* @param index The offset of the object.
* @return The Object which was removed.
*/
public Object remove(int index) {
int adjusted = getAdjustedIndex(index);
Object old = ContentList.this.get(adjusted);
if (filter.matches(old)) {
old = ContentList.this.remove(adjusted);
expected++;
count--;
}
else {
throw new IllegalAddException("Filter won't allow the " +
(old.getClass()).getName() +
" '" + old + "' (index " + index +
") to be removed");
}
return old;
}
/**
* Set the object at the specified location to the supplied
* object.
*
* @param index The location to set the value to.
* @param obj The location to set the value to.
* @return The object which was replaced.
* throws IndexOutOfBoundsException if index < 0 || index >= size()
*/
public Object set(int index, Object obj) {
Object old = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -