Repository navigation
Expand file tree
/
Copy pathPodcastLibrary.java
More file actions
146 lines (131 loc) · 3.7 KB
/
Copy pathPodcastLibrary.java
File metadata and controls
146 lines (131 loc) · 3.7 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
* Copyright (c) 2013 Yannic Remmet, Maximilian Berger.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* Yannic Remmet - initial API and implementation
* Maximilian Berger - initial API and implementation
******************************************************************************/
package data;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.HashSet;
import java.util.Observable;
import com.google.common.collect.ImmutableList;
/**
*
* @author yannic
*
*/
public class PodcastLibrary extends Observable implements Serializable {
private static PodcastLibrary lib = null;
private static final long serialVersionUID = -7883861152152068899L;
private HashSet<Podcast> podcasts;
/**
* Private Default Constructor
*/
private PodcastLibrary(){
podcasts = new HashSet<Podcast>();
}
/**
* @return the library instance
*/
public static PodcastLibrary getLibrary(){
if(lib == null)
lib = new PodcastLibrary();
return lib;
}
/**
* @return ImmutableList of all Podcasts
*/
public ImmutableList<Podcast> getPodcasts(){
return ImmutableList.copyOf(podcasts);
}
/**
*
* @param u URL of Podcastfeed
* @return this
*/
public PodcastLibrary putPodcast(URL u){ /// Haesslich ich weiss wird noch gefixt
new RSSParser(u, this);
return this;
}
/**
*
* @param p podcast
* @return this
*/
public PodcastLibrary updatePodcast(Podcast p){
new RSSParser(p, this);
return this;
}
/**
* @param p podcast added to the Library
*/
protected void addPodcast(Podcast p){
podcasts.add(p);
super.setChanged();
super.notifyObservers(p);
super.clearChanged();
}
/**
* Writes Library Data do Disk into
* Given Directory
* @throws FileNotFoundException
* @throws IOException
*/
public void exportLibrary() throws FileNotFoundException, IOException{
String saveDir =PodcastPropertiesManager.getPropertyManager().getProperty(PodcastPropertiesManager.LIBRARY_FILE);
File f = new File(saveDir);
if (!f.exists()) {
new File(f.getParent()).mkdirs();
f.createNewFile();
}
ObjectOutputStream out;
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
out.writeObject(this);
out.close();
}
/**
* Imports Library from given save Directory
* @return imported Library
* @throws FileNotFoundException
* @throws IOException
* @throws ClassNotFoundException
*/
public static PodcastLibrary importLibray() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream in;
in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(
PodcastPropertiesManager.getPropertyManager().getProperty(PodcastPropertiesManager.LIBRARY_FILE))));
lib = (PodcastLibrary) in.readObject();
in.close();
return lib;
}
/**
* Remove Podcast from Libray
* @param title Title of the Podcast
* @return true/ false
*/
public boolean removePodcast(String title){
for(Podcast p : podcasts)
if(p.getTitle().equals(title))
return podcasts.remove(p);
return false;
}
public void updatePodcasts() {
for(Podcast p : podcasts)
new RSSParser(p, this);
}
}