Ignore rules for git users
[stereo.git] / MemphisDJ / src / music / ShufflePlaylist.java
blobd219c0d758419fab5d5a8ec8e5da084252622037
1 package music;
3 import interfaces.Track;
4 import interfaces.collection.AbstractCollection;
5 import interfaces.collection.Collection;
6 import interfaces.collection.EditableSource;
7 import interfaces.collection.Source;
9 import java.util.LinkedList;
10 import java.util.List;
12 import notification.AbstractEventGenerator;
14 public class ShufflePlaylist
15 extends AbstractEventGenerator<Source.Listener>
16 implements Source<Track>, EditableSource<Track> {
18 private final PlaybackQueue queue;
19 private final int containerId;
20 private int collectionItemId;
22 public ShufflePlaylist(PlaybackQueue queue, final int id, final long pid, final String name) {
24 this.queue = queue;
25 this.containerId = id;
26 collectionItemId = id+1;
28 collection = new AbstractCollection<Track>(id, pid) {
30 public int editStatus() {
31 return Collection.EDITABLE;
34 public boolean isRoot() {
35 return false;
38 public String name() {
39 return name;
42 public Collection<? extends Track> parent() {
43 return null;
46 public int size() {
47 return shuffle.size();
50 public Source<Track> source() {
51 return shuffle;
57 private volatile LinkedList<Track> _list = new LinkedList<Track>();
59 private LinkedList<Track> getList() {
60 return _list;
63 private void setList(LinkedList<Track> list) {
64 this._list = list;
65 queue.notifyQueueChanged();
68 public void clear() {
69 setList(new LinkedList<Track>());
72 public boolean hasNext() {
73 return !getList().isEmpty();
76 public Track next() {
77 LinkedList<Track> old = getList();
78 Track next = old.peek();
79 LinkedList<Track> list = new LinkedList<Track>(old.subList(1, old.size()));
80 setList(list);
81 return next;
84 public int size() {
85 return getList().size();
88 public List<Track> tracks() {
89 return getList();
92 public void append(Track t) {
93 if (t != null) {
94 LinkedList<Track> n = new LinkedList<Track>(getList());
95 n.addLast(new CollectionTrack(t, containerId, collectionItemId++));
96 setList(n);
100 public void appendAll(java.util.Collection<? extends Track> ts) {
101 if (ts != null) {
102 LinkedList<Track> n = new LinkedList<Track>(getList());
103 for (Track t: ts) {
104 n.add(new CollectionTrack(t, containerId, this.collectionItemId++));
106 setList(n);
110 public void insertFirst(Track t) {
111 if (t != null) {
112 LinkedList<Track> n = new LinkedList<Track>(getList());
113 n.addFirst(t);
114 setList(n);
118 public void move(Track track, Track marker) {
119 if (track != null) {
120 LinkedList<Track> n = new LinkedList<Track>(getList());
121 if (marker == null) {
122 n.remove(track);
123 n.addLast(track);
125 else if (marker.equals(track));
126 else {
127 n.remove(track);
128 int i = 0;
129 for (Track t: n) {
130 if (t.equals(marker)) {
131 n.subList(0, i).add(track);
132 break;
134 i++;
136 if (i == n.size()) {
137 //didn't find element to insert before
138 n.addLast(track);
141 setList(n);
145 public void remove(Track t) {
146 LinkedList<Track> list = getList();
147 if (t != null && list.contains(t)) {
148 list = new LinkedList<Track>(list);
149 list.remove(t);
150 setList(list);
154 public void removeAll(java.util.Collection<? extends Track> coll) {
155 for (Track t: coll) {
156 remove(t);
160 public void trim(int size) {
161 if (getList().size() <= size) return;
162 LinkedList<Track> list = new LinkedList<Track>(getList().subList(0, size));
163 setList(list);
166 // Collection stuff
168 private final ShufflePlaylist shuffle = this;
169 private final Collection<Track> collection;
171 public final Collection<Track> collection() {
172 return collection;