📄 attributelist.java
字号:
}
throw exception;
}
return true;
}
/**
* Clear the current list.
*/
public void clear() {
if (elementData != null) {
for (int i = 0; i < size; i++) {
Attribute attribute = elementData[i];
attribute.setParent(null);
}
elementData = null;
size = 0;
}
modCount++;
}
/**
* Clear the current list and set it to the contents
* of the <code>Collection</code>.
* object.
*
* @param collection The collection to use.
*/
void clearAndSet(Collection collection) {
Attribute[] 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++) {
Attribute attribute = old[i];
attribute.setParent(null);
}
}
modCount++;
}
/**
* Increases the capacity of this <code>AttributeList</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.
*/
private void ensureCapacity(int minCapacity) {
if (elementData == null) {
elementData = new Attribute[Math.max(minCapacity, INITIAL_ARRAY_SIZE)];
}
else {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Attribute oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
elementData = new Attribute[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 the <code>Attribute</code> with the
* given name and <code>Namespace</code>.
*
* @param name name of attribute to return
* @param namespace <code>Namespace</code> to match
* @return the <code>Attribute</code>, or null if one doesn't exist.
*/
Object get(String name, Namespace namespace) {
int index = indexOf(name, namespace);
if (index < 0) {
return null;
}
return elementData[index];
}
/**
* Return index of the <code>Attribute</code> with the
* given name and uri.
*/
int indexOf(String name, Namespace namespace) {
String uri = namespace.getURI();
if (elementData != null) {
for (int i = 0; i < size; i++) {
Attribute old = elementData[i];
String oldURI = old.getNamespaceURI();
String oldName = old.getName();
if (oldURI.equals(uri) && oldName.equals(name)) {
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());
Attribute old = elementData[index];
old.setParent(null);
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 <code>Attribute</code> with the
* given name and <code>Namespace</code>.
*
* @param namespace <code>Namespace</code> to match
* @return the <code>true</code> if attribute was removed,
* <code>false</code> otherwise
*/
boolean remove(String name, Namespace namespace) {
int index = indexOf(name, namespace);
if (index < 0) {
return false;
}
remove(index);
return true;
}
/**
* 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 (obj instanceof Attribute) {
Attribute attribute = (Attribute) obj;
int duplicate = indexOfDuplicate(attribute);
if ((duplicate >= 0) && (duplicate != index)) {
throw new IllegalAddException("Cannot set duplicate attribute");
}
return set(index, attribute);
}
else if (obj == null) {
throw new IllegalAddException("Cannot add null attribute");
}
else {
throw new IllegalAddException("Class " +
obj.getClass().getName() +
" is not an attribute");
}
}
/**
* Set the object at the specified location to the supplied
* object. Note: does not check for duplicate attributes.
*
* @param index The location to set the value to.
* @param attribute The attribute to set.
* @return The object which was replaced.
* throws IndexOutOfBoundsException if index < 0 || index >= size()
*/
Object set(int index, Attribute attribute) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
if (attribute.getParent() != null) {
throw new IllegalAddException(
"The attribute already has an existing parent \"" +
attribute.getParent().getQualifiedName() + "\"");
}
String reason = Verifier.checkNamespaceCollision(attribute, parent);
if (reason != null) {
throw new IllegalAddException(parent, attribute, reason);
}
Attribute old = (Attribute) elementData[index];
old.setParent(null);
elementData[index] = attribute;
attribute.setParent(parent);
return old;
}
/**
* Return index of attribute with same name and Namespace, or
* -1 if one doesn't exist
*/
private int indexOfDuplicate(Attribute attribute) {
int duplicate = -1;
String name = attribute.getName();
Namespace namespace = attribute.getNamespace();
duplicate = indexOf(name, namespace);
return duplicate;
}
/**
* 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>
*/
public String toString() {
return super.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -