001package edu.pdx.cs410J.rmi;
002
003import java.rmi.NotBoundException;
004import java.rmi.RemoteException;
005import java.rmi.registry.LocateRegistry;
006import java.util.Comparator;
007
008/**
009 * This program queries the remote movie database an prints out the
010 * movies that were released in a given year.  The interesting thing
011 * about this program is that the query class is sent to the server
012 * from the client.
013 */
014public class GetMoviesInYear {
015
016  public static void main(String[] args) {
017    String host = args[0];
018    int port = Integer.parseInt(args[1]);
019    final int year = Integer.parseInt(args[2]);
020    try {
021      MovieDatabase db = (MovieDatabase) LocateRegistry.getRegistry(host, port).lookup(MovieDatabase.RMI_OBJECT_NAME);
022
023      Query query = movie -> movie.getYear() == year;
024
025      Comparator<Movie> sorter = new SortMoviesByTitle();
026
027      db.executeQuery(query, sorter).forEach(System.out::println);
028
029    } catch (RemoteException | NotBoundException ex) {
030      ex.printStackTrace(System.err);
031    }
032
033  }
034
035  /**
036   * An inner serializable comparator that sorts movies alphabetically
037   * by their titles.
038   */
039  static class SortMoviesByTitle 
040    implements Comparator<Movie>, java.io.Serializable {
041
042    @Override
043    public int compare(Movie m1, Movie m2) {
044      return m1.getTitle().compareTo(m2.getTitle());
045    }
046  }
047
048}