PlayList.java

package sidplay.player;

import libsidplay.config.ISidPlay2Section;
import libsidplay.sidtune.SidTune;

import static libsidplay.sidtune.SidTune.RESET;

/**
 * PlayList is a track list of songs to play. It starts with the first entry,
 * which is the marker to detect a wrap-around. The current entry is the
 * currently selected song.<BR>
 * E.g. 5 songs in a tune using start song number 3 will result in<BR>
 * [3],4,5,1,2 -&gt; first = 3, length=5, current song is within range 1..5
 *
 * @author Ken Händel
 *
 */
public class PlayList {
    /**
     * Configuration.
     */
    private final ISidPlay2Section sidplay2Section;
    /**
     * Current Tune.
     */
    private final SidTune tune;

    /**
     * First entry of the playlist.
     */
    private final int first;
    /**
     * Number of entries in the playlist.
     */
    private final int length;
    /**
     * Current entry of the playlist. It wraps around the number of entries.
     */
    private int current;

    public PlayList(final ISidPlay2Section sidplay2Section, final SidTune tune, boolean firstPlayListEntryIsOne) {
        this.sidplay2Section = sidplay2Section;
        this.tune = tune;
        if (tune != RESET) {
            this.current = tune.getInfo().getSelectedSong();
            this.length = sidplay2Section.isSingle() ? 1 : tune.getInfo().getSongs();
        } else {
            this.current = 1;
            this.length = 1;
        }
        this.first = firstPlayListEntryIsOne ? 1 : current;
    }

    public void prepare() {
        if (tune != RESET) {
            tune.getInfo().setSelectedSong(current);
            tune.prepare();
        }
    }

    /**
     * Get the currently selected playlist entry.
     *
     * @return current song number
     */
    public int getCurrent() {
        return current;
    }

    /**
     * Get the current track (playlist entry relative to the first).
     *
     * @return track number
     */
    public int getTrackNum() {
        int start = current - first + 1;
        return start < 1 ? start + length : start;
    }

    /**
     * Get a number of entries.
     *
     * @return number of songs
     */
    public int getLength() {
        return length;
    }

    /**
     * Choose the next playlist entry.
     */
    public void next() {
        current = getNext();
    }

    /**
     * Choose the previous playlist entry.
     */
    public void previous() {
        current = getPrevious();
    }

    /**
     * Choose the first playlist entry.
     */
    public void first() {
        current = first;
    }

    /**
     * Choose the last playlist entry.
     */
    public void last() {
        int last = sidplay2Section.isSingle() ? first : first - 1;
        current = last < 1 ? length : last;
    }

    /**
     * Is a previous playlist entry available?
     *
     * @return is a previous song available?
     */
    public boolean hasPrevious() {
        return current != first;
    }

    /**
     * Is a next playlist entry available?
     *
     * @return is the next song available?
     */
    public boolean hasNext() {
        return getNext() != first;
    }

    /**
     * Get the previous playlist entry.
     *
     * @return the previous song of the playlist
     */
    public int getPrevious() {
        int previous = sidplay2Section.isSingle() ? current : current - 1;
        return previous < 1 ? length : previous;
    }

    /**
     * Get the next playlist entry.
     *
     * @return the next song of the playlist
     */
    public int getNext() {
        int next = sidplay2Section.isSingle() ? current : current + 1;
        return next > length ? 1 : next;
    }

}