/******************************************************************************* * Copyright (c) 2005, 2010 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. * * Contributors: * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.common.utility.internal.iterators; import java.util.List; import java.util.ListIterator; import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.common.utility.internal.Transformer; import org.eclipse.jpt.common.utility.internal.iterables.ListIterable; /** * A TransformationListIterator wraps another {@link ListIterator} * and transforms its results for client consumption. To use, supply a * {@link Transformer} or subclass TransformationIterator * and override the {@link #transform(Object)} method. *

* The methods {@link #set(Object)} and {@link #add(Object)} * are left unsupported in this class. * * @param input: the type of elements to be transformed * @param output: the type of elements returned by the iterator * * @see org.eclipse.jpt.common.utility.internal.iterables.TransformationListIterable */ public class TransformationListIterator implements ListIterator { private final ListIterator listIterator; private final Transformer transformer; /** * Construct an iterator with the specified list * and a disabled transformer. * Use this constructor if you want to override the * {@link #transform(Object)} method instead of building * a {@link Transformer}. */ public TransformationListIterator(List list) { this(list.listIterator()); } /** * Construct an iterator with the specified nested listed iterator * and a disabled transformer. * Use this constructor if you want to override the * {@link #transform(Object)} method instead of building * a {@link Transformer}. */ public TransformationListIterator(ListIterator listIterator) { this(listIterator, Transformer.Disabled.instance()); } /** * Construct an iterator with the specified list * and a disabled transformer. * Use this constructor if you want to override the * {@link #transform(Object)} method instead of building * a {@link Transformer}. */ public TransformationListIterator(ListIterable listIterable) { this(listIterable.iterator()); } /** * Construct an iterator with the specified list and transformer. */ public TransformationListIterator(List list, Transformer transformer) { this(list.listIterator(), transformer); } /** * Construct an iterator with the specified list and transformer. */ public TransformationListIterator(ListIterable listIterable, Transformer transformer) { this(listIterable.iterator(), transformer); } /** * Construct an iterator with the specified nested iterator * and transformer. */ public TransformationListIterator(ListIterator listIterator, Transformer transformer) { super(); this.listIterator = listIterator; this.transformer = transformer; } public boolean hasNext() { // delegate to the nested iterator return this.listIterator.hasNext(); } public E2 next() { // transform the object returned by the nested iterator before returning it return this.transform(this.listIterator.next()); } public int nextIndex() { // delegate to the nested iterator return this.listIterator.nextIndex(); } public boolean hasPrevious() { // delegate to the nested iterator return this.listIterator.hasPrevious(); } public E2 previous() { // transform the object returned by the nested iterator before returning it return this.transform(this.listIterator.previous()); } public int previousIndex() { // delegate to the nested iterator return this.listIterator.previousIndex(); } public void add(E2 o) { throw new UnsupportedOperationException(); } public void set(E2 o) { throw new UnsupportedOperationException(); } public void remove() { // delegate to the nested iterator this.listIterator.remove(); } /** * Transform the specified object and return the result. */ protected E2 transform(E1 next) { return this.transformer.transform(next); } @Override public String toString() { return StringTools.buildToStringFor(this, this.listIterator); } }