77import org .lushplugins .pluginupdater .api .version .VersionDifference ;
88
99import java .io .IOException ;
10+ import java .util .ArrayDeque ;
1011import java .util .concurrent .*;
1112import java .util .logging .Level ;
1213
1314public class UpdateHandler {
1415 private final ScheduledExecutorService threads = Executors .newScheduledThreadPool (1 );
15- private final LinkedBlockingQueue <ProcessingData > queue = new LinkedBlockingQueue <>();
16+ private final ArrayDeque <ProcessingData > queue = new ArrayDeque <>();
1617
1718 public ScheduledExecutorService getThreads () {
1819 return threads ;
@@ -51,9 +52,13 @@ public void queue(ProcessingData processingData) {
5152 }
5253
5354 private void processQueue () {
54- try {
55- ProcessingData processingData = queue .take ();
56- if (processingData .getState ().equals (ProcessingData .State .UPDATE_CHECK )) {
55+ ProcessingData processingData = queue .poll ();
56+ if (processingData == null ) {
57+ return ;
58+ }
59+
60+ switch (processingData .getState ()) {
61+ case UPDATE_CHECK -> {
5762 PluginData pluginData = processingData .getPluginData ();
5863
5964 try {
@@ -67,7 +72,7 @@ private void processQueue() {
6772 String platformNames = String .join (", " , pluginData .getPlatformData ().stream ().map (PlatformData ::getName ).toList ());
6873 processingData .getFuture ().completeExceptionally (new IOException ("Failed to run check for plugin '" + pluginData .getPluginName () + "' using defined platforms: '" + platformNames + "'" ));
6974 }
70- else if ( processingData . getState (). equals ( ProcessingData . State . DOWNLOAD )) {
75+ case DOWNLOAD -> {
7176 PluginData pluginData = processingData .getPluginData ();
7277 if (!pluginData .isEnabled () || !pluginData .isUpdateAvailable () || pluginData .isAlreadyDownloaded ()) {
7378 processingData .getFuture ().complete (false );
@@ -79,6 +84,7 @@ else if (processingData.getState().equals(ProcessingData.State.DOWNLOAD)) {
7984 pluginData .setVersionDifference (VersionDifference .UNKNOWN );
8085 pluginData .setAlreadyDownloaded (true );
8186 processingData .getFuture ().complete (true );
87+ return ;
8288 } else {
8389 processingData .getFuture ().complete (false );
8490 }
@@ -89,8 +95,6 @@ else if (processingData.getState().equals(ProcessingData.State.DOWNLOAD)) {
8995 String platformNames = String .join (", " , pluginData .getPlatformData ().stream ().map (PlatformData ::getName ).toList ());
9096 processingData .getFuture ().completeExceptionally (new IOException ("Failed to download update for plugin '%s' using defined platforms: '%s'" .formatted (pluginData .getPluginName (), platformNames )));
9197 }
92- } catch (InterruptedException e ) {
93- e .printStackTrace ();
9498 }
9599 }
96100
0 commit comments