1 | package org.farng.mp3.id3; |
2 | |
3 | import java.util.Iterator; |
4 | import java.util.NoSuchElementException; |
5 | |
6 | /** |
7 | * This is a manual iterator for an ID3v2 tag |
8 | * |
9 | * @author Eric Farng |
10 | * @version $Revision: 1.4 $ |
11 | */ |
12 | public class ID3v1Iterator implements Iterator { |
13 | |
14 | private static final int TITLE = 1; |
15 | private static final int ARTIST = 2; |
16 | private static final int ALBUM = 3; |
17 | private static final int COMMENT = 4; |
18 | private static final int YEAR = 5; |
19 | private static final int GENRE = 6; |
20 | private static final int TRACK = 7; |
21 | private ID3v1 id3v1tag; |
22 | private int lastIndex = 0; |
23 | |
24 | /** |
25 | * Creates a new ID3v1Iterator object. |
26 | */ |
27 | public ID3v1Iterator(final ID3v1 id3v1tag) { |
28 | this.id3v1tag = id3v1tag; |
29 | } |
30 | |
31 | public boolean hasNext() { |
32 | return hasNext(this.lastIndex); |
33 | } |
34 | |
35 | public Object next() { |
36 | return next(this.lastIndex); |
37 | } |
38 | |
39 | public void remove() { |
40 | switch (this.lastIndex) { |
41 | case TITLE: |
42 | this.id3v1tag.title = ""; |
43 | case ARTIST: |
44 | this.id3v1tag.artist = ""; |
45 | case ALBUM: |
46 | this.id3v1tag.album = ""; |
47 | case COMMENT: |
48 | this.id3v1tag.comment = ""; |
49 | case YEAR: |
50 | this.id3v1tag.year = ""; |
51 | case GENRE: |
52 | this.id3v1tag.genre = -1; |
53 | case TRACK: |
54 | if (this.id3v1tag instanceof ID3v1_1) { |
55 | ((ID3v1_1) this.id3v1tag).track = -1; |
56 | } |
57 | } |
58 | } |
59 | |
60 | private boolean hasNext(final int index) { |
61 | switch (index) { |
62 | case TITLE: |
63 | return (this.id3v1tag.title.length() > 0) || hasNext(index + 1); |
64 | case ARTIST: |
65 | return (this.id3v1tag.artist.length() > 0) || hasNext(index + 1); |
66 | case ALBUM: |
67 | return (this.id3v1tag.album.length() > 0) || hasNext(index + 1); |
68 | case COMMENT: |
69 | return (this.id3v1tag.comment.length() > 0) || hasNext(index + 1); |
70 | case YEAR: |
71 | return (this.id3v1tag.year.length() > 0) || hasNext(index + 1); |
72 | case GENRE: |
73 | return (this.id3v1tag.genre >= 0) || hasNext(index + 1); |
74 | case TRACK: |
75 | if (this.id3v1tag instanceof ID3v1_1) { |
76 | return (((ID3v1_1) this.id3v1tag).track >= 0) || hasNext(index + 1); |
77 | } |
78 | default: |
79 | return false; |
80 | } |
81 | } |
82 | |
83 | private Object next(final int index) { |
84 | switch (this.lastIndex) { |
85 | case 0: |
86 | return (this.id3v1tag.title.length() > 0) ? this.id3v1tag.title : next(index + 1); |
87 | case TITLE: |
88 | return (this.id3v1tag.artist.length() > 0) ? this.id3v1tag.artist : next(index + 1); |
89 | case ARTIST: |
90 | return (this.id3v1tag.album.length() > 0) ? this.id3v1tag.album : next(index + 1); |
91 | case ALBUM: |
92 | return (this.id3v1tag.comment.length() > 0) ? this.id3v1tag.comment : next(index + 1); |
93 | case COMMENT: |
94 | return (this.id3v1tag.year.length() > 0) ? this.id3v1tag.year : next(index + 1); |
95 | case YEAR: |
96 | return (this.id3v1tag.genre >= 0) ? new Byte(this.id3v1tag.genre) : next(index + 1); |
97 | case GENRE: |
98 | return (this.id3v1tag instanceof ID3v1_1 && (((ID3v1_1) this.id3v1tag).track >= 0)) ? |
99 | new Byte(((ID3v1_1) this.id3v1tag).track) : |
100 | null; |
101 | default: |
102 | throw new NoSuchElementException("Iteration has no more elements."); |
103 | } |
104 | } |
105 | } |