001package edu.pdx.cs410J.rmi; 002 003import java.io.Serializable; 004import java.util.*; 005 006/** 007 * This class represents a remote <code>Movie</code> object. It is 008 * {@link Serializable} because instances of <code>Movie</code> are 009 * sent from the server to client. Because it does not implement the 010 * {@link java.rmi.Remote} interface, the client JVM receives copies 011 * of the <code>Movie</code> object. That is, changes to a 012 * <code>Movie</code> made on by the client are not reflected in the 013 * server. 014 */ 015public class Movie implements Serializable { 016 017 /** The next movie id */ 018 private static long nextId = 0; 019 020 /** A unique number that identifies this movie */ 021 private long id; 022 023 /** The title of the movie */ 024 private String title; 025 026 /** The year the movie was released */ 027 private int year; 028 029 /** A map of the characters in a movie to the id of the actor who played 030 * them. */ 031 private Map<String, Long> characters; 032 033 private int numberOfAwards; 034 035 ///////////////////////// Constructors //////////////////////// 036 037 /** 038 * Creates a new <code>Movie</code> with the given title and release 039 * year. Movies should only be created on the server side. 040 */ 041 Movie(String title, int year) { 042 this.id = Movie.nextId++; 043 this.title = title; 044 this.year = year; 045 this.characters = new HashMap<>(); 046 } 047 048 ////////////////////// Accessor Methods /////////////////////// 049 050 /** 051 * Returns this <code>Movie</code>'s id 052 */ 053 public long getId() { 054 return this.id; 055 } 056 057 /** 058 * Package-protected method for setting the id 059 * @param id The id for this movie 060 */ 061 void setId(long id) { 062 this.id = id; 063 } 064 065 /** 066 * Returns the title of this <code>Movie</code> 067 */ 068 public String getTitle() { 069 return this.title; 070 } 071 072 /** 073 * Sets the title of this <code>Movie</code> 074 */ 075 public void setTitle(String title) { 076 this.title = title; 077 } 078 079 /** 080 * Returns the year in which this movie was released 081 */ 082 public int getYear() { 083 return this.year; 084 } 085 086 /** 087 * Sets the year in which this movie was released 088 */ 089 public void setYear(int year) { 090 this.year = year; 091 } 092 093 /** 094 * Returns a map of character names to the actor that played 095 * them. 096 */ 097 public Map<String, Long> getCharacters() { 098 return this.characters; 099 } 100 101 /** 102 * Returns the ids of the actors that are in this movie 103 */ 104 public Set<Long> getActors() { 105 return new HashSet<>(this.characters.values()); 106 } 107 108 /** 109 * Makes note of a character in the movie played by a given actor. 110 * This behavior is intended to be server-side only. 111 * 112 * @throws IllegalArgumentException 113 * There is a character by that name that is played by a 114 * different actor. 115 */ 116 void addCharacter(String character, long actor) { 117 Long a = this.characters.get(character); 118 if (a != null && !a.equals(actor)) { 119 String s = "The character " + character + 120 " is already played by " + a; 121 throw new IllegalArgumentException(s); 122 } 123 124 this.characters.put(character, actor); 125 } 126 127 ////////////////////// Utility Methods ///////////////////////// 128 129 /** 130 * Returns a brief textual representation of this <code>Movie</code> 131 */ 132 public String toString() { 133 return "Movie " + this.getId() + " \"" + this.getTitle() + 134 "\" (" + this.getYear() + ")"; 135 } 136 137 /** 138 * Two <code>Movie</code>s are equal if they have the same id 139 */ 140 public boolean equals(Object o) { 141 if (o instanceof Movie) { 142 Movie other = (Movie) o; 143 return this.getId() == other.getId(); 144 } 145 146 return false; 147 } 148 149 public int getNumberOfAwards() { 150 return numberOfAwards; 151 } 152}