001package horstmann.ch09_animation1;
002import java.util.Comparator;
003
004/**
005   This runnable executes a sort algorithm.
006   When two elements are compared, the algorithm
007   pauses and updates a panel.
008 */
009public class Sorter implements Runnable
010{
011        /**
012      Constructs the sorter.
013      @param values the array to sort
014      @param panel the panel for displaying the array
015         */
016        public Sorter(Double[] values, ArrayComponent panel)
017        {
018                this.values = values;
019                this.panel = panel;
020        }
021
022        public void run()
023        {
024                Comparator<Double> comp = (d1, d2) -> {
025                        panel.setValues(values, d1, d2);
026                        try
027                        {
028                                Thread.sleep(DELAY);
029                        }
030                        catch (InterruptedException exception)
031                        {
032                                Thread.currentThread().interrupt();
033                        }
034                        return (d1).compareTo(d2);
035                };
036                MergeSorter.sort(values, comp);
037                panel.setValues(values, null, null);
038        }
039
040        private Double[] values;
041        private ArrayComponent panel;
042        private static final int DELAY = 100;
043}