If there is an odd number of items in the dataset, the one below
* the 50% mark will be returned. The true mathmatical mean, therefore
* would be:
*
* (beanArrayList.get(x).getProperty() + beanArrayList.get(x+1).getProperty() )/2
*
(o==null ? get(i)==null : o.equals(get(i)))
(if such
* an element exists).
*
* @param o element to be removed from this ArrayList, if present.
* @return true if the ArrayList contained the specified element.
* @since 1.2
*/
public boolean remove(Object o) {
boolean retValue;
T[] old = null;
if((this.indexPropertyName != null)&&(this.changes != null)) {
old = this.toTypedArray();
}
retValue = super.remove(o);
if(retValue&&(old != null)) {
changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray());
}
return retValue;
}
/**
* Removes the element at the specified position in this ArrayList.
* shifts any subsequent elements to the left (subtracts one from their
* indices). Returns the element that was removed from the ArrayList.
*
* @return element that was removed
* @since 1.2
* @param index the index of the element to removed.
*/
public T remove(int index) {
T retValue;
T[] old = null;
if((this.indexPropertyName != null)&&(this.changes != null)) {
old = this.toTypedArray();
}
retValue = super.remove(index);
if((retValue != null)&&(old != null)) {
changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray());
}
return retValue;
}
/**
* Removes from this ArrayList all of its elements that are contained in the
* specified Collection.
*
* @return true if this ArrayList changed as a result of the call.
* @since 1.2
* @param c a collection of elements to be removed from the ArrayList
*/
public boolean removeAll(Collection > c) {
boolean retValue;
T[] old = null;
if((this.indexPropertyName != null)&&(this.changes != null)) {
old = this.toTypedArray();
}
retValue = super.removeAll(c);
if(retValue&&(old != null)) {
changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray());
}
return retValue;
}
/**
* Removes all components from this ArrayList and sets its size to zero.* * This method is identical in functionality to the clear method * (which is part of the List interface). * * @see #clear * @see List */ public void removeAllElements() { T[] old = null; if((this.indexPropertyName != null)&&(this.changes != null)) { old = this.toTypedArray(); } super.clear(); if(old != null) { changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray()); } } /** * Removes the first (lowest-indexed) occurrence of the argument * from this ArrayList. If the object is found in this ArrayList, each * component in the ArrayList with an index greater or equal to the * object's index is shifted downward to have an index one smaller * than the value it had previously.
*
* This method is identical in functionality to the remove(Object)
* method (which is part of the List interface).
*
* @param obj the component to be removed.
* @return true
if the argument was a component of this
* ArrayList; false
otherwise.
* @see List#remove(Object)
* @see List
*/
public boolean removeElement(Object obj) {
boolean retValue;
T[] old = null;
if((this.indexPropertyName != null)&&(this.changes != null)) {
old = this.toTypedArray();
}
retValue = super.remove(obj);
if(retValue&&(old != null)) {
changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray());
}
return retValue;
}
/**
* Deletes the component at the specified index. Each component in
* this ArrayList with an index greater or equal to the specified
* index
is shifted downward to have an index one
* smaller than the value it had previously. The size of this ArrayList
* is decreased by 1.
*
* The index must be a value greater than or equal to 0
* and less than the current size of the ArrayList.
* * This method is identical in functionality to the remove method * (which is part of the List interface). Note that the remove method * returns the old value that was stored at the specified position. * * @see #size() * @see #remove(int) * @see List * @param index the index of the object to remove. */ public void removeElementAt(int index) { T[] old = null; if((this.indexPropertyName != null)&&(this.changes != null)) { old = this.toTypedArray(); } super.remove(index); if(old != null) { changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray()); } } /** * Removes propertyChangeListeners to be fired when something changes in the ArrayList. * Note, the whole ArrayList is an "indexedProperty" name specified and parent delineated in * the constructor. * @param listener listener to remove */ public void removePropertyChangeListener(PropertyChangeListener listener) { changes.removePropertyChangeListener(listener); } /** * Removes propertyChangeListeners to be fired when something changes in the ArrayList. * Note, the whole ArrayList is an "indexedProperty" name specified and parent delineated in * the constructor. * @param property must match the current property name for the ArrayList or will be ignored * @param listener listener to remove */ public void removePropertyChangeListener(String property,PropertyChangeListener listener) { if((property != null)&&property.equals(this.indexPropertyName)) { changes.removePropertyChangeListener(property,listener); } } /** * Resets the contents of the ArrayList to the values provided. * @param contents Array of object to replace the current contents with */ public synchronized void resetContents(T[] contents) { if((this.indexPropertyName != null)&&(this.changes != null)) { changes.firePropertyChange(this.indexPropertyName,this.toTypedArray(),contents); } this.removeAllElements(); for(T t : contents) this.add(t); } /** * Retains only the elements in this ArrayList that are contained in the * specified Collection. In other words, removes from this ArrayList all * of its elements that are not contained in the specified Collection. * * @return true if this ArrayList changed as a result of the call. * @since 1.2 * @param c a collection of elements to be retained in this ArrayList * (all other elements are removed) */ public boolean retainAll(Collection > c) { boolean retValue; T[] old = null; if((this.indexPropertyName != null)&&(this.changes != null)) { old = this.toTypedArray(); } retValue = super.retainAll(c); if(retValue&&(old != null)) { changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray()); } return retValue; } /** * Replaces the element at the specified position in this ArrayList with the * specified element. * * @return the element previously at the specified position. * @since 1.2 * @param index index of element to replace. * @param element element to be stored at the specified position. */ public T set(int index,T element) { T retValue; retValue = super.set(index,element); if((this.indexPropertyName != null)&&(this.changes != null)) { changes.fireIndexedPropertyChange(this.indexPropertyName,index,retValue,element); } return retValue; } /** * performs a selection sort on all the beans in the ArrayList by PropertyName * *
You can use a mixture of bean classes as long as all the beans support * the same property (getName() for instance), and all have the same return * type.
* *For optimal performance, it is recommended that if you have a * mixed class set the you have them grouped with like classes together as this * will minimize reflection inspections.
* @param propertyName String value containing the property to sort * on. * @throws java.lang.IllegalAccessException Property was not accessible * @throws java.beans.IntrospectionException Couldn't introspect * @throws java.lang.reflect.InvocationTargetException Is the proper signature getProperty() ? */ public void sortOnProperty(String propertyName) throws java.lang.IllegalAccessException,java.beans.IntrospectionException,java.lang.reflect.InvocationTargetException { this.sortOnProperty(propertyName,true); } /** * performs a selection sort on all the beans in the ArrayList by * PropertyName * *You can use a mixture of bean classes as long as all the beans * support the same property (getName() for instance), and all have the * same return type, or can be compareTo()ed each other.
* *For optimal performance, it is recommended that if you have a * mixed class set the you have them grouped with like classes together * as this will minimize reflection inspections.
* @param propertyName String value containing the property to sort on. * @param ascending == sorts up if true, down if not. * @throws java.lang.IllegalAccessException reflection exception * @throws java.beans.IntrospectionException reflection exception * @throws java.lang.reflect.InvocationTargetException reflection exception */ public synchronized void sortOnProperty(String propertyName,boolean ascending) throws java.lang.IllegalAccessException,java.beans.IntrospectionException,java.lang.reflect.InvocationTargetException { T[] old = null; if((this.indexPropertyName != null)&&(this.changes != null)) { old = this.toTypedArray(); } T temp = null; String currentClass = ""; PropertyDescriptor pd = null; HashMap cache = new HashMap(); for(int i = 0; i < (this.size() - 1); i++) { for(int j = i + 1; j < this.size(); j++) { T o1 = this.get(i); if(!currentClass.equals(o1.getClass().getName())) { pd = (PropertyDescriptor)cache.get(o1.getClass().getName()); if(pd == null) { PropertyDescriptor[] pds = Introspector.getBeanInfo(o1.getClass()).getPropertyDescriptors(); boolean foundProperty = false; for(int pdi = 0; (pdi < pds.length)&&!foundProperty; pdi++) { if(pds[pdi].getName().equals(propertyName)) { pd = pds[pdi]; cache.put(o1.getClass().getName(),pd); foundProperty = true; } } } } //System.out.println( "o1: "+o1+" "+pd); //System.out.println( propertyName +" "+ (pd == null )); Comparable oc1 = (Comparable)pd.getReadMethod().invoke(o1); T o2 = this.get(j); if(!currentClass.equals(o2.getClass().getName())) { pd = (PropertyDescriptor)cache.get(o2.getClass().getName()); if(pd == null) { PropertyDescriptor[] pds = Introspector.getBeanInfo(o2.getClass()).getPropertyDescriptors(); boolean foundProperty = false; for(int pdi = 0; (pdi < pds.length)&&!foundProperty; pdi++) { if(pds[pdi].getName().equals(propertyName)) { pd = pds[pdi]; foundProperty = true; } } } } Comparable oc2 = (Comparable)pd.getReadMethod().invoke(o2); if(ascending) { if((oc1 != oc2)&&((oc2 == null)||((oc1 != null)&&(oc2 != null)&&(oc2.compareTo(oc1) < 0)))) { //swap this.setElementAt(o2,i); this.setElementAt(o1,j); } } else { if((oc1 != oc2)&&((oc1 == null)||((oc1 != null)&&(oc2 != null)&&(oc1.compareTo(oc2) < 0)))) { //swap this.setElementAt(o2,i); this.setElementAt(o1,j); } } } if(old != null) { changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray()); } } } /** * Returns an Array of the generic type associated with this ArrayList. * @return Array representation of the current ArrayList. */ public T[] toTypedArray() { return (T[])this.toArray(); } /** * Removes from this List all of the elements whose index is between * fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding * elements to the left (reduces their index). * This call shortens the ArrayList by (toIndex - fromIndex) elements. (If * toIndex==fromIndex, this operation has no effect.) * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ protected void removeRange(int fromIndex,int toIndex) { T[] old = null; if((this.indexPropertyName != null)&&(this.changes != null)) { old = this.toTypedArray(); } super.removeRange(fromIndex,toIndex); if(old != null) { changes.firePropertyChange(this.indexPropertyName,old,this.toTypedArray()); } } }