📄 orderedset.java
字号:
//return !contained; } } /** *** Throws an UnsupportedOperationException *** @param ndx The index *** @param c The Collection *** @return True if this set was changed *** @throws UnsupportedOperationException always **/ public boolean addAll(int ndx, Collection<? extends K> c) { // java.util.List (optional) throw new UnsupportedOperationException(); } // ------------------------------------------------------------------------ /** *** Returns true if this set contains the specified Object *** @return True if this set contains the specified Object **/ public boolean contains(Object obj) { return this.getBackingList().contains(obj); } /** *** Returns true if this set contains all items specified in the Collection *** @return True if this set contains all items specified in the Collection **/ public boolean containsAll(Collection<?> c) { return this.getBackingList().containsAll(c); } // ------------------------------------------------------------------------ /** *** Returns true if this set is equivalent to the specified Object *** @param other The other Object *** @return True if this set is equivalent to the specified Object **/ public boolean equals(Object other) { if (other instanceof OrderedSet) { OrderedSet os = (OrderedSet)other; java.util.List L1 = this.getBackingList(); java.util.List L2 = os.getBackingList(); //boolean eq = L1.containsAll(L2) && L2.containsAll(L1); boolean eq = L1.equals(L2); // same elements, same order return eq; } else { return false; } } // ------------------------------------------------------------------------ /** *** Returns the hashcode for this set *** @return The hashcode for this set **/ public int hashCode() { return this.getBackingList().hashCode(); } // ------------------------------------------------------------------------ /** *** Removes the specified object from this set *** @param obj The Object to remove *** @return True if the object was remove, false if the object didn't exist in this set **/ protected boolean _remove(Object obj) { //Print.dprintln("Removing: " + obj); if (this.getBackingList().remove(obj)) { this.notifyChangeListeners(ENTRY_REMOVED, obj); return true; } else { return false; } } /** *** Removes the specified object referenced by the specified iterator from this set *** @param obj The Object to remove *** @param i The Iterator which references the Objet to remove *** @return True **/ protected boolean _remove(Object obj, Iterator i) { //Print.dprintln("Removing: " + obj); i.remove(); this.notifyChangeListeners(ENTRY_REMOVED, obj); return true; } /** *** Throws UnsupportedOperationException *** @param ndx The object index to remove *** @return The removed Object *** @throws UnsupportedOperationException always **/ public K remove(int ndx) { // java.util.List (optional) throw new UnsupportedOperationException(); } /** *** Removes the specified object from this set *** @param obj THe Object to remove *** @return True if the object was removed **/ public boolean remove(Object obj) { return this._remove(obj); } /** *** Remove all Objects contained in the specified Collection from this set *** @param c The Collection containing the list of Objects to remove from this set *** @return True if elements were removed from this set **/ public boolean removeAll(Collection<?> c) { if (!this.hasChangeListeners()) { return this.getBackingList().removeAll(c); } else if (c == this) { if (this.size() > 0) { this.clear(); return true; } else { return false; } } else if ((c != null) && (c.size() > 0)) { boolean changed = false; for (Iterator i = c.iterator(); i.hasNext();) { if (this.remove(i.next())) { changed = true; } } return changed; } else { return false; } } /** *** Removes all Object from this set that are not reference in the specified Collection *** @param c The Collection of Objects to keep *** @return True if any Objects were removed from this list **/ public boolean retainAll(Collection<?> c) { if (!this.hasChangeListeners()) { return this.getBackingList().retainAll(c); } else if (c == this) { return false; } else if ((c != null) && (c.size() > 0)) { boolean changed = false; for (Iterator<?> i = this.getBackingList().iterator(); i.hasNext();) { Object obj = i.next(); if (!c.contains(obj)) { this._remove(obj, i); changed = true; } } return changed; } else { return false; } } /** *** Clears all Objects from this set **/ public void clear() { if (!this.hasChangeListeners()) { this.getBackingList().clear(); } else { for (Iterator i = this.getBackingList().iterator(); i.hasNext();) { Object obj = i.next(); this._remove(obj, i); } } } // ------------------------------------------------------------------------ /** *** Returns the number of elements in this set *** @return The number of elements in this set **/ public int size() { return this.getBackingList().size(); } /** *** Returns true if this set is empty *** @return True if this set is empty **/ public boolean isEmpty() { return (this.size() == 0); } // ------------------------------------------------------------------------ /** *** Returns the index of the specified Object in this set *** @param obj The Object for which the index is returned *** @return The index of the specified Object, or -1 if the Object does not *** exist in this set. **/ public int indexOf(Object obj) { // java.util.List return this.getBackingList().indexOf(obj); } /** *** Returns the last index of the specified Object in this set. Since this is a *** 'Set', any value exists at-most once, this is essentially the same as calling *** 'indexOf(obj)'. *** @param obj The Object for which the index is returned *** @return The last index of the specified Object, or -1 if the Object does not *** exist in this set. **/ public int lastIndexOf(Object obj) { // java.util.List return this.getBackingList().lastIndexOf(obj); } // ------------------------------------------------------------------------ /** *** Returns an ordered Iterator over the elements in this set. *** @return An Iterator over the elements in this set. **/ public Iterator<K> iterator() { if (!this.hasChangeListeners()) { // OK, since only 'remove' is allowed return this.getBackingList().iterator(); } else { return new Iterator<K>() { private K thisObject = null; private Iterator<K> i = OrderedSet.this.getBackingList().iterator(); public boolean hasNext() { return i.hasNext(); } public K next() { this.thisObject = i.next(); return this.thisObject; } public void remove() { OrderedSet.this._remove(this.thisObject, i); this.thisObject = null; } }; } } /** *** Returns a ListIterator over this set *** @return The ListIterator **/ public ListIterator<K> listIterator() { // java.util.List (mandatory) return this.listIterator(-1); } /** *** Returns a ListIterator over this set. *** @param ndx The starting index. *** @return The ListIterator **/ public ListIterator<K> listIterator(final int ndx) { if (!this.hasChangeListeners()) { // OK, since only 'remove' is allowed return (ndx >= 0)? this.getBackingList().listIterator(ndx) : this.getBackingList().listIterator(); } else { return new ListIterator<K>() { private K thisObject = null; private ListIterator<K> i = (ndx >= 0)? OrderedSet.this.getBackingList().listIterator(ndx) : OrderedSet.this.getBackingList().listIterator(); public boolean hasNext() { return i.hasNext(); } public boolean hasPrevious() { return i.hasPrevious(); } public K next() { this.thisObject = i.next(); return this.thisObject; } public int nextIndex() { return i.nextIndex(); } public K previous() { this.thisObject = i.previous(); return this.thisObject; } public int previousIndex() { return i.previousIndex(); } public void remove() { OrderedSet.this._remove(this.thisObject, i); this.thisObject = null; } public void add(K obj) { throw new UnsupportedOperationException(); } public void set(K obj) { throw new UnsupportedOperationException(); } }; } } // ------------------------------------------------------------------------ /** *** Thows UnsupportedOperationException *** @param fromIndex The 'from' index. *** @param toIndex The 'to' index. *** @throws UnsupportedOperationException always **/ public List<K> subList(int fromIndex, int toIndex) { // java.util.List (mandatory?) // not currently worth the effort to implement this throw new UnsupportedOperationException(); } // ------------------------------------------------------------------------ /** *** Returns an array of Object elements in this set *** @return An array of Object elements in this set **/ public Object[] toArray() { return this.getBackingList().toArray(new Object[this.size()]); } /** *** Returns an array of Object elements in this set *** @param a The array into which the elements are copied *** @return An array of Object elements in this set **/ public <K> K[] toArray(K a[]) { return this.getBackingList().toArray(a); } // ------------------------------------------------------------------------ /** *** Prints the contents of this set (for debug/testing purposes) **/ public void printContents() { int n = 0; for (Iterator i = this.iterator(); i.hasNext();) { Print.logInfo("" + (n++) + "] " + i.next()); } } // ------------------------------------------------------------------------ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -