001package net.filebot.web;
002
003import static java.util.Arrays.*;
004import static java.util.Collections.*;
005import static java.util.stream.Collectors.*;
006
007import java.io.Serializable;
008import java.net.URL;
009import java.util.Comparator;
010import java.util.List;
011import java.util.Locale;
012import java.util.Objects;
013import java.util.stream.Stream;
014
015public class Artwork implements Serializable {
016
017        protected String[] tags;
018        protected URL url;
019
020        protected String language;
021        protected Double rating;
022
023        public Artwork() {
024                // used by serializer
025        }
026
027        public Artwork(Stream<?> tags, URL url, Locale language, Double rating) {
028                this.tags = tags.filter(Objects::nonNull).map(Object::toString).toArray(String[]::new);
029                this.url = url;
030                this.language = language == null || language.getLanguage().isEmpty() ? null : language.getLanguage();
031                this.rating = rating;
032        }
033
034        public List<String> getTags() {
035                return unmodifiableList(asList(tags));
036        }
037
038        public URL getUrl() {
039                return url;
040        }
041
042        public Locale getLanguage() {
043                return language == null ? null : new Locale(language);
044        }
045
046        public double getRating() {
047                return rating == null ? 0 : rating;
048        }
049
050        public boolean matches(Object... tags) {
051                if (tags == null || tags.length == 0) {
052                        return true;
053                }
054
055                return stream(tags).filter(Objects::nonNull).map(Object::toString).allMatch(tag -> {
056                        return stream(this.tags).anyMatch(tag::equalsIgnoreCase) || tag.equalsIgnoreCase(language);
057                });
058        }
059
060        @Override
061        public int hashCode() {
062                return url.hashCode();
063        }
064
065        @Override
066        public boolean equals(Object other) {
067                if (other instanceof Artwork) {
068                        Artwork artwork = (Artwork) other;
069                        return url.sameFile(artwork.url);
070                }
071                return false;
072        }
073
074        @Override
075        public String toString() {
076                return Stream.of(String.join("/", tags), language, rating, url).filter(Objects::nonNull).map(Object::toString).collect(joining(", ", "[", "]"));
077        }
078
079        public static final Comparator<Artwork> RATING_ORDER = Comparator.comparing(Artwork::getRating, reverseOrder());
080
081}