| 1 | package org.farng.mp3.lyrics3; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.NoSuchElementException; |
| 5 | |
| 6 | /** |
| 7 | * This is a manual iterator for a Lyrics3v1 tag |
| 8 | * |
| 9 | * @author Eric Farng |
| 10 | * @version $Revision: 1.4 $ |
| 11 | */ |
| 12 | public class Lyrics3v1Iterator implements Iterator { |
| 13 | |
| 14 | private Lyrics3v1 tag = null; |
| 15 | private int lastIndex = 0; |
| 16 | private int removeIndex = 0; |
| 17 | |
| 18 | /** |
| 19 | * Creates a new Lyrics3v1Iterator object. |
| 20 | */ |
| 21 | public Lyrics3v1Iterator(final Lyrics3v1 lyrics3v1Tag) { |
| 22 | this.tag = lyrics3v1Tag; |
| 23 | } |
| 24 | |
| 25 | public boolean hasNext() { |
| 26 | return !((this.tag.getLyric().indexOf('\n', this.lastIndex) < 0) && |
| 27 | (this.lastIndex > this.tag.getLyric().length())); |
| 28 | } |
| 29 | |
| 30 | public Object next() { |
| 31 | final int nextIndex = this.tag.getLyric().indexOf('\n', this.lastIndex); |
| 32 | this.removeIndex = this.lastIndex; |
| 33 | final String line; |
| 34 | if (this.lastIndex >= 0) { |
| 35 | if (nextIndex >= 0) { |
| 36 | line = this.tag.getLyric().substring(this.lastIndex, nextIndex); |
| 37 | } else { |
| 38 | line = this.tag.getLyric().substring(this.lastIndex); |
| 39 | } |
| 40 | this.lastIndex = nextIndex; |
| 41 | } else { |
| 42 | throw new NoSuchElementException("Iteration has no more elements."); |
| 43 | } |
| 44 | return line; |
| 45 | } |
| 46 | |
| 47 | public void remove() { |
| 48 | final String lyric = this.tag.getLyric().substring(0, this.removeIndex) + |
| 49 | this.tag.getLyric().substring(this.lastIndex); |
| 50 | this.tag.setLyric(lyric); |
| 51 | } |
| 52 | } |