OSDN Git Service

34b81e55e6803f92215002ca5d8ce333cfd85cb6
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / lastfm / PaginatedResult.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.Iterator;
26
27 /**
28  * A <code>PaginatedResult</code> is returned by methods which result set might
29  * be so large that it needs to be paginated. Each <code>PaginatedResult</code>
30  * contains the total number of result pages, the current page and a
31  * <code>Collection</code> of entries for the current page.
32  *
33  * @author Janni Kovacs
34  */
35 public class PaginatedResult<T> implements Iterable<T> {
36
37     private final int page;
38
39     private final int totalPages;
40
41     public final Collection<T> pageResults;
42
43     /**
44      * @param page
45      * @param totalPages
46      * @param pageResults
47      */
48     PaginatedResult(final int page, final int totalPages, final Collection<T> pageResults) {
49         this.page = page;
50         this.totalPages = totalPages;
51         this.pageResults = pageResults;
52     }
53
54     /**
55      * Returns the page number of this result.
56      *
57      * @return page number
58      */
59     public int getPage() {
60         return page;
61     }
62
63     /**
64      * Returns the total number of pages available.
65      *
66      * @return total pages
67      */
68     public int getTotalPages() {
69         return totalPages;
70     }
71
72     /**
73      * Returns <code>true</code> if this Result contains no elements, which is
74      * the case for service calls that would have returned a
75      * <code>PaginatedResult</code> but fail.
76      *
77      * @return <code>true</code> if this result contains no elements
78      */
79     public boolean isEmpty() {
80         return pageResults == null || pageResults.isEmpty();
81     }
82
83     @Override
84     public Iterator<T> iterator() {
85         return pageResults.iterator();
86     }
87 }