Skip to content

Commit 004cbc6

Browse files
committed
Updated all examples to use CompletableFuture rather than Future.
1 parent a16123b commit 004cbc6

74 files changed

Lines changed: 706 additions & 464 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎examples/authorization/authorization_code/AuthorizationCodeExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import java.io.IOException;
1010
import java.net.URI;
11-
import java.util.concurrent.ExecutionException;
12-
import java.util.concurrent.Future;
11+
import java.util.concurrent.CancellationException;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.CompletionException;
1314

1415
public class AuthorizationCodeExample {
1516
private static final String clientId = "zyuxhfo1c51b5hxjk09x2uhv5n0svgd6g";
@@ -41,19 +42,22 @@ public static void authorizationCode_Sync() {
4142

4243
public static void authorizationCode_Async() {
4344
try {
44-
final Future<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRequest.executeAsync();
45+
final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRequest.executeAsync();
4546

46-
// ...
47+
// Thread free to do other tasks...
4748

48-
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.get();
49+
// Example Only. Never block in production code.
50+
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.join();
4951

5052
// Set access and refresh token for further "spotifyApi" object usage
5153
spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
5254
spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
5355

5456
System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
55-
} catch (InterruptedException | ExecutionException e) {
57+
} catch (CompletionException e) {
5658
System.out.println("Error: " + e.getCause().getMessage());
59+
} catch (CancellationException e) {
60+
System.out.println("Async operation cancelled.");
5761
}
5862
}
5963
}

‎examples/authorization/authorization_code/AuthorizationCodeRefreshExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import com.wrapper.spotify.requests.authorization.authorization_code.AuthorizationCodeRefreshRequest;
77

88
import java.io.IOException;
9-
import java.util.concurrent.ExecutionException;
10-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1112

1213
public class AuthorizationCodeRefreshExample {
1314
private static final String clientId = "zyuxhfo1c51b5hxjk09x2uhv5n0svgd6g";
@@ -38,18 +39,21 @@ public static void authorizationCodeRefresh_Sync() {
3839

3940
public static void authorizationCodeRefresh_Async() {
4041
try {
41-
final Future<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRefreshRequest.executeAsync();
42+
final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRefreshRequest.executeAsync();
4243

43-
// ...
44+
// Thread free to do other tasks...
4445

45-
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.get();
46+
// Example Only. Never block in production code.
47+
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.join();
4648

4749
// Set access token for further "spotifyApi" object usage
4850
spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
4951

5052
System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
51-
} catch (InterruptedException | ExecutionException e) {
53+
} catch (CompletionException e) {
5254
System.out.println("Error: " + e.getCause().getMessage());
55+
} catch (CancellationException e) {
56+
System.out.println("Async operation cancelled.");
5357
}
5458
}
5559
}

‎examples/authorization/authorization_code/AuthorizationCodeUriExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import com.wrapper.spotify.requests.authorization.authorization_code.AuthorizationCodeUriRequest;
66

77
import java.net.URI;
8-
import java.util.concurrent.ExecutionException;
9-
import java.util.concurrent.Future;
8+
import java.util.concurrent.CancellationException;
9+
import java.util.concurrent.CompletableFuture;
10+
import java.util.concurrent.CompletionException;
1011

1112
public class AuthorizationCodeUriExample {
1213
private static final String clientId = "zyuxhfo1c51b5hxjk09x2uhv5n0svgd6g";
@@ -32,15 +33,18 @@ public static void authorizationCodeUri_Sync() {
3233

3334
public static void authorizationCodeUri_Async() {
3435
try {
35-
final Future<URI> uriFuture = authorizationCodeUriRequest.executeAsync();
36+
final CompletableFuture<URI> uriFuture = authorizationCodeUriRequest.executeAsync();
3637

37-
// ...
38+
// Thread free to do other tasks...
3839

39-
final URI uri = uriFuture.get();
40+
// Example Only. Never block in production code.
41+
final URI uri = uriFuture.join();
4042

4143
System.out.println("URI: " + uri.toString());
42-
} catch (InterruptedException | ExecutionException e) {
44+
} catch (CompletionException e) {
4345
System.out.println("Error: " + e.getCause().getMessage());
46+
} catch (CancellationException e) {
47+
System.out.println("Async operation cancelled.");
4448
}
4549
}
4650
}

‎examples/authorization/client_credentials/ClientCredentialsExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest;
77

88
import java.io.IOException;
9-
import java.util.concurrent.ExecutionException;
10-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1112

1213
public class ClientCredentialsExample {
1314
private static final String clientId = "zyuxhfo1c51b5hxjk09x2uhv5n0svgd6g";
@@ -35,18 +36,21 @@ public static void clientCredentials_Sync() {
3536

3637
public static void clientCredentials_Async() {
3738
try {
38-
final Future<ClientCredentials> clientCredentialsFuture = clientCredentialsRequest.executeAsync();
39+
final CompletableFuture<ClientCredentials> clientCredentialsFuture = clientCredentialsRequest.executeAsync();
3940

40-
// ...
41+
// Thread free to do other tasks...
4142

42-
final ClientCredentials clientCredentials = clientCredentialsFuture.get();
43+
// Example Only. Never block in production code.
44+
final ClientCredentials clientCredentials = clientCredentialsFuture.join();
4345

4446
// Set access token for further "spotifyApi" object usage
4547
spotifyApi.setAccessToken(clientCredentials.getAccessToken());
4648

4749
System.out.println("Expires in: " + clientCredentials.getExpiresIn());
48-
} catch (InterruptedException | ExecutionException e) {
50+
} catch (CompletionException e) {
4951
System.out.println("Error: " + e.getCause().getMessage());
52+
} catch (CancellationException e) {
53+
System.out.println("Async operation cancelled.");
5054
}
5155
}
5256
}

‎examples/data/albums/GetAlbumExample.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package data.albums;
22

3-
import com.neovisionaries.i18n.CountryCode;
43
import com.wrapper.spotify.SpotifyApi;
54
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
65
import com.wrapper.spotify.model_objects.specification.Album;
76
import com.wrapper.spotify.requests.data.albums.GetAlbumRequest;
87

98
import java.io.IOException;
10-
import java.util.concurrent.ExecutionException;
11-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1212

1313
public class GetAlbumExample {
1414
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -33,15 +33,18 @@ public static void getAlbum_Sync() {
3333

3434
public static void getAlbum_Async() {
3535
try {
36-
final Future<Album> albumFuture = getAlbumRequest.executeAsync();
36+
final CompletableFuture<Album> albumFuture = getAlbumRequest.executeAsync();
3737

38-
// ...
38+
// Thread free to do other tasks...
3939

40-
final Album album = albumFuture.get();
40+
// Example Only. Never block in production code.
41+
final Album album = albumFuture.join();
4142

4243
System.out.println("Name: " + album.getName());
43-
} catch (InterruptedException | ExecutionException e) {
44+
} catch (CompletionException e) {
4445
System.out.println("Error: " + e.getCause().getMessage());
46+
} catch (CancellationException e) {
47+
System.out.println("Async operation cancelled.");
4548
}
4649
}
4750
}

‎examples/data/albums/GetAlbumsTracksExample.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package data.albums;
22

3-
import com.neovisionaries.i18n.CountryCode;
43
import com.wrapper.spotify.SpotifyApi;
54
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
65
import com.wrapper.spotify.model_objects.specification.Paging;
76
import com.wrapper.spotify.model_objects.specification.TrackSimplified;
87
import com.wrapper.spotify.requests.data.albums.GetAlbumsTracksRequest;
98

109
import java.io.IOException;
11-
import java.util.concurrent.ExecutionException;
12-
import java.util.concurrent.Future;
10+
import java.util.concurrent.CancellationException;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.CompletionException;
1313

1414
public class GetAlbumsTracksExample {
1515
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -36,15 +36,18 @@ public static void getAlbumsTracks_Sync() {
3636

3737
public static void getAlbumsTracks_Async() {
3838
try {
39-
final Future<Paging<TrackSimplified>> pagingFuture = getAlbumsTracksRequest.executeAsync();
39+
final CompletableFuture<Paging<TrackSimplified>> pagingFuture = getAlbumsTracksRequest.executeAsync();
4040

41-
// ...
41+
// Thread free to do other tasks...
4242

43-
final Paging<TrackSimplified> trackSimplifiedPaging = pagingFuture.get();
43+
// Example Only. Never block in production code.
44+
final Paging<TrackSimplified> trackSimplifiedPaging = pagingFuture.join();
4445

4546
System.out.println("Total: " + trackSimplifiedPaging.getTotal());
46-
} catch (InterruptedException | ExecutionException e) {
47+
} catch (CompletionException e) {
4748
System.out.println("Error: " + e.getCause().getMessage());
49+
} catch (CancellationException e) {
50+
System.out.println("Async operation cancelled.");
4851
}
4952
}
5053
}

‎examples/data/albums/GetSeveralAlbumsExample.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package data.albums;
22

3-
import com.neovisionaries.i18n.CountryCode;
43
import com.wrapper.spotify.SpotifyApi;
54
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
65
import com.wrapper.spotify.model_objects.specification.Album;
76
import com.wrapper.spotify.requests.data.albums.GetSeveralAlbumsRequest;
87

98
import java.io.IOException;
10-
import java.util.concurrent.ExecutionException;
11-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1212

1313
public class GetSeveralAlbumsExample {
1414
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -33,15 +33,18 @@ public static void getSeveralAlbums_Sync() {
3333

3434
public static void getSeveralAlbums_Async() {
3535
try {
36-
final Future<Album[]> albumsFuture = getSeveralAlbumsRequest.executeAsync();
36+
final CompletableFuture<Album[]> albumsFuture = getSeveralAlbumsRequest.executeAsync();
3737

38-
// ...
38+
// Thread free to do other tasks...
3939

40-
final Album[] albums = albumsFuture.get();
40+
// Example Only. Never block in production code.
41+
final Album[] albums = albumsFuture.join();
4142

4243
System.out.println("Length: " + albums.length);
43-
} catch (InterruptedException | ExecutionException e) {
44+
} catch (CompletionException e) {
4445
System.out.println("Error: " + e.getCause().getMessage());
46+
} catch (CancellationException e) {
47+
System.out.println("Async operation cancelled.");
4548
}
4649
}
4750
}

‎examples/data/artists/GetArtistExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import com.wrapper.spotify.requests.data.artists.GetArtistRequest;
77

88
import java.io.IOException;
9-
import java.util.concurrent.ExecutionException;
10-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1112

1213
public class GetArtistExample {
1314
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -31,15 +32,18 @@ public static void getArtist_Sync() {
3132

3233
public static void getArtist_Async() {
3334
try {
34-
final Future<Artist> albumFuture = getArtistRequest.executeAsync();
35+
final CompletableFuture<Artist> albumFuture = getArtistRequest.executeAsync();
3536

36-
// ...
37+
// Thread free to do other tasks...
3738

38-
final Artist artist = albumFuture.get();
39+
// Example Only. Never block in production code.
40+
final Artist artist = albumFuture.join();
3941

4042
System.out.println("Name: " + artist.getName());
41-
} catch (InterruptedException | ExecutionException e) {
43+
} catch (CompletionException e) {
4244
System.out.println("Error: " + e.getCause().getMessage());
45+
} catch (CancellationException e) {
46+
System.out.println("Async operation cancelled.");
4347
}
4448
}
4549
}

‎examples/data/artists/GetArtistsAlbumsExample.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package data.artists;
22

3-
import com.neovisionaries.i18n.CountryCode;
43
import com.wrapper.spotify.SpotifyApi;
54
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
65
import com.wrapper.spotify.model_objects.specification.AlbumSimplified;
76
import com.wrapper.spotify.model_objects.specification.Paging;
87
import com.wrapper.spotify.requests.data.artists.GetArtistsAlbumsRequest;
98

109
import java.io.IOException;
11-
import java.util.concurrent.ExecutionException;
12-
import java.util.concurrent.Future;
10+
import java.util.concurrent.CancellationException;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.CompletionException;
1313

1414
public class GetArtistsAlbumsExample {
1515
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -37,15 +37,18 @@ public static void getArtistsAlbums_Sync() {
3737

3838
public static void getArtistsAlbums_Async() {
3939
try {
40-
final Future<Paging<AlbumSimplified>> pagingFuture = getArtistsAlbumsRequest.executeAsync();
40+
final CompletableFuture<Paging<AlbumSimplified>> pagingFuture = getArtistsAlbumsRequest.executeAsync();
4141

42-
// ...
42+
// Thread free to do other tasks...
4343

44-
final Paging<AlbumSimplified> albumSimplifiedPaging = pagingFuture.get();
44+
// Example Only. Never block in production code.
45+
final Paging<AlbumSimplified> albumSimplifiedPaging = pagingFuture.join();
4546

4647
System.out.println("Total: " + albumSimplifiedPaging.getTotal());
47-
} catch (InterruptedException | ExecutionException e) {
48+
} catch (CompletionException e) {
4849
System.out.println("Error: " + e.getCause().getMessage());
50+
} catch (CancellationException e) {
51+
System.out.println("Async operation cancelled.");
4952
}
5053
}
5154
}

‎examples/data/artists/GetArtistsRelatedArtistsExample.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import com.wrapper.spotify.requests.data.artists.GetArtistsRelatedArtistsRequest;
77

88
import java.io.IOException;
9-
import java.util.concurrent.ExecutionException;
10-
import java.util.concurrent.Future;
9+
import java.util.concurrent.CancellationException;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.CompletionException;
1112

1213
public class GetArtistsRelatedArtistsExample {
1314
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
@@ -32,15 +33,18 @@ public static void getArtistsRelatedArtists_Sync() {
3233

3334
public static void getArtistsRelatedArtists_Async() {
3435
try {
35-
final Future<Artist[]> artistsFuture = getArtistsRelatedArtistsRequest.executeAsync();
36+
final CompletableFuture<Artist[]> artistsFuture = getArtistsRelatedArtistsRequest.executeAsync();
3637

37-
// ...
38+
// Thread free to do other tasks...
3839

39-
final Artist[] artists = artistsFuture.get();
40+
// Example Only. Never block in production code.
41+
final Artist[] artists = artistsFuture.join();
4042

4143
System.out.println("Length: " + artists.length);
42-
} catch (InterruptedException | ExecutionException e) {
44+
} catch (CompletionException e) {
4345
System.out.println("Error: " + e.getCause().getMessage());
46+
} catch (CancellationException e) {
47+
System.out.println("Async operation cancelled.");
4448
}
4549
}
4650
}

0 commit comments

Comments
 (0)