📄 fastcollection.java
字号:
return true;
}
/**
* Removes from this collection all the values that are contained in the
* specified collection.
*
* @param c collection that defines which values will be removed from
* this collection.
* @return <code>true</code> if this collection changed as a result of
* the call; <code>false</code> otherwise.
*/
public boolean removeAll(final Collection/*<?>*/c) {
boolean modified = false;
// Iterates from the tail and removes the record if present in c.
for ( Record head = head(), r = tail().getPrevious(), previous; r != head; r = previous ) {
previous = r.getPrevious(); // Saves previous.
if ( c.contains( valueOf( r ) ) ) {
delete( r );
modified = true;
}
}
return modified;
}
/**
* Retains only the values in this collection that are contained in the
* specified collection.
*
* @param c collection that defines which values this set will retain.
* @return <code>true</code> if this collection changed as a result of
* the call; <code>false</code> otherwise.
*/
public boolean retainAll(final Collection/*<?>*/c) {
boolean modified = false;
// Iterates from the tail and remove the record if not present in c.
for ( Record head = head(), r = tail().getPrevious(), previous; r != head; r = previous ) {
previous = r.getPrevious(); // Saves previous.
if ( !c.contains( valueOf( r ) ) ) {
delete( r );
modified = true;
}
}
return modified;
}
/**
* Returns a new array allocated on the heap containing all of the values
* in this collection in proper sequence.
* <p> Note: To avoid heap allocation {@link #toArray(Object[])} is
* recommended.</p>
* @return <code>toArray(new Object[size()])</code>
*/
public Object[] toArray() {
return toArray( new Object[size()] );
}
/**
* Fills the specified array with the values of this collection in
* the proper sequence.
*
* <p> Note: Unlike standard Collection, this method does not try to resize
* the array using reflection (which might not be supported) if
* the array is too small. UnsupportedOperationException is raised
* if the specified array is too small for this collection.</p>
*
* @param array the array into which the values of this collection
* are to be stored.
* @return the specified array.
* @throws UnsupportedOperationException if <code>array.length < size()</code>
*/
public Object[]/* <T> T[]*/toArray(final Object[]/*T[]*/array) {
final int size = size();
if ( array.length < size ) {
throw new UnsupportedOperationException( "Destination array too small" );
}
if ( array.length > size ) {
array[size] = null; // As per Collection contract.
}
int i = 0;
final Object[] arrayView = array;
for ( Record r = head(), end = tail(); (r = r.getNext()) != end; ) {
arrayView[i++] = valueOf( r );
}
return array;
}
/**
* Returns the textual representation of this collection.
*
* @return this collection textual representation.
*/
public String toString() {
final String sep = ", ";
StringBuffer text = new StringBuffer( "[" );
for ( Record r = head(), end = tail(); (r = r.getNext()) != end; ) {
text = text.append( String.valueOf( r ) );
if ( r.getNext() != end ) {
text = text.append( sep );
}
}
return text.append( ']' ).toString();
}
/**
* Compares the specified object with this collection for equality. Returns
* <code>true</code> if and only both collection contains the same values
* regardless of the order; unless this collection is a list instance
* in which case both collection must be list with the same order.
*
* @param obj the object to be compared for equality with this collection.
* @return <code>true</code> if the specified object is equal to this
* collection; <code>false</code> otherwise.
*/
public boolean equals(final Object obj) {
if ( this instanceof List ) {
return equalsList( obj );
}
return obj == this || (obj instanceof Collection && ((Collection) obj).size() == size() && containsAll( (Collection) obj ));
}
private boolean equalsList(final Object obj) {
final FastComparator comp = this.getValueComparator();
if ( obj == this ) {
return true;
}
if ( obj instanceof List ) {
final List/*<?>*/list = (List) obj;
if ( this.size() != list.size() ) {
return false;
}
Record r1 = this.head();
final Iterator/*<?>*/i2 = list.iterator();
for ( int i = this.size(); i-- != 0; ) {
r1 = r1.getNext();
final Object o1 = this.valueOf( r1 );
final Object o2 = i2.next();
if ( !comp.areEqual( o1,
o2 ) ) {
return false;
}
}
return true;
}
return false;
}
/**
* Returns the hash code for this collection (independent from the
* collection order; unless this collection is a list instance).
*
* @return the hash code for this collection.
*/
public int hashCode() {
if ( this instanceof List ) {
return hashCodeList();
}
final FastComparator valueComp = this.getValueComparator();
int hash = 0;
for ( Record r = head(), end = tail(); (r = r.getNext()) != end; ) {
hash += valueComp.hashCodeOf( valueOf( r ) );
}
return hash;
}
private int hashCodeList() {
final FastComparator comp = this.getValueComparator();
int h = 1;
for ( Record r = head(), end = tail(); (r = r.getNext()) != end; ) {
h = 31 * h + comp.hashCodeOf( valueOf( r ) );
}
return h;
}
/**
* This interface represents the collection records which can directly be
* iterated over.
*/
public interface Record {
/**
* Returns the record before this one.
*
* @return the previous record.
*/
public Record getPrevious();
/**
* Returns the record after this one.
*
* @return the next record.
*/
public Record getNext();
}
/**
* This inner class represents an unmodifiable view over the collection.
*/
private final class Unmodifiable extends FastCollection
implements
Set,
List { // Allows to be used for unmodifiable set/list view.
/**
*
*/
private static final long serialVersionUID = -5929790997648211020L;
// Implements abstract method.
public int size() {
return FastCollection.this.size();
}
// Implements abstract method.
public Record head() {
return FastCollection.this.head();
}
// Implements abstract method.
public Record tail() {
return FastCollection.this.tail();
}
// Implements abstract method.
public Object valueOf(final Record record) {
return FastCollection.this.valueOf( record );
}
// Forwards...
public boolean contains(final Object value) {
return (FastCollection.this).contains( value );
}
// Forwards...
public boolean containsAll(final Collection c) {
return (FastCollection.this).containsAll( c );
}
// Forwards...
public FastComparator getValueComparator() {
return FastCollection.this.getValueComparator();
}
// Disallows...
public FastCollection setValueComparator(final FastComparator comparator) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
// Disallows...
public boolean add(final Object obj) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
// Disallows...
public void delete(final Record node) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
//////////////////////////////////////////
// List interface supplementary methods //
//////////////////////////////////////////
public boolean addAll(final int index,
final Collection c) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
public Object get(final int index) {
return ((List) FastCollection.this).get( index );
}
public Object set(final int index,
final Object element) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
public void add(final int index,
final Object element) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
public Object remove(final int index) {
throw new UnsupportedOperationException( "Unmodifiable" );
}
public int indexOf(final Object o) {
return ((List) FastCollection.this).indexOf( o );
}
public int lastIndexOf(final Object o) {
return ((List) FastCollection.this).lastIndexOf( o );
}
public ListIterator listIterator() {
throw new UnsupportedOperationException( "List iterator not supported for unmodifiable collection" );
}
public ListIterator listIterator(final int index) {
throw new UnsupportedOperationException( "List iterator not supported for unmodifiable collection" );
}
public List subList(final int fromIndex,
final int toIndex) {
throw new UnsupportedOperationException( "Sub-List not supported for unmodifiable collection" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -