001/*
002 * Copyright (c) 2012, the Last.fm Java Project and Committers
003 * All rights reserved.
004 *
005 * Redistribution and use of this software in source and binary forms, with or without modification, are
006 * permitted provided that the following conditions are met:
007 *
008 * - Redistributions of source code must retain the above
009 *   copyright notice, this list of conditions and the
010 *   following disclaimer.
011 *
012 * - Redistributions in binary form must reproduce the above
013 *   copyright notice, this list of conditions and the
014 *   following disclaimer in the documentation and/or other
015 *   materials provided with the distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026
027package de.umass.lastfm;
028
029import java.util.HashMap;
030import java.util.Map;
031
032/**
033 * The <code>ItemFactoryBuilder</code> can be used to obtain {@link ItemFactory ItemFactories} for a specific type.
034 *
035 * @author Janni Kovacs
036 * @see ItemFactory
037 */
038final class ItemFactoryBuilder {
039
040        private static final ItemFactoryBuilder INSTANCE = new ItemFactoryBuilder();
041        private Map<Class, ItemFactory> factories = new HashMap<Class, ItemFactory>();
042
043        private ItemFactoryBuilder() {
044                // register default factories
045                addItemFactory(Album.class, Album.FACTORY);
046                addItemFactory(Track.class, Track.FACTORY);
047                addItemFactory(Artist.class, Artist.FACTORY);
048                addItemFactory(Tag.class, Tag.FACTORY);
049                addItemFactory(Image.class, Image.FACTORY);
050                addItemFactory(User.class, User.FACTORY);
051                addItemFactory(Event.class, Event.FACTORY);
052                addItemFactory(Venue.class, Venue.FACTORY);
053                addItemFactory(Shout.class, Shout.FACTORY);
054                addItemFactory(Playlist.class, Playlist.FACTORY);
055        }
056
057        /**
058         * Retrieve the instance of the <code>ItemFactoryBuilder</code>.
059         *
060         * @return the instance
061         */
062        public static ItemFactoryBuilder getFactoryBuilder() {
063                return INSTANCE;
064        }
065
066        public <T> void addItemFactory(Class<T> itemClass, ItemFactory<T> factory) {
067                factories.put(itemClass, factory);
068        }
069
070        /**
071         * Retrieves an {@link ItemFactory} for the given type, or <code>null</code> if no such factory was registered.
072         *
073         * @param itemClass the type's Class object
074         * @return the <code>ItemFactory</code> or <code>null</code>
075         */
076        @SuppressWarnings("unchecked")
077        public <T> ItemFactory<T> getItemFactory(Class<T> itemClass) {
078                return factories.get(itemClass);
079        }
080}