1 /*** 2 * Ambient - A music player for the Android platform 3 Copyright (C) 2007 Martin Vysny 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package sk.baka.ambient.collection; 20 21 import java.io.Serializable; 22 23 import sk.baka.ambient.commons.MiscUtils; 24 25 /*** 26 * A single category, may be an artist, an album or a genre. 27 * 28 * @author Martin Vysny 29 */ 30 public final class CategoryItem implements Serializable { 31 private static final long serialVersionUID = -6381439171709962868L; 32 /*** 33 * The collection-strategy-dependent ID, may be <code>null</code>. 34 */ 35 public final Object id; 36 /*** 37 * The name of the item, may be <code>null</code> if the name is missing. 38 */ 39 public final String name; 40 41 /*** 42 * Returns displayable name (the "[empty]" string instead of 43 * <code>null</code>). 44 * 45 * @return the displayable name. 46 */ 47 public String getDisplayableName() { 48 if (MiscUtils.isEmptyOrWhitespace(name)) { 49 return "[empty]"; 50 } 51 return name; 52 } 53 54 /*** 55 * Returns displayable year (the "[empty]" string instead of 56 * <code>null</code>). 57 * 58 * @return the displayable year. 59 */ 60 public String getDisplayableYear() { 61 if (MiscUtils.isEmptyOrWhitespace(year)) { 62 return "[empty]"; 63 } 64 return year; 65 } 66 67 /*** 68 * In case of an album the year may be specified. <code>null</code> if not 69 * known. 70 */ 71 public final String year; 72 73 /*** 74 * The category of this item, must not be <code>null</code>. 75 */ 76 public final CategoryEnum category; 77 /*** 78 * Contains a number of albums for given artist or genre. Equals to 1 for 79 * {@link CategoryEnum#Title} and {@link CategoryEnum#Album} categories. May 80 * be -1 if not known. 81 */ 82 public final int albums; 83 /*** 84 * Contains a number of songs for given artist, genre or album. Equals to 1 85 * for {@link CategoryEnum#Title}. May be -1 if not known. 86 */ 87 public final int songs; 88 89 /*** 90 * Creates new category item instance. 91 * 92 * @param id 93 * the collection-strategy-dependent ID, may be <code>null</code> 94 * 95 * @param name 96 * the name of the item. 97 * @param year 98 * in case of an album the year may be specified. 99 * <code>null</code> if not known. 100 * @param category 101 * The category of this item, must not be <code>null</code>. 102 * @param albums 103 * Contains a number of albums for given artist or genre. Equals 104 * to 1 for {@link CategoryEnum#Title} and 105 * {@link CategoryEnum#Album} categories. May be -1 if not known. 106 * @param songs 107 * Contains a number of songs for given artist, genre or album. 108 * Equals to 1 for {@link CategoryEnum#Title}. May be -1 if not 109 * known. 110 */ 111 public CategoryItem(final Object id, final String name, final String year, 112 final CategoryEnum category, final int albums, final int songs) { 113 if (category == null) { 114 throw new IllegalArgumentException("category is null"); 115 } 116 this.id = id; 117 this.name = name; 118 this.year = year; 119 this.category = category; 120 this.albums = albums; 121 this.songs = songs; 122 } 123 124 /*** 125 * Builds the {@link CategoryItem}. 126 * 127 * @author Martin Vysny 128 */ 129 public static class Builder { 130 /*** 131 * The collection-strategy-dependent ID, may be <code>null</code>. 132 */ 133 public Object id; 134 /*** 135 * The name of the item, may be <code>null</code> if the name is 136 * missing. 137 */ 138 public String name; 139 /*** 140 * In case of an album the year may be specified. <code>null</code> if 141 * not known. 142 */ 143 public String year; 144 145 /*** 146 * The category of this item, must not be <code>null</code>. 147 */ 148 public CategoryEnum category; 149 150 /*** 151 * Contains a number of albums for given artist or genre. Equals to 1 152 * for {@link CategoryEnum#Title} and {@link CategoryEnum#Album} 153 * categories. May be -1 if not known. 154 */ 155 public int albums = -1; 156 /*** 157 * Contains a number of songs for given artist, genre or album. Equals 158 * to 1 for {@link CategoryEnum#Title}. May be -1 if not known. 159 */ 160 public int songs = -1; 161 162 /*** 163 * Builds new category item. 164 * 165 * @return the category item instance. 166 */ 167 public CategoryItem build() { 168 return new CategoryItem(id, name, year, category, albums, songs); 169 } 170 171 /*** 172 * Sets id. 173 * 174 * @param id 175 * The collection-strategy-dependent ID, may be 176 * <code>null</code>. 177 * @return this 178 */ 179 public Builder setId(final Object id) { 180 this.id = id; 181 return this; 182 } 183 184 /*** 185 * Sets name. 186 * 187 * @param name 188 * The name of the item. 189 * @return this 190 */ 191 public Builder setName(final String name) { 192 this.name = name; 193 return this; 194 } 195 196 /*** 197 * Sets year. 198 * 199 * @param year 200 * In case of an album the year may be specified. 201 * <code>null</code> if not known. 202 * @return this 203 */ 204 public Builder setYear(final String year) { 205 this.year = year; 206 return this; 207 } 208 209 /*** 210 * Sets the category. 211 * 212 * @param category 213 * The category of this item, must not be <code>null</code>. 214 * @return this 215 */ 216 public Builder setCategory(final CategoryEnum category) { 217 this.category = category; 218 return this; 219 } 220 221 /*** 222 * Contains a number of albums for given artist or genre. Equals to 1 223 * for {@link CategoryEnum#Title} and {@link CategoryEnum#Album} 224 * categories. May be -1 if not known. 225 * 226 * @param albums 227 * The number of albums for this item. 228 * @return this 229 */ 230 public Builder setAlbums(int albums) { 231 this.albums = albums; 232 return this; 233 } 234 235 /*** 236 * Contains a number of songs for given artist, genre or album. Equals 237 * to 1 for {@link CategoryEnum#Title}. May be -1 if not known. 238 * 239 * @param songs 240 * The number of songs for this item. 241 * @return this 242 */ 243 public Builder setSongs(int songs) { 244 this.songs = songs; 245 return this; 246 } 247 248 /*** 249 * Fills this builder with data from given item. 250 * 251 * @param item 252 * the item to read 253 * @return this 254 */ 255 public Builder getData(CategoryItem item) { 256 id = item.id; 257 albums = item.albums; 258 category = item.category; 259 name = item.name; 260 songs = item.songs; 261 year = item.year; 262 return this; 263 } 264 } 265 266 @Override 267 public String toString() { 268 return id + ": " + name + (year == null ? "" : " (" + year + ")"); 269 } 270 271 @Override 272 public boolean equals(Object o) { 273 if (o == this) { 274 return true; 275 } 276 if (!(o instanceof CategoryItem)) { 277 return false; 278 } 279 final CategoryItem other = (CategoryItem) o; 280 return (category == other.category) 281 && MiscUtils.nullEquals(year, other.year) 282 && MiscUtils.nullEquals(name, other.name); 283 } 284 285 @Override 286 public int hashCode() { 287 int i = category.hashCode(); 288 i = i * 1001 + MiscUtils.hashCode(year); 289 i = i * 1001 + MiscUtils.hashCode(name); 290 return i; 291 } 292 }