2019-10-09 11:36:56 +00:00
|
|
|
package reentrantrwlock;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2019-10-20 23:05:19 +00:00
|
|
|
import java.util.Iterator;
|
2019-10-09 11:36:56 +00:00
|
|
|
|
|
|
|
public class MovieCatalog {
|
|
|
|
|
|
|
|
private final Map<String, Movie> movies;
|
|
|
|
private final ReentrantReadWriteLock rwlock;
|
|
|
|
private final Lock rLock, wLock;
|
|
|
|
private static final Random generator = new Random();
|
|
|
|
|
|
|
|
public MovieCatalog() {
|
|
|
|
movies = new TreeMap<>();
|
|
|
|
rwlock = new ReentrantReadWriteLock();
|
|
|
|
rLock = rwlock.readLock();
|
|
|
|
wLock = rwlock.writeLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current size of this catalog
|
|
|
|
* @return the size of this catalog
|
|
|
|
*/
|
|
|
|
public int getSize() {
|
2019-10-20 23:05:19 +00:00
|
|
|
int length;
|
|
|
|
rLock.lock();
|
|
|
|
length = movies.size();
|
2019-10-21 08:45:09 +00:00
|
|
|
System.out.println("The movies catalog size is: " + length);
|
2019-10-20 23:05:19 +00:00
|
|
|
rLock.unlock();
|
|
|
|
return length;
|
2019-10-09 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a movie from this catalog
|
|
|
|
* @param the title of movie to be obtained
|
|
|
|
* @return the movie with the title provided or
|
|
|
|
* null if the movie is not in the catalog
|
|
|
|
*/
|
|
|
|
public Movie getMovie(String title) {
|
2019-10-20 23:05:19 +00:00
|
|
|
Movie m;
|
|
|
|
rLock.lock();
|
|
|
|
m = movies.get(title);
|
2019-10-21 08:45:09 +00:00
|
|
|
System.out.println("Getting movie: " + m.getTitle());
|
2019-10-20 23:05:19 +00:00
|
|
|
rLock.unlock();
|
|
|
|
return m;
|
2019-10-09 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of all movie titles in this catalog
|
2019-10-20 23:05:19 +00:00
|
|
|
* @return a list of the movie titles in this catalog
|
2019-10-09 11:36:56 +00:00
|
|
|
*/
|
|
|
|
public List<String> getTitles() {
|
2019-10-20 23:05:19 +00:00
|
|
|
List<String> titles;
|
|
|
|
rLock.lock();
|
|
|
|
titles = new ArrayList<>(movies.keySet());
|
2019-10-21 08:45:09 +00:00
|
|
|
System.out.println("Getting title list of length " + titles.size());
|
2019-10-20 23:05:19 +00:00
|
|
|
rLock.unlock();
|
|
|
|
return titles;
|
2019-10-09 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a random movie from this catalog
|
|
|
|
* @return a randomly selected movie from this catalog
|
|
|
|
*/
|
|
|
|
public Movie getRandomMovie() {
|
2019-10-20 23:05:19 +00:00
|
|
|
Movie m;
|
|
|
|
rLock.lock();
|
|
|
|
final int r = generator.nextInt(movies.size()) - 1;
|
|
|
|
final Iterator<Movie> it = movies.values().iterator();
|
|
|
|
for (int i = 0; i < r; i++) {
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
m = it.next();
|
2019-10-21 08:45:09 +00:00
|
|
|
System.out.println("Getting random movie: " + m.getTitle());
|
2019-10-20 23:05:19 +00:00
|
|
|
rLock.unlock();
|
|
|
|
return m;
|
2019-10-09 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a movie to the catalog
|
|
|
|
* @param the movie to be added to the catalog
|
|
|
|
*/
|
|
|
|
public void addMovie(Movie movie) {
|
2019-10-20 23:05:19 +00:00
|
|
|
wLock.lock();
|
|
|
|
movies.put(movie.getTitle(), movie);
|
2019-10-21 08:45:09 +00:00
|
|
|
System.out.println("Putting movie: " + movie.getTitle());
|
2019-10-20 23:05:19 +00:00
|
|
|
wLock.unlock();
|
2019-10-09 11:36:56 +00:00
|
|
|
}
|
|
|
|
}
|