1 package be.bastard.mtracker;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.TreeSet;
6
7 /***
8 * Created by IntelliJ IDEA.
9 * User: rls
10 * Date: 6-feb-2006
11 * Time: 14:48:07
12 * To change this template use File | Settings | File Templates.
13 */
14 public class MediaCatalog extends AbstractMediaItem {
15 protected Collection<MediaItem> items;
16 protected String title;
17
18 public MediaCatalog(String title) {
19 this.title = title;
20 this.items = new TreeSet<MediaItem>();
21 }
22
23 public MediaItem search(MediaId id) {
24 for (MediaItem item : this.items) {
25 if (item.getId().equals(id)) {
26 return item;
27 }
28 }
29 return null;
30 }
31
32 public Collection<MediaItem> search(Collection<? extends MediaId> ids) {
33 Collection<MediaItem> items = new ArrayList<MediaItem>(ids.size());
34 for (MediaItem item : this.items) {
35 if (ids.contains(item.getId())) {
36 items.add(item);
37 }
38 }
39 return items;
40 }
41
42 public int add(MediaItem item) {
43 return this.items.add(item) ? 1 : 0;
44 }
45
46 public int add(Collection<? extends MediaItem> items) {
47 return this.items.addAll(items) ? items.size() : 0;
48 }
49
50 public int remove(MediaItem item) {
51 return this.items.remove(item) ? 1 : 0;
52 }
53
54 public int remove(MediaId id) {
55 return this.remove(search(id));
56 }
57
58 public int remove(Collection<? extends MediaId> ids) {
59 int count = 0;
60 for (MediaItem item : this.items) {
61 if (ids.contains(item.getId())) {
62 count += this.remove(item);
63 }
64 }
65 return count;
66 }
67
68 public int remove() {
69 int count = this.items.size();
70 this.items.clear();
71 return count;
72 }
73
74 public boolean equals(MediaItem otherItem) {
75 if (otherItem instanceof MediaCatalog) {
76 return super.equals(otherItem);
77 }
78 return false;
79 }
80
81 public int size() {
82 return this.items.size();
83 }
84 }