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.library; 20 21 import java.util.Collections; 22 import java.util.Enumeration; 23 import java.util.HashSet; 24 import java.util.Iterator; 25 import java.util.Map; 26 import java.util.Set; 27 import java.util.StringTokenizer; 28 29 /*** 30 * Handles genre cache and new genre creation. 31 * 32 * @author Martin Vysny 33 */ 34 public final class GenreCache { 35 /*** 36 * The backend. 37 */ 38 private final DBStrategy backend; 39 40 /*** 41 * The genre cache. 42 */ 43 private final Map<String, Long> genreCache; 44 45 /*** 46 * Creates new genre cache. 47 * 48 * @param backend 49 * the backend. 50 */ 51 GenreCache(final DBStrategy backend) { 52 super(); 53 this.backend = backend; 54 genreCache = backend.getAllGenres(); 55 } 56 57 private final Set<Long> genreIds = new HashSet<Long>(); 58 private final Set<Long> readonlyGenreIds = Collections 59 .unmodifiableSet(genreIds); 60 61 /*** 62 * Registers a genre and returns a list of ids of registered genres. 63 * 64 * @param genres 65 * the genre to register, a comma-separated list of genres. 66 * @return read-only list of genre database ids. 67 */ 68 public Set<Long> register(final String genres) { 69 genreIds.clear(); 70 for (final String genre : splitGenres(genres)) { 71 Long genreId = genreCache.get(genre); 72 if (genreId == null) { 73 genreId = backend.registerNewGenre(genre); 74 genreCache.put(genre, genreId); 75 } 76 genreIds.add(genreId); 77 } 78 return readonlyGenreIds; 79 } 80 81 /*** 82 * Split given genre list (genres separated by , or ;). 83 * 84 * @param genreList 85 * the list to split. If <code>null</code> is given then an 86 * empty iterable is returned. 87 * @return tokens. use {@link String#trim()} to get rid of surrounding 88 * whitespaces. 89 */ 90 @SuppressWarnings("unchecked") 91 public static Iterable<String> splitGenres(final String genreList) { 92 if ((genreList == null) || (genreList.length() == 0)) { 93 return Collections.EMPTY_LIST; 94 } 95 final Enumeration<Object> e = new StringTokenizer(genreList, ",;"); 96 return new Iterable<String>() { 97 public Iterator<String> iterator() { 98 return new Iterator<String>() { 99 public boolean hasNext() { 100 return e.hasMoreElements(); 101 } 102 103 public String next() { 104 return e.nextElement().toString().trim(); 105 } 106 107 public void remove() { 108 throw new UnsupportedOperationException(); 109 } 110 }; 111 } 112 }; 113 } 114 }