Added volume to client plus other fixes
[stereo.git] / MemphisDJ / src / music / UserCollection.java
blob75566cc9595b0485f78b0485b5cf5208b84c55a5
1 package music;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
9 import interfaces.collection.AbstractCollection;
10 import interfaces.collection.Collection;
11 import interfaces.collection.Source;
13 public class UserCollection extends AbstractCollection<Track> implements Source.Listener {
15 private final String name;
17 public UserCollection(String name, int id, long persistent, Source<? extends Track> source) {
18 super(id, persistent);
19 this.name = name;
20 source.registerListener(this);
23 public boolean isRoot() {
24 return false;
27 public String name() {
28 return name;
31 public Collection<Track> parent() {
32 return null;
35 public synchronized int size() {
36 return songs.size();
39 public synchronized Iterator<Track> iterator() {
40 return new ArrayList<Track>(songs).iterator();
43 // Source methods: only returns available tracks
45 public synchronized boolean hasNext() {
46 return !available.isEmpty();
49 public synchronized Track next() {
50 int size = available.size();
51 int idx = ((int)(Math.random()*size))%size;
52 int i = 0;
53 for (Track t: available) {
54 if (i == idx) return t;
55 i++;
57 return null;
60 public synchronized Iterable<? extends Track> tracks() {
61 return new ArrayList<Track>(available);
64 private Set<Track> songs = new HashSet<Track>();
65 private Set<Track> available = new HashSet<Track>();
67 public void added(Iterable<? extends Track> tracks) {
68 List<Track> added = new ArrayList<Track>();
70 synchronized (this) {
71 for (Track t: tracks) {
72 if (songs.contains(t) && !available.contains(t)) {
73 added.add(t);
74 available.add(t);
79 for (Source.Listener l: listeners()) {
80 l.added(added);
84 public synchronized void removed(Set<? extends Track> tracks) {
85 Set<Track> removed = new HashSet<Track>();
87 synchronized (this) {
88 for (Track t: tracks) {
89 if (available.contains(t)) {
90 removed.add(t);
91 available.remove(t);
96 for (Source.Listener l: listeners()) {
97 l.removed(removed);