home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Collections example source code file (ReverseListIterator.java)

This example Commons Collections source code file (ReverseListIterator.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Collections tags/keywords

cannot, cannot, illegalstateexception, illegalstateexception, list, listiterator, object, object, resettablelistiterator, reverselistiterator, reverselistiterator, util

The Commons Collections ReverseListIterator.java source code

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.
 */
package org.apache.commons.collections.iterators;

import java.util.List;
import java.util.ListIterator;

import org.apache.commons.collections.ResettableListIterator;

/**
 * Iterates backwards through a List, starting with the last element
 * and continuing to the first. This is useful for looping around
 * a list in reverse order without needing to actually reverse the list.
 * <p>
 * The first call to <code>next() will return the last element
 * from the list, and so on. The <code>hasNext() method works
 * in concert with the <code>next() method as expected.
 * However, the <code>nextIndex() method returns the correct
 * index in the list, thus it starts high and reduces as the iteration
 * continues. The previous methods work similarly.
 *
 * @author Serge Knystautas
 * @author Stephen Colebourne
 * @since Commons Collections 3.2
 * @version $Revision: $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
 */
public class ReverseListIterator implements ResettableListIterator {

    /** The list being wrapped. */
    private final List list;
    /** The list iterator being wrapped. */
    private ListIterator iterator;
    /** Flag to indicate if updating is possible at the moment. */
    private boolean validForUpdate = true;

    /**
     * Constructor that wraps a list.
     *
     * @param list  the list to create a reversed iterator for
     * @throws NullPointerException if the list is null
     */
    public ReverseListIterator(List list) {
        super();
        this.list = list;
        iterator = list.listIterator(list.size());
    }

    //-----------------------------------------------------------------------
    /**
     * Checks whether there is another element.
     *
     * @return true if there is another element
     */
    public boolean hasNext() {
        return iterator.hasPrevious();
    }

    /**
     * Gets the next element.
     * The next element is the previous in the list.
     *
     * @return the next element in the iterator
     */
    public Object next() {
        Object obj = iterator.previous();
        validForUpdate = true;
        return obj;
    }

    /**
     * Gets the index of the next element.
     *
     * @return the index of the next element in the iterator
     */
    public int nextIndex() {
        return iterator.previousIndex();
    }

    /**
     * Checks whether there is a previous element.
     *
     * @return true if there is a previous element
     */
    public boolean hasPrevious() {
        return iterator.hasNext();
    }

    /**
     * Gets the previous element.
     * The next element is the previous in the list.
     *
     * @return the previous element in the iterator
     */
    public Object previous() {
        Object obj = iterator.next();
        validForUpdate = true;
        return obj;
    }

    /**
     * Gets the index of the previous element.
     *
     * @return the index of the previous element in the iterator
     */
    public int previousIndex() {
        return iterator.nextIndex();
    }

    /**
     * Removes the last returned element.
     *
     * @throws UnsupportedOperationException if the list is unmodifiable
     * @throws IllegalStateException if there is no element to remove
     */
    public void remove() {
        if (validForUpdate == false) {
            throw new IllegalStateException("Cannot remove from list until next() or previous() called");
        }
        iterator.remove();
    }

    /**
     * Replaces the last returned element.
     *
     * @param obj  the object to set
     * @throws UnsupportedOperationException if the list is unmodifiable
     * @throws IllegalStateException if the iterator is not in a valid state for set
     */
    public void set(Object obj) {
        if (validForUpdate == false) {
            throw new IllegalStateException("Cannot set to list until next() or previous() called");
        }
        iterator.set(obj);
    }

    /**
     * Adds a new element to the list between the next and previous elements.
     *
     * @param obj  the object to add
     * @throws UnsupportedOperationException if the list is unmodifiable
     * @throws IllegalStateException if the iterator is not in a valid state for set
     */
    public void add(Object obj) {
        // the validForUpdate flag is needed as the necessary previous()
        // method call re-enables remove and add
        if (validForUpdate == false) {
            throw new IllegalStateException("Cannot add to list until next() or previous() called");
        }
        validForUpdate = false;
        iterator.add(obj);
        iterator.previous();
    }

    /**
     * Resets the iterator back to the start (which is the
     * end of the list as this is a reversed iterator)
     */
    public void reset() {
        iterator = list.listIterator(list.size());
    }

}

Other Commons Collections examples (source code examples)

Here is a short list of links related to this Commons Collections ReverseListIterator.java source code file:

new blog posts

 

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.