001package net.filebot.web; 002 003import static java.util.Collections.*; 004 005import java.io.Serializable; 006import java.util.Collection; 007import java.util.List; 008 009import net.filebot.util.FunctionList; 010 011public class SearchResult implements Serializable { 012 013 protected int id; 014 protected String name; 015 protected String[] aliasNames; 016 017 public SearchResult() { 018 // used by serializer 019 } 020 021 public SearchResult(SearchResult other) { 022 this.id = other.id; 023 this.name = other.name; 024 this.aliasNames = other.aliasNames; 025 } 026 027 public SearchResult(int id) { 028 this(id, null, EMPTY_STRING_ARRAY); 029 } 030 031 public SearchResult(int id, String name) { 032 this(id, name, EMPTY_STRING_ARRAY); 033 } 034 035 public SearchResult(int id, String name, Collection<String> aliasNames) { 036 this(id, name, aliasNames.toArray(EMPTY_STRING_ARRAY)); 037 } 038 039 public SearchResult(int id, String name, String[] aliasNames) { 040 this.id = id; 041 this.name = name; 042 this.aliasNames = aliasNames; 043 } 044 045 public int getId() { 046 return id; 047 } 048 049 public String getName() { 050 return name; 051 } 052 053 public String[] getAliasNames() { 054 return aliasNames == null ? EMPTY_STRING_ARRAY : aliasNames.clone(); 055 } 056 057 public List<String> getEffectiveNames() { 058 if (name == null || name.isEmpty()) { 059 return emptyList(); 060 } 061 if (aliasNames == null || aliasNames.length == 0) { 062 return singletonList(name); 063 } 064 return FunctionList.of(i -> { 065 return i == 0 ? name : aliasNames[i - 1]; 066 }, 1 + aliasNames.length); 067 } 068 069 @Override 070 public int hashCode() { 071 return id; 072 } 073 074 @Override 075 public boolean equals(Object other) { 076 if (other instanceof SearchResult) { 077 SearchResult r = (SearchResult) other; 078 return id == r.id; 079 } 080 return false; 081 } 082 083 @Override 084 public String toString() { 085 return name != null ? name : Integer.toString(id); 086 } 087 088 @Override 089 public SearchResult clone() { 090 return new SearchResult(this); 091 } 092 093 protected static final String[] EMPTY_STRING_ARRAY = new String[0]; 094 095}