Skip to content

Commit 34552b2

Browse files
committed
add SpotifyUri.isUriType to let users easily check if a string is a uri of type
1 parent a414d18 commit 34552b2

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/main/kotlin/com/adamratzman/spotify/models/SpotifyUris.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private fun String.remove(type: String): String {
2727
* @property uri retrieve this URI as a string
2828
* @property id representation of this uri as an id
2929
*/
30-
sealed class SpotifyUri(input: String, type: String) {
30+
sealed class SpotifyUri(input: String, type: UriType) {
3131
val uri: String
3232
val id: String
3333

@@ -37,8 +37,8 @@ sealed class SpotifyUri(input: String, type: String) {
3737
this.uri = input
3838
this.id = input
3939
} else {
40-
this.uri = it.add(type)
41-
this.id = it.remove(type)
40+
this.uri = it.add(type.toString())
41+
this.id = it.remove(type.toString())
4242
}
4343
}
4444
}
@@ -53,34 +53,49 @@ sealed class SpotifyUri(input: String, type: String) {
5353
result = 31 * result + id.hashCode()
5454
return result
5555
}
56+
57+
enum class UriType(private val typeStr: String) {
58+
ALBUM("album"),
59+
ARTIST("artist"),
60+
TRACK("track"),
61+
USER("user"),
62+
PLAYLIST("playlist"),
63+
LOCAL_TRACK("local");
64+
65+
override fun toString() = typeStr
66+
}
67+
68+
companion object {
69+
fun isUriType(uri: String, type: UriType) = uri.matchType(type.toString()) != null
70+
}
5671
}
5772

5873
/**
5974
* Represents a Spotify **Album** URI, parsed from either a Spotify ID or taken from an endpoint.
6075
*/
61-
class AlbumURI(input: String) : SpotifyUri(input, "album")
76+
class AlbumURI(input: String) : SpotifyUri(input, UriType.ALBUM)
6277

6378
/**
6479
* Represents a Spotify **Artist** URI, parsed from either a Spotify ID or taken from an endpoint.
6580
*/
66-
class ArtistURI(input: String) : SpotifyUri(input, "artist")
81+
class ArtistURI(input: String) : SpotifyUri(input, UriType.ARTIST)
6782

6883
/**
6984
* Represents a Spotify **Track** URI, parsed from either a Spotify ID or taken from an endpoint.
7085
*/
71-
class TrackURI(input: String) : SpotifyUri(input, "track")
86+
class TrackURI(input: String) : SpotifyUri(input, UriType.TRACK)
7287

7388
/**
7489
* Represents a Spotify **User** URI, parsed from either a Spotify ID or taken from an endpoint.
7590
*/
76-
class UserURI(input: String) : SpotifyUri(input, "user")
91+
class UserURI(input: String) : SpotifyUri(input, UriType.USER)
7792

7893
/**
7994
* Represents a Spotify **Playlist** URI, parsed from either a Spotify ID or taken from an endpoint.
8095
*/
81-
class PlaylistURI(input: String) : SpotifyUri(input, "playlist")
96+
class PlaylistURI(input: String) : SpotifyUri(input, UriType.PLAYLIST)
8297

8398
/**
8499
* Represents a Spotify **local track** URI
85100
*/
86-
class LocalTrackURI(input: String) : SpotifyUri(input, "local")
101+
class LocalTrackURI(input: String) : SpotifyUri(input, UriType.LOCAL_TRACK)

0 commit comments

Comments
 (0)