3 import java
.util
.ArrayList
;
4 import java
.util
.HashSet
;
5 import java
.util
.Iterator
;
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
);
20 source
.registerListener(this);
23 public boolean isRoot() {
27 public String
name() {
31 public Collection
<Track
> parent() {
35 public synchronized int 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
;
53 for (Track t
: available
) {
54 if (i
== idx
) return t
;
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
>();
71 for (Track t
: tracks
) {
72 if (songs
.contains(t
) && !available
.contains(t
)) {
79 for (Source
.Listener l
: listeners()) {
84 public synchronized void removed(Set
<?
extends Track
> tracks
) {
85 Set
<Track
> removed
= new HashSet
<Track
>();
88 for (Track t
: tracks
) {
89 if (available
.contains(t
)) {
96 for (Source
.Listener l
: listeners()) {