001package net.filebot.web;
002
003import static java.util.Collections.*;
004
005import java.io.Serializable;
006import java.util.Collection;
007import java.util.List;
008import java.util.Locale;
009
010import net.filebot.util.FunctionList;
011
012public class SearchResult implements Serializable {
013
014        protected int id;
015        protected String name;
016        protected String[] aliasNames;
017
018        // optional details
019        protected SimpleDate releaseDate;
020        protected String originalLanguage;
021        protected Integer runtime;
022        protected Integer popularity;
023
024        // optional language hint
025        protected String language;
026
027        public SearchResult() {
028                // used by serializer
029        }
030
031        public SearchResult(int id, String name, String[] aliasNames, SimpleDate releaseDate, String originalLanguage, Integer runtime, Number popularity, Locale locale) {
032                this.id = id;
033                this.name = name;
034                this.aliasNames = aliasNames;
035                this.releaseDate = releaseDate;
036                this.originalLanguage = originalLanguage;
037                this.runtime = runtime;
038                this.popularity = popularity == null ? null : popularity.intValue();
039                this.language = locale == null ? null : locale.toLanguageTag();
040        }
041
042        public SearchResult(SearchResult other) {
043                this.id = other.id;
044                this.name = other.name;
045                this.aliasNames = other.aliasNames;
046                this.releaseDate = other.releaseDate;
047                this.originalLanguage = other.originalLanguage;
048                this.runtime = other.runtime;
049                this.popularity = other.popularity;
050                this.language = other.language;
051        }
052
053        public SearchResult(int id, String name, String[] aliasNames) {
054                this(id, name, aliasNames, null, null, null, null, null);
055        }
056
057        public SearchResult(int id) {
058                this(id, null, EMPTY_STRING_ARRAY);
059        }
060
061        public SearchResult(int id, String name) {
062                this(id, name, EMPTY_STRING_ARRAY);
063        }
064
065        public SearchResult(int id, String name, Collection<String> aliasNames) {
066                this(id, name, aliasNames.toArray(EMPTY_STRING_ARRAY));
067        }
068
069        public int getId() {
070                return id;
071        }
072
073        public String getName() {
074                return name;
075        }
076
077        public String[] getAliasNames() {
078                return aliasNames == null ? EMPTY_STRING_ARRAY : aliasNames.clone();
079        }
080
081        public List<String> getEffectiveNames() {
082                if (name == null || name.isEmpty()) {
083                        return emptyList();
084                }
085                if (aliasNames == null || aliasNames.length == 0) {
086                        return singletonList(name);
087                }
088                return FunctionList.of(i -> {
089                        return i == 0 ? name : aliasNames[i - 1];
090                }, 1 + aliasNames.length);
091        }
092
093        public SimpleDate getReleaseDate() {
094                return releaseDate;
095        }
096
097        public String getOriginalLanguage() {
098                return originalLanguage;
099        }
100
101        public Integer getRuntime() {
102                return runtime;
103        }
104
105        public Integer getPopularity() {
106                return popularity;
107        }
108
109        public Locale getLanguage() {
110                return language == null ? null : Locale.forLanguageTag(language);
111        }
112
113        @Override
114        public int hashCode() {
115                return id;
116        }
117
118        @Override
119        public boolean equals(Object other) {
120                if (other instanceof SearchResult) {
121                        SearchResult r = (SearchResult) other;
122                        return id == r.id;
123                }
124                return false;
125        }
126
127        @Override
128        public String toString() {
129                return name != null ? name : Integer.toString(id);
130        }
131
132        public String getNameWithYear() {
133                if (name != null) {
134                        if (releaseDate != null) {
135                                String suffix = " (" + releaseDate.getYear() + ")";
136                                if (!name.endsWith(suffix)) {
137                                        return name + suffix;
138                                }
139                        }
140                        return name;
141                }
142                return Integer.toString(id);
143        }
144
145        @Override
146        public SearchResult clone() {
147                return new SearchResult(this);
148        }
149
150        protected static final String[] EMPTY_STRING_ARRAY = new String[0];
151
152}