You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SupplierLoggingSwingWorker.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components;
  23. import java.util.List;
  24. import java.util.concurrent.ExecutionException;
  25. import java.util.function.Consumer;
  26. import java.util.function.Supplier;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import static com.dmdirc.util.LogUtils.USER_ERROR;
  30. /**
  31. * {@link LoggingSwingWorker} that runs a {@link Supplier}.
  32. */
  33. public class SupplierLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
  34. private static final Logger LOG = LoggerFactory.getLogger(SupplierLoggingSwingWorker.class);
  35. private final Consumer<T> doneConsumer;
  36. private final Consumer<List<V>> processConsumer;
  37. private final Supplier<T> backgroundSupplier;
  38. /**
  39. * Creates a new logging swing worker.
  40. *
  41. * @param backgroundSupplier The supplier to call as the background task, off the EDT.
  42. */
  43. public SupplierLoggingSwingWorker(
  44. final Supplier<T> backgroundSupplier) {
  45. this(backgroundSupplier, result -> {});
  46. }
  47. /**
  48. * Creates a new logging swing worker.
  49. *
  50. * @param backgroundSupplier The supplier to call as the background task, off the EDT.
  51. * @param doneConsumer The consumer called when the background task is complete
  52. */
  53. public SupplierLoggingSwingWorker(
  54. final Supplier<T> backgroundSupplier,
  55. final Consumer<T> doneConsumer) {
  56. this(backgroundSupplier, doneConsumer, chunks -> {});
  57. }
  58. /**
  59. * Creates a new logging swing worker.
  60. *
  61. * @param backgroundSupplier The supplier to call as the background task, off the EDT.
  62. * @param doneConsumer The consumer called when the background task is complete
  63. * @param processConsumer The consumer called to process results of the background task
  64. * as it progresses
  65. */
  66. public SupplierLoggingSwingWorker(
  67. final Supplier<T> backgroundSupplier,
  68. final Consumer<T> doneConsumer,
  69. final Consumer<List<V>> processConsumer) {
  70. this.backgroundSupplier = backgroundSupplier;
  71. this.doneConsumer = doneConsumer;
  72. this.processConsumer = processConsumer;
  73. }
  74. @Override
  75. protected T doInBackground() throws Exception {
  76. return backgroundSupplier.get();
  77. }
  78. @Override
  79. protected void process(final List<V> chunks) {
  80. processConsumer.accept(chunks);
  81. }
  82. @Override
  83. protected void done() {
  84. try {
  85. handleDone(get());
  86. } catch (InterruptedException ex) {
  87. //Ignore
  88. } catch (ExecutionException ex) {
  89. LOG.warn(USER_ERROR, ex.getMessage(), ex);
  90. }
  91. }
  92. public void handleDone(final T value) {
  93. doneConsumer.accept(value);
  94. }
  95. }