OSDN Git Service

overhaul
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / lastfm / api / ResponseBuilder.java
1 /*\r
2  * Copyright (c) 2012, the Last.fm Java Project and Committers\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use of this software in source and binary forms, with or without modification, are\r
6  * permitted provided that the following conditions are met:\r
7  *\r
8  * - Redistributions of source code must retain the above\r
9  *   copyright notice, this list of conditions and the\r
10  *   following disclaimer.\r
11  *\r
12  * - Redistributions in binary form must reproduce the above\r
13  *   copyright notice, this list of conditions and the\r
14  *   following disclaimer in the documentation and/or other\r
15  *   materials provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED\r
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\r
19  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\r
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR\r
23  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25  */\r
26 \r
27 package com.andrew.apollo.lastfm.api;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.Collection;\r
31 import java.util.Collections;\r
32 \r
33 import com.andrew.apollo.utils.DomElement;\r
34 \r
35 \r
36 /**\r
37  * This utility class can be used to generically generate Result objects (usually Lists or {@link PaginatedResult}s) from an XML response\r
38  * using {@link ItemFactory ItemFactories}.\r
39  *\r
40  * @author Janni Kovacs\r
41  */\r
42 public final class ResponseBuilder {\r
43 \r
44         private ResponseBuilder() {\r
45         }\r
46 \r
47         private static <T> ItemFactory<T> getItemFactory(Class<T> itemClass) {\r
48                 return ItemFactoryBuilder.getFactoryBuilder().getItemFactory(itemClass);\r
49         }\r
50 \r
51         public static <T> Collection<T> buildCollection(Result result, Class<T> itemClass) {\r
52                 return buildCollection(result, getItemFactory(itemClass));\r
53         }\r
54 \r
55         public static <T> Collection<T> buildCollection(Result result, ItemFactory<T> factory) {\r
56                 if (!result.isSuccessful())\r
57                         return Collections.emptyList();\r
58                 return buildCollection(result.getContentElement(), factory);\r
59         }\r
60 \r
61         public static <T> Collection<T> buildCollection(DomElement element, Class<T> itemClass) {\r
62                 return buildCollection(element, getItemFactory(itemClass));\r
63         }\r
64 \r
65         public static <T> Collection<T> buildCollection(DomElement element, ItemFactory<T> factory) {\r
66                 if (element == null)\r
67                         return Collections.emptyList();\r
68                 Collection<DomElement> children = element.getChildren();\r
69                 Collection<T> items = new ArrayList<T>(children.size());\r
70                 for (DomElement child : children) {\r
71                         items.add(factory.createItemFromElement(child));\r
72                 }\r
73                 return items;\r
74         }\r
75 \r
76         public static <T> PaginatedResult<T> buildPaginatedResult(Result result, Class<T> itemClass) {\r
77                 return buildPaginatedResult(result, getItemFactory(itemClass));\r
78         }\r
79 \r
80         public static <T> PaginatedResult<T> buildPaginatedResult(Result result, ItemFactory<T> factory) {\r
81                 if (!result.isSuccessful()) {\r
82                         return new PaginatedResult<T>(0, 0, Collections.<T>emptyList());\r
83                 }\r
84 \r
85                 DomElement contentElement = result.getContentElement();\r
86                 return buildPaginatedResult(contentElement, contentElement, factory);\r
87         }\r
88 \r
89         public static <T> PaginatedResult<T> buildPaginatedResult(DomElement contentElement, DomElement childElement, Class<T> itemClass) {\r
90                 return buildPaginatedResult(contentElement, childElement, getItemFactory(itemClass));\r
91         }\r
92 \r
93         public static <T> PaginatedResult<T> buildPaginatedResult(DomElement contentElement, DomElement childElement, ItemFactory<T> factory) {\r
94                 Collection<T> items = buildCollection(childElement, factory);\r
95 \r
96                 String totalPagesAttribute = contentElement.getAttribute("totalPages");\r
97                 if (totalPagesAttribute == null)\r
98                         totalPagesAttribute = contentElement.getAttribute("totalpages");\r
99 \r
100                 int page = Integer.parseInt(contentElement.getAttribute("page"));\r
101                 int totalPages = Integer.parseInt(totalPagesAttribute);\r
102 \r
103                 return new PaginatedResult<T>(page, totalPages, items);\r
104         }\r
105 \r
106         public static <T> T buildItem(Result result, Class<T> itemClass) {\r
107                 return buildItem(result, getItemFactory(itemClass));\r
108         }\r
109 \r
110         public static <T> T buildItem(Result result, ItemFactory<T> factory) {\r
111                 if (!result.isSuccessful())\r
112                         return null;\r
113                 return buildItem(result.getContentElement(), factory);\r
114         }\r
115 \r
116         public static <T> T buildItem(DomElement element, Class<T> itemClass) {\r
117                 return buildItem(element, getItemFactory(itemClass));\r
118         }\r
119 \r
120         private static <T> T buildItem(DomElement element, ItemFactory<T> factory) {\r
121                 return factory.createItemFromElement(element);\r
122         }\r
123 }\r