OSDN Git Service

Update Eleven headers and namespace for open source
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / lastfm / ImageHolder.java
1 /*
2  * Copyright (c) 2012, the Last.fm Java Project and Committers All rights
3  * reserved. Redistribution and use of this software in source and binary forms,
4  * with or without modification, are permitted provided that the following
5  * conditions are met: - Redistributions of source code must retain the above
6  * copyright notice, this list of conditions and the following disclaimer. -
7  * Redistributions in binary form must reproduce the above copyright notice,
8  * this list of conditions and the following disclaimer in the documentation
9  * and/or other materials provided with the distribution. THIS SOFTWARE IS
10  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
11  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
13  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
14  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
16  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
17  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  */
21
22 package com.cyanogenmod.eleven.lastfm;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Set;
29
30 /**
31  * Abstract superclass for all items that may contain images (such as
32  * {@link Artist}s, {@link Album}s or {@link Track}s).
33  * 
34  * @author Janni Kovacs
35  */
36 public abstract class ImageHolder {
37
38     protected Map<ImageSize, String> imageUrls = new HashMap<ImageSize, String>();
39
40     /**
41      * Returns a Set of all {@link ImageSize}s available.
42      * 
43      * @return all sizes
44      */
45     public Set<ImageSize> availableSizes() {
46         return imageUrls.keySet();
47     }
48
49     /**
50      * Returns the URL of the image in the specified size, or <code>null</code>
51      * if not available.
52      * 
53      * @param size The preferred size
54      * @return an image URL
55      */
56     public String getImageURL(final ImageSize size) {
57         return imageUrls.get(size);
58     }
59
60     /**
61      * @param holder
62      * @param element
63      */
64     protected static void loadImages(final ImageHolder holder, final DomElement element) {
65         final Collection<DomElement> images = element.getChildren("image");
66         for (final DomElement image : images) {
67             final String attribute = image.getAttribute("size");
68             ImageSize size = null;
69             if (attribute == null) {
70                 size = ImageSize.UNKNOWN;
71             } else {
72                 try {
73                     size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
74                 } catch (final IllegalArgumentException e) {
75                     // if they suddenly again introduce a new image size
76                 }
77             }
78             if (size != null) {
79                 holder.imageUrls.put(size, image.getText());
80             }
81         }
82     }
83 }