@@ -29,6 +29,19 @@ import java.util.function.Supplier
2929
3030enum class PagingTraversalType { BACKWARDS , FORWARDS }
3131
32+ /* *
33+ * The offset-based paging object is a container for a set of objects. It contains a key called items
34+ * (whose value is an array of the requested objects) along with other keys like previous, next and
35+ * limit that can be useful in future calls.
36+ *
37+ * @property href A link to the Web API endpoint returning the full result of the request.
38+ * @property items The requested data.
39+ * @property limit The maximum number of items in the response (as set in the query or by default).
40+ * @property next URL to the next page of items. ( null if none)
41+ * @property previous URL to the previous page of items. ( null if none)
42+ * @property total The maximum number of items available to return.
43+ * @property offset The offset of the items returned (as set in the query or by default).
44+ */
3245class PagingObject <T >(
3346 href : String ,
3447 items : List <T >,
@@ -38,12 +51,18 @@ class PagingObject<T>(
3851 previous : String? ,
3952 total : Int
4053) : AbstractPagingObject<T>(href, items, limit, next, offset, previous, total) {
54+ /* *
55+ * Get the next set of [T] items
56+ */
4157 fun getNext () = endpoint.toAction(Supplier {
4258 catch {
4359 getImpl(PagingTraversalType .FORWARDS ) as ? PagingObject <T >
4460 }
4561 })
4662
63+ /* *
64+ * Get the previous set of [T] items
65+ */
4766 fun getPrevious () = endpoint.toAction(Supplier {
4867 catch {
4968 getImpl(PagingTraversalType .BACKWARDS ) as ? PagingObject <T >
@@ -52,16 +71,16 @@ class PagingObject<T>(
5271
5372 override fun getImpl (type : PagingTraversalType ): AbstractPagingObject <T >? {
5473 return (if (type == PagingTraversalType .FORWARDS ) next else previous)?.let { endpoint.get(it) }?.let { json ->
55- when {
56- itemClazz == SimpleTrack ::class .java -> json.toPagingObject<SimpleTrack >(null , endpoint)
57- itemClazz == SpotifyCategory ::class .java -> json.toPagingObject<SpotifyCategory >(null , endpoint)
58- itemClazz == SimpleAlbum ::class .java -> json.toPagingObject<SimpleAlbum >(null , endpoint)
59- itemClazz == SimplePlaylist ::class .java -> json.toPagingObject<SimplePlaylist >(null , endpoint)
60- itemClazz == SavedTrack ::class .java -> json.toPagingObject<SavedTrack >(null , endpoint)
61- itemClazz == SavedAlbum ::class .java -> json.toPagingObject<SavedAlbum >(null , endpoint)
62- itemClazz == Artist ::class .java -> json.toPagingObject<Artist >(null , endpoint)
63- itemClazz == Track ::class .java -> json.toPagingObject<Track >(null , endpoint)
64- itemClazz == PlaylistTrack ::class .java -> json.toPagingObject<PlaylistTrack >(null , endpoint)
74+ when (itemClazz) {
75+ SimpleTrack ::class .java -> json.toPagingObject<SimpleTrack >(null , endpoint)
76+ SpotifyCategory ::class .java -> json.toPagingObject<SpotifyCategory >(null , endpoint)
77+ SimpleAlbum ::class .java -> json.toPagingObject<SimpleAlbum >(null , endpoint)
78+ SimplePlaylist ::class .java -> json.toPagingObject<SimplePlaylist >(null , endpoint)
79+ SavedTrack ::class .java -> json.toPagingObject<SavedTrack >(null , endpoint)
80+ SavedAlbum ::class .java -> json.toPagingObject<SavedAlbum >(null , endpoint)
81+ Artist ::class .java -> json.toPagingObject<Artist >(null , endpoint)
82+ Track ::class .java -> json.toPagingObject<Track >(null , endpoint)
83+ PlaylistTrack ::class .java -> json.toPagingObject<PlaylistTrack >(null , endpoint)
6584 else -> throw IllegalArgumentException (" Unknown type in $href response" )
6685 } as ? PagingObject <T >
6786 }
@@ -87,28 +106,56 @@ class PagingObject<T>(
87106 return pagingObjects.asSequence()
88107 }
89108
109+ /* *
110+ * Get all PagingObjects associated with the request
111+ */
90112 fun getAll () = endpoint.toAction(Supplier { (getAllImpl() as Sequence <PagingObject <T >>).toList() })
113+
114+ /* *
115+ * Get all items of type [T] associated with the request
116+ */
91117 fun getAllItems () = endpoint.toAction(Supplier { getAll().complete().map { it.items }.flatten() })
92118}
93119
120+ /* *
121+ * The cursor-based paging object is a container for a set of objects. It contains a key called
122+ * items (whose value is an array of the requested objects) along with other keys like next and
123+ * cursors that can be useful in future calls.
124+ *
125+ * @property href A link to the Web API endpoint returning the full result of the request.
126+ * @property items The requested data.
127+ * @property limit The maximum number of items in the response (as set in the query or by default).
128+ * @property next URL to the next page of items. ( null if none)
129+ * @property total The maximum number of items available to return.
130+ * @property cursor The cursors used to find the next set of items..
131+ */
94132class CursorBasedPagingObject <T >(
95133 href : String ,
96134 items : List <T >,
97135 limit : Int ,
98136 next : String? ,
99- val cursors : Cursor ,
137+ @Json(name = " cursors" ) val cursor : Cursor ,
100138 total : Int
101139) : AbstractPagingObject<T>(href, items, limit, next, 0 , null , total) {
140+ /* *
141+ * Get the next set of [T] items
142+ */
102143 fun getNext () = endpoint.toAction(Supplier {
103144 catch {
104145 getImpl(PagingTraversalType .FORWARDS ) as ? CursorBasedPagingObject <T >
105146 }
106147 })
107148
149+ /* *
150+ * Get all CursorBasedPagingObjects associated with the request
151+ */
108152 fun getAll () = endpoint.toAction(Supplier {
109153 getAllImpl() as Sequence <CursorBasedPagingObject <T >>
110154 })
111155
156+ /* *
157+ * Get all items of type [T] associated with the request
158+ */
112159 fun getAllItems () = endpoint.toAction(Supplier {
113160 getAll().complete().map { it.items }.flatten().toList()
114161 })
@@ -138,6 +185,15 @@ class CursorBasedPagingObject<T>(
138185 }
139186}
140187
188+ /* *
189+ * @property href A link to the Web API endpoint returning the full result of the request.
190+ * @property items The requested data.
191+ * @property limit The maximum number of items in the response (as set in the query or by default).
192+ * @property next URL to the next page of items. ( null if none)
193+ * @property previous URL to the previous page of items. ( null if none)
194+ * @property total The maximum number of items available to return.
195+ * @property offset The offset of the items returned (as set in the query or by default).
196+ */
141197abstract class AbstractPagingObject <T >(
142198 val href : String ,
143199 val items : List <T >,
@@ -148,7 +204,7 @@ abstract class AbstractPagingObject<T>(
148204 val total : Int
149205) : ArrayList<T>(items) {
150206 @Json(ignored = true )
151- lateinit var endpoint: SpotifyEndpoint
207+ internal lateinit var endpoint: SpotifyEndpoint
152208
153209 @Json(ignored = true )
154210 lateinit var itemClazz: Class <T >
0 commit comments