📄 storediterator.java
字号:
if (!coll.view.recNumAccess) { throw new UnsupportedOperationException( "Record number access not supported"); } try { return hasPrevious() ? (cursor.getCurrentRecordNumber() - coll.getIndexOffset()) : (-1); } catch (Exception e) { throw StoredContainer.convertException(e); } } /** * Replaces the last element returned by next or previous with the * specified element (optional operation). * This method conforms to the {@link ListIterator#set} interface. * * @param value the new value. * * @throws UnsupportedOperationException if the collection is a {@link * StoredKeySet} (the set returned by {@link java.util.Map#keySet}), or if * duplicates are sorted since this would change the iterator position, or * 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 void set(Object value) { if (!coll.hasValues()) throw new UnsupportedOperationException(); if (!setAndRemoveAllowed) throw new IllegalStateException(); try { moveToCurrent(); cursor.putCurrent(value); } catch (Exception e) { throw StoredContainer.convertException(e); } } /** * Removes the last element that was returned by next or previous (optional * operation). * This method conforms to the {@link ListIterator#remove} interface except * that when the collection is a list and the RECNO-RENUMBER access method * is not used, list indices will not be renumbered. * * <p>Note that for the JE product, RECNO-RENUMBER databases are not * supported, and therefore list indices are never renumbered by this * method.</p> * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is read-only. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public void remove() { if (!setAndRemoveAllowed) throw new IllegalStateException(); try { moveToCurrent(); cursor.delete(); setAndRemoveAllowed = false; } catch (Exception e) { throw StoredContainer.convertException(e); } } /** * Inserts the specified element into the list or inserts a duplicate into * other types of collections (optional operation). * This method conforms to the {@link ListIterator#add} interface when * the collection is a list and the RECNO-RENUMBER access method is used. * Otherwise, this method may only be called when duplicates are allowed. * If duplicates are unsorted, the new value will be inserted in the same * manner as list elements. * If duplicates are sorted, the new value will be inserted in sort order. * * <p>Note that for the JE product, RECNO-RENUMBER databases are not * supported, and therefore this method may only be used to add * duplicates.</p> * * @param value the new value. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is indexed, or if the collection is read-only, or if * the collection is a list and the RECNO-RENUMBER access method was not * used, or if the collection is not a list and duplicates are not allowed. * * @throws IllegalStateException if the collection is empty and is not a * list with RECNO-RENUMBER access. * * @throws IllegalArgumentException if a duplicate value is being added * that already exists and duplicates are sorted. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public void add(Object value) { coll.checkIterAddAllowed(); try { OperationStatus status = OperationStatus.SUCCESS; if (toNext != 0 && toPrevious != 0) { // database is empty if (coll.view.keysRenumbered) { // recno-renumber database /* * Close cursor during append and then reopen to support * CDB restriction that append may not be called with a * cursor open; note the append will still fail if the * application has another cursor open. */ close(); status = coll.view.append(value, null, null); cursor = new DataCursor(coll.view, writeAllowed); reset(); next(); // move past new record } else { // hash/btree with duplicates throw new IllegalStateException( "Collection is empty, cannot add() duplicate"); } } else { // database is not empty boolean putBefore = false; if (coll.view.keysRenumbered) { // recno-renumber database moveToCurrent(); if (hasNext()) { status = cursor.putBefore(value); putBefore = true; } else { status = cursor.putAfter(value); } } else { // hash/btree with duplicates if (coll.areDuplicatesOrdered()) { status = cursor.putNoDupData(null, value, null, true); } else if (toNext == 0) { status = cursor.putBefore(value); putBefore = true; } else { status = cursor.putAfter(value); } } if (putBefore) { toPrevious = 0; toNext = MOVE_NEXT; } } if (status == OperationStatus.KEYEXIST) { throw new IllegalArgumentException("Duplicate value"); } else if (status != OperationStatus.SUCCESS) { throw new IllegalArgumentException("Could not insert: " + status); } setAndRemoveAllowed = false; } catch (Exception e) { throw StoredContainer.convertException(e); } } // --- end Iterator/ListIterator methods --- /** * Resets cursor to an uninitialized state. */ private void reset() { toNext = MOVE_FIRST; toPrevious = MOVE_PREV; toCurrent = 0; currentData = null; /* * Initialize cursor at beginning to avoid "initial previous == last" * behavior when cursor is uninitialized. * * FindBugs whines about us ignoring the return value from hasNext(). */ hasNext(); } /** * Returns the number of elements having the same key value as the key * value of the element last returned by next() or previous(). If no * duplicates are allowed, 1 is always returned. * This method does not exist in the standard {@link Iterator} or {@link * ListIterator} interfaces. * * @return the number of duplicates. * * @throws IllegalStateException if next() or previous() has not been * called for this iterator, or if remove() or add() were called after * the last call to next() or previous(). */ public int count() { if (!setAndRemoveAllowed) throw new IllegalStateException(); try { moveToCurrent(); return cursor.count(); } catch (Exception e) { throw StoredContainer.convertException(e); } } /** * Closes this iterator. * This method does not exist in the standard {@link Iterator} or {@link * ListIterator} interfaces. * * <p>After being closed, only the {@link #hasNext} and {@link * #hasPrevious} methods may be called and these will return false. {@link * #close()} may also be called again and will do nothing. If other * methods are called a <code>NullPointerException</code> will generally be * thrown.</p> * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public void close() { if (cursor != null) { coll.closeCursor(cursor); cursor = null; } } /** * Returns the collection associated with this iterator. * This method does not exist in the standard {@link Iterator} or {@link * ListIterator} interfaces. * * @return the collection associated with this iterator. */ public final StoredCollection getCollection() { return coll; } // --- begin BaseIterator methods --- public final ListIterator dup() { try { StoredIterator o = (StoredIterator) super.clone(); o.cursor = cursor.cloneCursor(); return o; } catch (Exception e) { throw StoredContainer.convertException(e); } } public final boolean isCurrentData(Object currentData) { return (this.currentData == currentData); } public final boolean moveToIndex(int index) { try { OperationStatus status = cursor.getSearchKey(new Integer(index), null, lockForWrite); setAndRemoveAllowed = (status == OperationStatus.SUCCESS); return setAndRemoveAllowed; } catch (Exception e) { throw StoredContainer.convertException(e); } } // --- end BaseIterator methods --- private void moveToCurrent() throws DatabaseException { if (toCurrent != 0) { move(toCurrent); toCurrent = 0; } } private OperationStatus move(int direction) throws DatabaseException { switch (direction) { case MOVE_NEXT: if (coll.iterateDuplicates()) { return cursor.getNext(lockForWrite); } else { return cursor.getNextNoDup(lockForWrite); } case MOVE_PREV: if (coll.iterateDuplicates()) { return cursor.getPrev(lockForWrite); } else { return cursor.getPrevNoDup(lockForWrite); } case MOVE_FIRST: return cursor.getFirst(lockForWrite); default: throw new IllegalArgumentException(String.valueOf(direction)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -