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>FilenameParenthesis</code> class. |
8 | * |
9 | * @author Eric Farng |
10 | * @version $Revision: 1.1 $ |
11 | */ |
12 | public class FilenameParenthesisIterator implements Iterator { |
13 | |
14 | /** |
15 | * iterator after the parenthesis |
16 | */ |
17 | private Iterator afterIterator = null; |
18 | /** |
19 | * iterator before the parenthesis |
20 | */ |
21 | private Iterator beforeIterator = null; |
22 | /** |
23 | * iterator between the parenthesis |
24 | */ |
25 | private Iterator middleIterator = null; |
26 | |
27 | /** |
28 | * Creates a new FilenameParenthesisIterator object. |
29 | * |
30 | * @param filenameParenthesis FilenameParenthesis object to iterate through |
31 | */ |
32 | public FilenameParenthesisIterator(final FilenameParenthesis filenameParenthesis) { |
33 | super(); |
34 | if (filenameParenthesis.getBeforeComposite() != null) { |
35 | beforeIterator = filenameParenthesis.getBeforeComposite().iterator(); |
36 | } |
37 | if (filenameParenthesis.getMiddleComposite() != null) { |
38 | middleIterator = filenameParenthesis.getMiddleComposite().iterator(); |
39 | } |
40 | if (filenameParenthesis.getAfterComposite() != null) { |
41 | afterIterator = filenameParenthesis.getAfterComposite().iterator(); |
42 | } |
43 | } |
44 | |
45 | /** |
46 | * Returns true if the iteration has more elements. (In other words, returns true if next would return an element |
47 | * rather than throwing an exception.) |
48 | * |
49 | * @return true if the iteration has more elements |
50 | */ |
51 | public boolean hasNext() { |
52 | boolean nextFlag = false; |
53 | if (beforeIterator != null) { |
54 | nextFlag = beforeIterator.hasNext(); |
55 | } |
56 | if (middleIterator != null) { |
57 | nextFlag = nextFlag || middleIterator.hasNext(); |
58 | } |
59 | if (afterIterator != null) { |
60 | nextFlag = nextFlag || afterIterator.hasNext(); |
61 | } |
62 | return nextFlag; |
63 | } |
64 | |
65 | /** |
66 | * Returns the next element in the iteration. |
67 | * |
68 | * @return the next element in the iteration. |
69 | */ |
70 | public Object next() { |
71 | if (beforeIterator != null && beforeIterator.hasNext()) { |
72 | return beforeIterator.next(); |
73 | } else if (middleIterator != null && middleIterator.hasNext()) { |
74 | return middleIterator.next(); |
75 | } else if (afterIterator != null && afterIterator.hasNext()) { |
76 | return afterIterator.next(); |
77 | } |
78 | throw new NoSuchElementException("Iteration has no more elements."); |
79 | } |
80 | |
81 | /** |
82 | * This method is not supported in this iterator. |
83 | * |
84 | * @throws UnsupportedOperationException This method is not supported in this iterator |
85 | */ |
86 | public void remove() { |
87 | //todo Implement this java.util.Iterator method |
88 | throw new UnsupportedOperationException("Method remove() not yet implemented."); |
89 | } |
90 | } |