Class Sorter
java.lang.Object
com.github.rlacher.sortbench.sorter.Sorter
Context class for sorting algorithms, utilising the Strategy design pattern.
This class encapsulates the sorting logic, delegating the actual sorting operation to a SortStrategy.
This allows for dynamic switching of sorting algorithms at runtime.
For simplicity at this project scale, the Sorter class directly acts as the Strategy pattern's context, rather than using a separate interface.
The sorter centralises argument validation to ensure data integrity.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidsetStrategy(SortStrategy sortStrategy) Sets the sorting strategy to be used.sort(int[] array) Validates the input array and delegates the sorting task to the currently setSortStrategy.
-
Constructor Details
-
Sorter
public Sorter()Constructs a default instance ofSorter.This constructor performs no custom initialisation.
-
-
Method Details
-
setStrategy
Sets the sorting strategy to be used.- Parameters:
sortStrategy- The sorting strategy to be used.- Throws:
IllegalArgumentException- If the sort strategy isnull.
-
sort
Validates the input array and delegates the sorting task to the currently setSortStrategy.Requires a prior call to
setStrategy(com.github.rlacher.sortbench.strategies.SortStrategy).- Parameters:
array- The array to be sorted.- Returns:
- Returns a
BenchmarkMetricwith the metric value for theBenchmarker.ProfilingMode. Returns 0 if the array is empty. - Throws:
IllegalArgumentException- If the array isnull.IllegalStateException- If the sort strategy isnull.
-