Package org.apache.ojb.odmg.collections

Source Code of org.apache.ojb.odmg.collections.DListIterator_2

package org.apache.ojb.odmg.collections;

/* Copyright 2003-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.apache.ojb.odmg.TransactionImpl;
import org.apache.ojb.odmg.TxManagerFactory;
import org.odmg.Transaction;

import java.util.ListIterator;

/**
* Insert the type's description here.
* Creation date: (09.02.2001 15:49:39)
* @author Thomas Mahler
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: DListIterator_2.java,v 1.3 2004/04/04 23:53:39 brianm Exp $
*/
class DListIterator_2 implements ListIterator
{
    protected ListIterator iter;
    private DListImpl_2 dlist;
    private DListEntry_2 currentEntry = null;

    /**
     * DListIterator constructor comment.
     */
    public DListIterator_2(DListImpl_2 list)
    {
        this.dlist = list;
        this.iter = list.getElements().listIterator();
    }

    /**
     * DListIterator constructor comment.
     */
    public DListIterator_2(DListImpl_2 list, int index)
    {
        this.dlist = list;
        this.iter = list.getElements().listIterator(index);
    }

    /**
     * Inserts the specified element into the list (optional operation).  The
     * element is inserted immediately before the next element that would be
     * returned by <tt>next</tt>, if any, and after the next element that
     * would be returned by <tt>previous</tt>, if any.  (If the list contains
     * no elements, the new element becomes the sole element on the list.)
     * The new element is inserted before the implicit cursor: a subsequent
     * call to <tt>next</tt> would be unaffected, and a subsequent call to
     * <tt>previous</tt> would return the new element.  (This call increases
     * by one the value that would be returned by a call to <tt>nextIndex</tt>
     * or <tt>previousIndex</tt>.)
     *
     * @exception UnsupportedOperationException if the <tt>add</tt> method is
     * not supported by this list iterator.
     *
     * @exception ClassCastException if the class of the specified element
     * prevents it from being added to this Set.
     *
     * @exception IllegalArgumentException if some aspect of this element
     * prevents it from being added to this Collection.
     */
    public void add(Object obj)
    {
        DListEntry_2 entry = new DListEntry_2(this.dlist, obj);
        entry.setPosition(this.nextIndex() - 1);
        iter.add(entry);

        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
        if (tx != null)
        {
            tx.lock(entry, Transaction.WRITE);
            entry.prepareForPersistency(tx.getBroker());
        }
    }

    /**
     * Returns <tt>true</tt> if the iteration has more elements. (In other
     * words, returns <tt>true</tt> if <tt>next</tt> would return an element
     * rather than throwing an exception.)
     *
     * @return <tt>true</tt> if the iterator has more elements.
     */
    public boolean hasNext()
    {
        return iter.hasNext();
    }

    /**
     * Returns <tt>true</tt> if this list iterator has more elements when
     * traversing the list in the reverse direction.  (In other words, returns
     * <tt>true</tt> if <tt>previous</tt> would return an element rather than
     * throwing an exception.)
     *
     * @return <tt>true</tt> if the list iterator has more elements when
     * traversing the list in the reverse direction.
     */
    public boolean hasPrevious()
    {
        return iter.hasPrevious();
    }

    /**
     * Returns the next element in the interation.
     *
     * @return the next element in the interation.
     * @exception NoSuchElementException iteration has no more elements.
     */
    public Object next()
    {
        currentEntry = ((DListEntry_2) iter.next());
        return currentEntry.getRealSubject();
    }

    /**
     * Returns the index of the element that would be returned by a subsequent
     * call to <tt>next</tt>. (Returns list size if the list iterator is at the
     * end of the list.)
     *
     * @return the index of the element that would be returned by a subsequent
     * call to <tt>next</tt>, or list size if list iterator is at end
     * of list.
     */
    public int nextIndex()
    {
        return iter.nextIndex();
    }

    /**
     * Returns the previous element in the list.  This method may be called
     * repeatedly to iterate through the list backwards, or intermixed with
     * calls to <tt>next</tt> to go back and forth.  (Note that alternating
     * calls to <tt>next</tt> and <tt>previous</tt> will return the same
     * element repeatedly.)
     *
     * @return the previous element in the list.
     *
     * @exception NoSuchElementException if the iteration has no previous
     * element.
     */
    public Object previous()
    {
        currentEntry = ((DListEntry_2) iter.previous());
        return currentEntry.getRealSubject();
    }

    /**
     * Returns the index of the element that would be returned by a subsequent
     * call to <tt>previous</tt>. (Returns -1 if the list iterator is at the
     * beginning of the list.)
     *
     * @return the index of the element that would be returned by a subsequent
     * call to <tt>previous</tt>, or -1 if list iterator is at
     * beginning of list.
     */
    public int previousIndex()
    {
        return iter.previousIndex();
    }

    /**
     *
     * Removes from the underlying collection the last element returned by the
     * iterator (optional operation).  This method can be called only once per
     * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
     * the underlying collection is modified while the iteration is in
     * progress in any way other than by calling this method.
     *
     * @exception UnsupportedOperationException if the <tt>remove</tt>
     * operation is not supported by this Iterator.
     *
     * @exception IllegalStateException if the <tt>next</tt> method has not
     * yet been called, or the <tt>remove</tt> method has already
     * been called after the last call to the <tt>next</tt>
     * method.
     */
    public void remove()
    {
        iter.remove();
        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
        if (tx != null)
        {
            tx.markDelete(currentEntry);
        }
        currentEntry = null;
    }

    /**
     * Replaces the last element returned by <tt>next</tt> or
     * <tt>previous</tt> with the specified element (optional operation).
     * This call can be made only if neither <tt>ListIterator.remove</tt> nor
     * <tt>ListIterator.add</tt> have been called after the last call to
     * <tt>next</tt> or <tt>previous</tt>.
     *
     * @exception UnsupportedOperationException if the <tt>set</tt> operation
     * is not supported by this list iterator.
     * @exception ClassCastException if the class of the specified element
     * prevents it from being added to this list.
     * @exception IllegalArgumentException if some aspect of the specified
     * element prevents it from being added to this list.
     * @exception IllegalStateException if neither <tt>next</tt> nor
     * <tt>previous</tt> have been called, or <tt>remove</tt> or
     * <tt>add</tt> have been called after the last call to
     * <tt>next</tt> or <tt>previous</tt>.
     */
    public void set(Object o)
    {
        throw new UnsupportedOperationException("Method is not supported");
        // currentEntry.setRealSubject(o);
    }

}
TOP

Related Classes of org.apache.ojb.odmg.collections.DListIterator_2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.