001package net.filebot.web; 002 003import static java.util.Collections.*; 004 005import java.util.List; 006import java.util.Locale; 007import java.util.regex.Matcher; 008import java.util.regex.Pattern; 009 010import net.filebot.util.FunctionList; 011 012public class Movie extends SearchResult { 013 014 public static final int UNDEFINED = 0; 015 016 protected int year; 017 protected int imdbId; 018 protected int tmdbId; 019 020 public Movie() { 021 // used by deserializer 022 } 023 024 public Movie(int id, String name, String[] aliasNames, int year, int imdbId, int tmdbId, Locale locale) { 025 this(id, name, aliasNames, year, imdbId, tmdbId, null, null, null, null, locale); 026 } 027 028 public Movie(int id, String name, String[] aliasNames, int year, int imdbId, int tmdbId, SimpleDate releaseDate, String originalLanguage, Integer runtime, Number popularity, Locale locale) { 029 super(id, name, aliasNames, releaseDate, originalLanguage, runtime, popularity, locale); 030 this.year = year; 031 this.imdbId = imdbId; 032 this.tmdbId = tmdbId; 033 } 034 035 public Movie(Movie other) { 036 super(other); 037 this.year = other.year; 038 this.imdbId = other.imdbId; 039 this.tmdbId = other.tmdbId; 040 } 041 042 public int getYear() { 043 return year; 044 } 045 046 public int getImdbId() { 047 return imdbId; 048 } 049 050 public int getTmdbId() { 051 return tmdbId; 052 } 053 054 @Override 055 public String getNameWithYear() { 056 return toString(name, year); 057 } 058 059 @Override 060 public List<String> getEffectiveNames() { 061 if (name == null || name.length() == 0) { 062 return emptyList(); 063 } 064 if (aliasNames == null || aliasNames.length == 0) { 065 return singletonList(toString(name, year)); 066 } 067 return FunctionList.of(i -> { 068 return i == 0 ? Movie.toString(name, year) : Movie.toString(aliasNames[i - 1], year); 069 }, 1 + aliasNames.length); 070 } 071 072 public List<String> getEffectiveNamesWithoutYear() { 073 return super.getEffectiveNames(); 074 } 075 076 @Override 077 public boolean equals(Object object) { 078 if (object instanceof Movie) { 079 Movie other = (Movie) object; 080 081 if (tmdbId > 0 && other.tmdbId > 0) { 082 return tmdbId == other.tmdbId; 083 } 084 if (imdbId > 0 && other.imdbId > 0) { 085 return imdbId == other.imdbId; 086 } 087 088 return year == other.year && name.equals(other.name); 089 } 090 091 return false; 092 } 093 094 @Override 095 public Movie clone() { 096 return new Movie(this); 097 } 098 099 @Override 100 public String toString() { 101 if (name != null && year > 0) { 102 return toString(name, year); 103 } 104 105 if (tmdbId > 0) { 106 return "TheMovieDB::" + tmdbId; 107 } 108 109 if (imdbId > 0) { 110 return "IMDB::" + imdbId; 111 } 112 113 return super.toString(); 114 } 115 116 public static Movie IMDB(int imdbId) { 117 return new Movie(imdbId, null, null, UNDEFINED, imdbId, UNDEFINED, null, null, null, null, null); 118 } 119 120 public static Movie IMDB(String name, int year, int imdbId) { 121 return new Movie(imdbId, name, null, year, imdbId, UNDEFINED, null, null, null, null, null); 122 } 123 124 public static Movie TMDB(int tmdbId) { 125 return new Movie(tmdbId, null, null, UNDEFINED, UNDEFINED, tmdbId, null, null, null, null, null); 126 } 127 128 public static Movie TMDB(String name, int year, int tmdbId) { 129 return new Movie(tmdbId, name, null, year, UNDEFINED, tmdbId, null, null, null, null, null); 130 } 131 132 public static Movie NameYear(String name, int year) { 133 return new Movie(UNDEFINED, name, null, year, UNDEFINED, UNDEFINED, null, null, null, null, null); 134 } 135 136 public static Movie ID(Integer tmdbId, Integer imdbId, String name, Integer year) { 137 if ((tmdbId != null && tmdbId > 0) || (imdbId != null && imdbId > 0) || (name != null && year != null && !name.isEmpty() && year > 0)) { 138 int id = tmdbId != null && tmdbId > 0 ? tmdbId : imdbId != null && imdbId > 0 ? imdbId : UNDEFINED; 139 return new Movie(id, name, null, year == null ? UNDEFINED : year, imdbId == null ? UNDEFINED : imdbId, tmdbId == null ? UNDEFINED : tmdbId, null, null, null, null, null); 140 } 141 return null; 142 } 143 144 private static final Pattern NAME_YEAR_PATTERN = Pattern.compile("(.+?)[ _.][(]?((?:19|20)\\d{2})[)]?"); 145 146 public static Movie matchNameYear(String s) { 147 if (s == null || s.isEmpty()) { 148 return null; 149 } 150 151 Matcher m = NAME_YEAR_PATTERN.matcher(s.trim()); 152 if (m.matches()) { 153 return Movie.NameYear(m.group(1).trim(), Integer.parseInt(m.group(2))); 154 } 155 156 return null; 157 } 158 159 public static String toString(String name, int year) { 160 return year > 0 ? name + " (" + year + ")" : name; 161 } 162 163}