forked from freyacodes/Lavalink-Client
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/java/lavalink/client/io/FunctionalResultHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package lavalink.client.io; | ||
|
||
import lavalink.client.player.track.AudioPlaylist; | ||
import lavalink.client.player.track.AudioTrack; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Helper class for creating an audio result handler using only methods that can be passed as lambdas. | ||
*/ | ||
public class FunctionalResultHandler implements LoadResultHandler { | ||
private final Consumer<AudioTrack> trackConsumer; | ||
private final Consumer<AudioPlaylist> playlistConsumer; | ||
private final Consumer<List<AudioTrack>> searchResultConsumer; | ||
private final Runnable emptyResultHandler; | ||
private final Consumer<FriendlyException> exceptionConsumer; | ||
|
||
/** | ||
* Refer to {@link LoadResultHandler} methods for details on when each method is called. | ||
* | ||
* @param trackConsumer Consumer for single track result | ||
* @param playlistConsumer Consumer for playlist result | ||
* @param searchResultConsumer Consumer for search result | ||
* @param emptyResultHandler Empty result handler | ||
* @param exceptionConsumer Consumer for an exception when loading the item fails | ||
*/ | ||
public FunctionalResultHandler(Consumer<AudioTrack> trackConsumer, Consumer<AudioPlaylist> playlistConsumer, Consumer<List<AudioTrack>> searchResultConsumer, Runnable emptyResultHandler, Consumer<FriendlyException> exceptionConsumer) { | ||
this.trackConsumer = trackConsumer; | ||
this.playlistConsumer = playlistConsumer; | ||
this.searchResultConsumer = searchResultConsumer; | ||
this.emptyResultHandler = emptyResultHandler; | ||
this.exceptionConsumer = exceptionConsumer; | ||
} | ||
|
||
@Override | ||
public void trackLoaded(AudioTrack track) { | ||
if (trackConsumer != null) { | ||
trackConsumer.accept(track); | ||
} | ||
} | ||
|
||
@Override | ||
public void playlistLoaded(AudioPlaylist playlist) { | ||
if (playlistConsumer != null) { | ||
playlistConsumer.accept(playlist); | ||
} | ||
} | ||
|
||
@Override | ||
public void searchResultLoaded(List<AudioTrack> tracks) { | ||
if (searchResultConsumer != null) { | ||
searchResultConsumer.accept(tracks); | ||
} | ||
} | ||
|
||
@Override | ||
public void noMatches() { | ||
if (emptyResultHandler != null) { | ||
emptyResultHandler.run(); | ||
} | ||
} | ||
|
||
@Override | ||
public void loadFailed(FriendlyException exception) { | ||
if (exceptionConsumer != null) { | ||
exceptionConsumer.accept(exception); | ||
} | ||
} | ||
} | ||
|