Skip to content

Commit

Permalink
rename AudioSearchSourceManager to AudioSearchManager
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Sep 6, 2023
1 parent 6f5d2d7 commit c32bfca
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
package com.github.topi314.lavasearch;

import com.github.topi314.lavasearch.result.AudioSearchResult;
import kotlin.annotations.jvm.ReadOnly;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AudioSearchManager {
public interface AudioSearchManager {

private final List<AudioSearchSourceManager> sourceManagers;

public AudioSearchManager() {
this.sourceManagers = new ArrayList<>();
}

public void registerSourceManager(AudioSearchSourceManager sourceManager) {
sourceManagers.add(sourceManager);
}

@Nullable
public <T extends AudioSearchSourceManager> T source(Class<T> klass) {
for (var sourceManager : sourceManagers) {
if (klass.isAssignableFrom(sourceManager.getClass())) {
return klass.cast(sourceManager);
}
}

return null;
}

public List<AudioSearchSourceManager> getSourceManagers() {
return this.sourceManagers;
}

public void shutdown() {
for (var sourceManager : this.sourceManagers) {
sourceManager.shutdown();
}
}
@NotNull
String getSourceName();

@Nullable
public AudioSearchResult loadSearch(String query, Set<AudioSearchResult.Type> types) {
for (var sourceManager : this.sourceManagers) {
var searchResults = sourceManager.loadSearch(query, types);
if (searchResults != null) {
return searchResults;
}
}
return null;
}
AudioSearchResult loadSearch(@NotNull String query, @NotNull @ReadOnly Set<AudioSearchResult.Type> types);

void shutdown();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.topi314.lavasearch;

import com.github.topi314.lavasearch.result.AudioSearchResult;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class SearchManager {

private final List<AudioSearchManager> sourceManagers;

public SearchManager() {
this.sourceManagers = new ArrayList<>();
}

public void registerSourceManager(AudioSearchManager sourceManager) {
sourceManagers.add(sourceManager);
}

@Nullable
public <T extends AudioSearchManager> T source(Class<T> klass) {
for (var sourceManager : sourceManagers) {
if (klass.isAssignableFrom(sourceManager.getClass())) {
return klass.cast(sourceManager);
}
}

return null;
}

public List<AudioSearchManager> getSourceManagers() {
return this.sourceManagers;
}

public void shutdown() {
for (var sourceManager : this.sourceManagers) {
sourceManager.shutdown();
}
}

@Nullable
public AudioSearchResult loadSearch(String query, Set<AudioSearchResult.Type> types) {
for (var sourceManager : this.sourceManagers) {
var searchResults = sourceManager.loadSearch(query, types);
if (searchResults != null) {
return searchResults;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.topi314.lavasearch.api

import com.github.topi314.lavasearch.AudioSearchManager
import com.github.topi314.lavasearch.SearchManager

interface SearchManagerConfiguration {
fun configure(manager: AudioSearchManager): AudioSearchManager
fun configure(manager: SearchManager): SearchManager
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.topi314.lavasearch.plugin

import com.github.topi314.lavasearch.result.AudioSearchResult
import com.github.topi314.lavasearch.AudioSearchManager
import com.github.topi314.lavasearch.SearchManager
import com.github.topi314.lavasearch.api.SearchPluginInfoModifier
import com.github.topi314.lavasearch.protocol.SearchResult
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager
Expand All @@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController

@RestController
class AudioSearchRestHandler(
private val audioSearchManager: AudioSearchManager,
private val searchManager: SearchManager,
private val playerManager: AudioPlayerManager,
private val pluginInfoModifiers: List<AudioPluginInfoModifier>,
private val searchResultInfoModifiers: List<SearchPluginInfoModifier>
Expand All @@ -33,7 +33,7 @@ class AudioSearchRestHandler(
): ResponseEntity<SearchResult> {
log.debug("loadSearch called with query: {}, types: {}", query, types)
val finalTypes = if (types.isNullOrBlank()) emptySet() else types.split(""",\s*""".toRegex()).mapNotNull { AudioSearchResult.Type.fromName(it) }.toSet()
val result = audioSearchManager.loadSearch(query, finalTypes)
val result = searchManager.loadSearch(query, finalTypes)
return if (result != null) {
ResponseEntity.ok(result.toSearchResult(playerManager, pluginInfoModifiers, searchResultInfoModifiers))
} else ResponseEntity.notFound().build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.topi314.lavasearch.plugin;

import com.github.topi314.lavasearch.AudioSearchManager;
import com.github.topi314.lavasearch.SearchManager;
import com.github.topi314.lavasearch.api.SearchManagerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -11,8 +11,8 @@
public class SearchConfiguration {

@Bean
public AudioSearchManager audioSearchManagerSupplier(Collection<SearchManagerConfiguration> searchManagerConfigurations) {
var manager = new AudioSearchManager();
public SearchManager audioSearchManagerSupplier(Collection<SearchManagerConfiguration> searchManagerConfigurations) {
var manager = new SearchManager();

for (var config : searchManagerConfigurations) {
manager = config.configure(manager);
Expand Down

0 comments on commit c32bfca

Please sign in to comment.