3 import interfaces
.Album
;
4 import interfaces
.Constants
;
5 import interfaces
.Track
;
7 import java
.io
.IOException
;
8 import java
.util
.ArrayList
;
9 import java
.util
.HashMap
;
10 import java
.util
.HashSet
;
14 public class LibraryTrack
implements Track
{
17 private final long persistentId
;
18 private final Set
<Track
> backingTracks
= new HashSet
<Track
>(2);
20 private LibraryTrack(int id
, long persistantId
, Album album
) {
22 this.persistentId
= persistantId
;
27 Track current
= current();
28 if (current
== null) return id
;
32 public Album
getAlbum() {
33 Track current
= current();
34 if (current
== null) return null;
35 return current
.getAlbum();
38 public long persistentId() {
39 Track current
= current();
40 if (current
== null) return persistentId
;
41 return current
.persistentId();
44 public void put(Constants tag
, Object value
) {
45 for (Track t
: backingTracks
) {
50 public void setAlbum(Album album
) {
51 for (Track t
: backingTracks
) {
56 public Object
get(Constants tag
) {
57 Track current
= current();
58 if (current
== null) return null;
59 return current
.get(tag
);
62 public Object
get(Constants tag
, Object defaultValue
) {
63 Object ret
= get(tag
);
72 public Iterable
<Constants
> getAllTags() {
73 Track current
= current();
74 if (current
== null) return new ArrayList
<Constants
>(0);
75 return current
.getAllTags();
78 public void getStream(StreamReader reader
) throws IOException
{
79 Track current
= current();
80 if (current
== null) return;
81 current
.getStream(reader
);
84 public String
toString() {
86 if (c
== null) return "";
90 synchronized void addBackingTrack(Track t
) {
94 synchronized void removeBackingTrack(Track t
) {
95 backingTracks
.remove(t
);
98 synchronized int numBackingTracks() {
99 return backingTracks
.size();
102 private synchronized Track
current() {
103 if (backingTracks
.isEmpty()) return null;
104 return backingTracks
.iterator().next();
107 private static Factory factory
= new Factory();
108 public static Factory
factory() {
112 public static class Factory
{
113 private Map
<Long
, LibraryTrack
> tracks
= new HashMap
<Long
, LibraryTrack
>();
114 private volatile int id
= 0;
115 public synchronized LibraryTrack
create(Track t
) {
116 LibraryTrack track
= tracks
.get(t
.persistentId());
118 track
= new LibraryTrack(++id
, t
.persistentId(), t
.getAlbum());
119 tracks
.put(t
.persistentId(), track
);