1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 package com.buckosoft.BuckoVidLib.domain;
54
55 import java.util.ArrayList;
56 import java.util.HashMap;
57 import java.util.HashSet;
58 import java.util.LinkedList;
59 import java.util.List;
60 import java.util.ListIterator;
61
62
63
64
65
66
67
68 public class Library {
69 private List<LibrarySection> librarySections = new ArrayList<LibrarySection>();
70 private List<LibrarySection> unrestrictedLibrarySections = new ArrayList<LibrarySection>();
71 private List<String> restrictedSectionNames = new ArrayList<String>();
72
73 private HashMap<Integer, VideoBase> videoMap = new HashMap<Integer, VideoBase>();
74 private HashMap<Integer, Integer> mapVideoIdtoHashKey = new HashMap<Integer, Integer>();
75 private HashSet<Actor> actors = new HashSet<Actor>();
76 private HashSet<Director> directors = new HashSet<Director>();
77 private HashSet<Genre> genres = new HashSet<Genre>();
78 private HashSet<Writer> writers = new HashSet<Writer>();
79
80 private LinkedList<VideoBase> newestVideos = new LinkedList<VideoBase>();
81 private final int maxNewestVideos = 100;
82
83
84
85
86
87
88
89 public int getMaxNewestVideos() {
90 return maxNewestVideos;
91 }
92
93
94
95
96 public List<LibrarySection> getLibrarySections() {
97 return librarySections;
98 }
99 public List<LibrarySection> getUnrestrictedLibrarySections() {
100 return(unrestrictedLibrarySections);
101 }
102 public LibrarySection getLibrarySection(int key) {
103 for (LibrarySection ls : librarySections) {
104 if (ls.getKey() == key)
105 return(ls);
106 }
107 return(null);
108 }
109
110
111
112 public void setLibrarySections(List<LibrarySection> librarySections) {
113 this.librarySections = librarySections;
114 this.unrestrictedLibrarySections.clear();
115 for (LibrarySection ls : librarySections)
116 if (!ls.isRestricted())
117 this.unrestrictedLibrarySections.add(ls);
118 }
119
120 public void addLibrarySection(LibrarySection librarySection) {
121 this.librarySections.add(librarySection);
122 if (!librarySection.isRestricted())
123 this.unrestrictedLibrarySections.add(librarySection);
124 }
125
126
127
128
129 public List<String> getRestrictedSectionNames() {
130 return restrictedSectionNames;
131 }
132
133
134
135 public void setRestrictedSectionNames(List<String> restrictedSectionNames) {
136 this.restrictedSectionNames = restrictedSectionNames;
137 }
138
139
140
141
142 public HashMap<Integer, VideoBase> getVideoMap() {
143 return videoMap;
144 }
145
146
147
148
149
150
151
152
153 public void addVideo(int key, VideoBase video) {
154 videoMap.put(key, video);
155 this.mapVideoIdtoHashKey.put(video.getId(), key);
156
157
158
159
160
161
162
163
164
165
166 if (newestVideos.isEmpty()) {
167 newestVideos.add(video);
168 } else if (video.getAddedAt() > newestVideos.getLast().getAddedAt()
169 || newestVideos.size() < maxNewestVideos) {
170 ListIterator<VideoBase> iter = newestVideos.listIterator();
171 boolean added = false;
172 while (iter.hasNext()) {
173 VideoBase v = iter.next();
174 if (video.getAddedAt() > v.getAddedAt()) {
175 iter.previous();
176 iter.add(video);
177 added = true;
178 break;
179 }
180 }
181 if (!added)
182 newestVideos.addLast(video);
183 if (newestVideos.size() > maxNewestVideos)
184 newestVideos.removeLast();
185 }
186 }
187
188 public VideoBase getVideoBase(int key) {
189 return(videoMap.get(key));
190 }
191
192 public VideoBase getVideoById(int videoId) {
193 return(videoMap.get(mapVideoIdtoHashKey.get(videoId)));
194 }
195
196 public VideoBase getNewestVideo() {
197 if (!newestVideos.isEmpty())
198 return(newestVideos.getFirst());
199 return(null);
200 }
201
202 public List<VideoBase> getNewestVideos() {
203 return(newestVideos);
204 }
205
206
207
208
209 public HashSet<Actor> getActors() {
210 return actors;
211 }
212
213
214
215
216 public void setActors(HashSet<Actor> actors) {
217 this.actors = actors;
218 }
219
220
221
222
223 public HashSet<Genre> getGenres() {
224 return genres;
225 }
226
227
228
229
230 public void setGenres(HashSet<Genre> genres) {
231 this.genres = genres;
232 }
233
234
235
236
237 public HashSet<Director> getDirectors() {
238 return directors;
239 }
240
241
242
243
244 public void setDirectors(HashSet<Director> directors) {
245 this.directors = directors;
246 }
247
248
249
250
251 public HashSet<Writer> getWriters() {
252 return writers;
253 }
254
255
256
257
258 public void setWriters(HashSet<Writer> writers) {
259 this.writers = writers;
260 }
261
262
263 }