| 1 | package org.farng.mp3.filename; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.NoSuchElementException; |
| 5 | |
| 6 | /** |
| 7 | * This class is an iterator for the <code>FilenameDelimiter</code> class. |
| 8 | * |
| 9 | * @author Eric Farng |
| 10 | * @version $Revision: 1.1 $ |
| 11 | */ |
| 12 | public class FilenameDelimiterIterator implements Iterator { |
| 13 | |
| 14 | /** |
| 15 | * iterator for the composite before the delimiter |
| 16 | */ |
| 17 | private Iterator afterIterator = null; |
| 18 | /** |
| 19 | * iterator for the composite after the delimiter |
| 20 | */ |
| 21 | private Iterator beforeIterator = null; |
| 22 | |
| 23 | /** |
| 24 | * Creates a new FilenameDelimiterIterator object. |
| 25 | * |
| 26 | * @param filenameDelimiter FilenameDelimiter to create an interator from. |
| 27 | */ |
| 28 | public FilenameDelimiterIterator(final FilenameDelimiter filenameDelimiter) { |
| 29 | super(); |
| 30 | if (filenameDelimiter.getBeforeComposite() != null) { |
| 31 | beforeIterator = filenameDelimiter.getBeforeComposite().iterator(); |
| 32 | } |
| 33 | if (filenameDelimiter.getAfterComposite() != null) { |
| 34 | afterIterator = filenameDelimiter.getAfterComposite().iterator(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns true if the iteration has more elements. (In other words, returns true if next would return an element |
| 40 | * rather than throwing an exception.) |
| 41 | * |
| 42 | * @return true if the iteration has more elements |
| 43 | */ |
| 44 | public boolean hasNext() { |
| 45 | boolean nextFlag = false; |
| 46 | if (beforeIterator != null) { |
| 47 | nextFlag = beforeIterator.hasNext(); |
| 48 | } |
| 49 | if (afterIterator != null) { |
| 50 | nextFlag = nextFlag || afterIterator.hasNext(); |
| 51 | } |
| 52 | return nextFlag; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the next element in the iteration. |
| 57 | * |
| 58 | * @return the next element in the iteration. |
| 59 | */ |
| 60 | public Object next() { |
| 61 | if (beforeIterator != null && beforeIterator.hasNext()) { |
| 62 | return beforeIterator.next(); |
| 63 | } else if (afterIterator != null && afterIterator.hasNext()) { |
| 64 | return afterIterator.next(); |
| 65 | } else { |
| 66 | throw new NoSuchElementException("Iteration has no more elements."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * This method is not supported in this iterator. |
| 72 | * |
| 73 | * @throws UnsupportedOperationException This method is not supported in this iterator |
| 74 | */ |
| 75 | public void remove() { |
| 76 | //todo Implement this java.util.Iterator method |
| 77 | throw new UnsupportedOperationException("Method remove() not yet implemented."); |
| 78 | } |
| 79 | } |