📄 storedlist.java
字号:
closeCursor(cursor); StoredIterator.close(i); throw handleException(e, doAutoCommit); } } /** * Returns true if this list contains the specified element. * This method conforms to the {@link List#contains} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean contains(Object value) { return containsValue(value); } /** * Returns the element at the specified position in this list. * This method conforms to the {@link List#get} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public Object get(int index) { return super.get(new Long(index)); } /** * Returns the index in this list of the first occurrence of the specified * element, or -1 if this list does not contain this element. * This method conforms to the {@link List#indexOf} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public int indexOf(Object value) { return indexOf(value, true); } /** * Returns the index in this list of the last occurrence of the specified * element, or -1 if this list does not contain this element. * This method conforms to the {@link List#lastIndexOf} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public int lastIndexOf(Object value) { return indexOf(value, false); } private int indexOf(Object value, boolean findFirst) { DataCursor cursor = null; try { cursor = new DataCursor(view, false); OperationStatus status = cursor.findValue(value, findFirst); return (status == OperationStatus.SUCCESS) ? (cursor.getCurrentRecordNumber() - baseIndex) : (-1); } catch (Exception e) { throw StoredContainer.convertException(e); } finally { closeCursor(cursor); } } int getIndexOffset() { return baseIndex; } /** * Returns a list iterator of the elements in this list (in proper * sequence). * The iterator will be read-only if the collection is read-only. * This method conforms to the {@link List#listIterator()} interface. * * <p>For information on cursor stability and iterator block size, see * {@link #iterator()}.</p> * * @return a {@link ListIterator} for this collection. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. * * @see #isWriteAllowed */ public ListIterator listIterator() { return blockIterator(); } /** * Returns a list iterator of the elements in this list (in proper * sequence), starting at the specified position in this list. * The iterator will be read-only if the collection is read-only. * This method conforms to the {@link List#listIterator(int)} interface. * * <p>For information on cursor stability and iterator block size, see * {@link #iterator()}.</p> * * @return a {@link ListIterator} for this collection. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. * * @see #isWriteAllowed */ public ListIterator listIterator(int index) { BlockIterator i = blockIterator(); if (i.moveToIndex(index)) { return i; } else { throw new IndexOutOfBoundsException(String.valueOf(index)); } } /** * Removes the element at the specified position in this list (optional * operation). * This method conforms to the {@link List#remove(int)} interface. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is read-only. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public Object remove(int index) { try { Object[] oldVal = new Object[1]; removeKey(new Long(index), oldVal); return oldVal[0]; } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(e.getMessage()); } } /** * Removes the first occurrence in this list of the specified element * (optional operation). * This method conforms to the {@link List#remove(Object)} interface. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is read-only. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean remove(Object value) { return removeValue(value); } /** * Replaces the element at the specified position in this list with the * specified element (optional operation). * This method conforms to the {@link List#set} interface. * * @throws UnsupportedOperationException if the collection is indexed, or * if the collection is read-only. * * @throws IllegalArgumentException if an entity value binding is used and * the primary key of the value given is different than the existing stored * primary key. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public Object set(int index, Object value) { try { return put(new Long(index), value); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(e.getMessage()); } } /** * Returns a view of the portion of this list between the specified * fromIndex, inclusive, and toIndex, exclusive. * Note that add() and remove() may not be called for the returned sublist. * This method conforms to the {@link List#subList} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public List subList(int fromIndex, int toIndex) { if (fromIndex < 0 || fromIndex > toIndex) { throw new IndexOutOfBoundsException(String.valueOf(fromIndex)); } try { int newBaseIndex = baseIndex + fromIndex; return new StoredList( view.subView(new Long(fromIndex), true, new Long(toIndex), false, new IndexKeyBinding(newBaseIndex)), newBaseIndex); } catch (KeyRangeException e) { throw new IndexOutOfBoundsException(e.getMessage()); } catch (Exception e) { throw StoredContainer.convertException(e); } } /** * Compares the specified object with this list for equality. * A value comparison is performed by this method and the stored values * are compared rather than calling the equals() method of each element. * This method conforms to the {@link List#equals} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean equals(Object other) { if (!(other instanceof List)) return false; List otherList = (List) other; StoredIterator i1 = null; ListIterator i2 = null; try { i1 = storedIterator(); i2 = storedOrExternalListIterator(otherList); while (i1.hasNext()) { if (!i2.hasNext()) return false; if (i1.nextIndex() != i2.nextIndex()) return false; Object o1 = i1.next(); Object o2 = i2.next(); if (o1 == null) { if (o2 != null) return false; } else { if (!o1.equals(o2)) return false; } } if (i2.hasNext()) return false; return true; } finally { i1.close(); StoredIterator.close(i2); } } /** * Returns a StoredIterator if the given collection is a StoredCollection, * else returns a regular/external ListIterator. The iterator returned * should be closed with the static method StoredIterator.close(Iterator). */ final ListIterator storedOrExternalListIterator(List list) { if (list instanceof StoredCollection) { return ((StoredCollection) list).storedIterator(); } else { return list.listIterator(); } } /* * Add this in to keep FindBugs from whining at us about implementing * equals(), but not hashCode(). */ public int hashCode() { return super.hashCode(); } Object makeIteratorData(BaseIterator iterator, DatabaseEntry keyEntry, DatabaseEntry priKeyEntry, DatabaseEntry valueEntry) { return view.makeValue(priKeyEntry, valueEntry); } boolean hasValues() { return true; } private static class IndexKeyBinding extends RecordNumberBinding { private int baseIndex; private IndexKeyBinding(int baseIndex) { this.baseIndex = baseIndex; } public Object entryToObject(DatabaseEntry data) { return new Long(entryToRecordNumber(data) - baseIndex); } public void objectToEntry(Object object, DatabaseEntry data) { recordNumberToEntry(((Number) object).intValue() + baseIndex, data); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -