Skip to content

Commit

Permalink
Merge pull request #7 from espertus/order-results
Browse files Browse the repository at this point in the history
Enable ordering results
  • Loading branch information
espertus authored Dec 16, 2023
2 parents d87554e + 07dd978 commit 9a4f2f9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
52 changes: 52 additions & 0 deletions src/main/java/com/spertus/jacquard/common/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,56 @@ public static void changeVisibility(
final List<Result> results, final Visibility visibility) {
results.forEach((Result r) -> r.setVisibility(visibility));
}

/**
* The publication order for results. The default is {@link #NATURAL}.
*
* @see {@link #reorderResults(List, Order)}
*/
public enum Order {
/**
* The order in which they were generated. In other words,
* this will not reorder elements.
*/
// This relies on Collection.sort() being stable, per API
// https://stackoverflow.com/a/44452446/631051
NATURAL((r1, r2) -> 0),

/**
* Alphabetical order by result name.
*/
ALPHABETICAL(Comparator.comparing(r -> r.name)),

/**
* In increasing order by maximum score.
*/
INCREASING_MAX_SCORE(Comparator.comparing(r -> r.maxScore)),

/**
* In decreasing order by maximum score.
*/
DECREASING_MAX_SCORE(Comparator.comparing(r -> -r.maxScore));

private Comparator<Result> comparator;

Order(Comparator<Result> comparator) {
this.comparator = comparator;
}
}

/**
* Produces a sorted copy of the results.
*
* @param results the results
* @param order the ordering
* @return the sorted result list
*/
public static List<Result> reorderResults(List<Result> results, Order order) {
// This copies the list rather than sorting in place for two reasons:
// 1. The list parameter might be immutable.
// 2. To avoid side effects.
List<Result> mutableResults = new ArrayList<>(results);
Collections.sort(mutableResults, order.comparator);
return mutableResults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public String serializeResults(final List<Result> results) {
}

@Override
public boolean publishResults(final List<Result> results) {
public boolean publishResults(final List<Result> results, Result.Order order) {
try {
if (Files.exists(RESULTS_PATH)) {
Files.write(RESULTS_PATH.resolve(RESULTS_FILE_NAME), serializeResults(results).getBytes());
List<Result> sortedResults = Result.reorderResults(results, order);
Files.write(
RESULTS_PATH.resolve(RESULTS_FILE_NAME),
serializeResults(sortedResults).getBytes());
return true;
}
} catch (IOException e) {
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/spertus/jacquard/publisher/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ public abstract class Publisher {
* @param results the results
* @return whether the results were successfully published
*/
public abstract boolean publishResults(List<Result> results);
public boolean publishResults(List<Result> results) {
return publishResults(results, Result.Order.NATURAL);
}

/**
* Publishes the results in a manner appropriate for the external grading
* tool. This does nothing (and returns {@code false}) if it detects that
* it is not running within the tool (i.e., is on a development machine).
*
* @param results the results
* @param order how to order the results
* @return whether the results were successfully published
*/
public abstract boolean publishResults(List<Result> results, Result.Order order);

/**
* Displays the results in a human-readable format.
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/spertus/jacquard/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
import com.spertus.jacquard.common.*;
import org.junit.jupiter.api.*;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ResultTest {
private Result rBigSuccess;
private Result rHugeFailure;
private Result rMixed;
private List<Result> results;

@BeforeEach
public void setup() {
Autograder.initForTest();
rBigSuccess = Result.makeSuccess("big success", 10, "big success message");
rHugeFailure = Result.makeFailure("huge Failure", 15, "huge failure message");
rMixed = Result.makeResult("mixed success", 3.0, 6.0, "mixed success message");
results = List.of(rBigSuccess, rHugeFailure, rMixed);
}

@Test
Expand All @@ -33,4 +44,27 @@ public void testChangedDefaultVisibility() {
Autograder.resetForTest(); // keep from affecting downstream tests
}
}

@Test
public void testReorderNaturally() {
assertEquals(
List.of(rBigSuccess, rHugeFailure, rMixed),
Result.reorderResults(results, Result.Order.NATURAL)
);
}

@Test
public void testReorderAlphabetically() {
assertEquals(List.of(rBigSuccess, rHugeFailure, rMixed), Result.reorderResults(results, Result.Order.ALPHABETICAL));
}

@Test
public void testReorderDecreasingMaxScore() {
assertEquals(List.of(rHugeFailure, rBigSuccess, rMixed), Result.reorderResults(results, Result.Order.DECREASING_MAX_SCORE));
}

@Test
public void testReorderIncreasingMaxScore() {
assertEquals(List.of(rMixed, rBigSuccess, rHugeFailure), Result.reorderResults(results, Result.Order.INCREASING_MAX_SCORE));
}
}

0 comments on commit 9a4f2f9

Please sign in to comment.