Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Replace if-else chains with pattern matching #9303

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8640ab2
use pattern matching instead of if-else chain
SamuelRoettgermann Sep 10, 2024
4c01129
Complaint now uses pattern matching
SamuelRoettgermann Sep 10, 2024
bae5a6e
SubmissionRepository now uses pattern matching
SamuelRoettgermann Sep 10, 2024
ce46af4
SubmissionVersionService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
16699ee
ExamService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
93b4a24
StudentExamService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
3350eb4
DataExportQuizExerciseCreationService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
733d571
QuizExerciseImportService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
04c639e
QuizService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
53b5194
ComplaintResource now uses pattern matching
SamuelRoettgermann Sep 10, 2024
4bd1e24
LectureResource now uses pattern matching
SamuelRoettgermann Sep 10, 2024
67ae585
ExamUtilService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
e4c93e6
StudentExamIntegrationTest now uses pattern matching
SamuelRoettgermann Sep 10, 2024
8b028f6
AthenaRequestMockProvider now uses pattern matching
SamuelRoettgermann Sep 10, 2024
93cf232
QuizComparisonTest now uses pattern matching
SamuelRoettgermann Sep 10, 2024
13dbf4f
ParticipationUtilService now uses pattern matching
SamuelRoettgermann Sep 10, 2024
b5997ff
SubmissionExportIntegrationTest now uses pattern matching
SamuelRoettgermann Sep 10, 2024
11f9bac
Update src/main/java/de/tum/in/www1/artemis/web/rest/ComplaintResourc…
SamuelRoettgermann Sep 10, 2024
886f8b5
quick syntax fix to ComplaintResource
SamuelRoettgermann Sep 10, 2024
b496140
Update src/main/java/de/tum/in/www1/artemis/service/exam/ExamService.…
SamuelRoettgermann Sep 11, 2024
aec2a5c
additional syntax fix after accepted improvement suggestion
SamuelRoettgermann Sep 11, 2024
165ba41
StudentExamService improved readability
SamuelRoettgermann Sep 11, 2024
4a139b7
minor readability improvement
SamuelRoettgermann Sep 11, 2024
ff47d77
extracted functionality, unable to extract into a single function
SamuelRoettgermann Sep 20, 2024
3b48009
fixed a typo
SamuelRoettgermann Oct 20, 2024
91c323d
merged develop into this branch
SamuelRoettgermann Oct 20, 2024
e832686
reapplied all the changes from before the merge
SamuelRoettgermann Oct 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,14 @@ public void setComplaintResponse(ComplaintResponse complaintResponse) {
* @param participant either a team or user
*/
public void setParticipant(Participant participant) {
if (participant instanceof User) {
this.student = (User) participant;
}
else if (participant instanceof Team) {
this.team = (Team) participant;
}
else if (participant == null) {
this.student = null;
this.team = null;
}
else {
throw new Error("Unknown participant type");
switch (participant) {
case User user -> this.student = user;
case Team team1 -> this.team = team1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use consistent variable naming in switch-case patterns

In the switch statement, the variable team1 is used in case Team team1 -> this.team = team1;. For consistency and clarity, consider renaming team1 to team, aligning it with the field name this.team and improving readability.

Apply this diff to rename the variable:

             switch (participant) {
                 case User user -> this.student = user;
-                case Team team1 -> this.team = team1;
+                case Team team -> this.team = team;
                 case null -> {
                     this.student = null;
                     this.team = null;
                 }
                 default -> throw new Error("Unknown participant type");
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
case Team team1 -> this.team = team1;
case Team team -> this.team = team;

case null -> {
this.student = null;
this.team = null;
}
default -> throw new Error("Unknown participant type");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Throw an appropriate exception instead of 'Error' in the default case

Throwing an Error is generally reserved for serious system failures and is not recommended for handling unexpected input or states. Instead, use a more appropriate unchecked exception like IllegalArgumentException to indicate that the participant type is not recognized.

Apply this diff to fix the issue:

                 default -> throw new Error("Unknown participant type");
+                default -> throw new IllegalArgumentException("Unknown participant type");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
default -> throw new Error("Unknown participant type");
default -> throw new IllegalArgumentException("Unknown participant type");

}
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,36 +337,36 @@ private void filterOutUselessDataFromComplaint(Complaint complaint) {
}

Submission originalSubmission = complaint.getResult().getSubmission();
if (originalSubmission != null) {
Submission submissionWithOnlyId;
switch (originalSubmission) {
case TextSubmission ignored -> submissionWithOnlyId = new TextSubmission();
case ModelingSubmission ignored -> submissionWithOnlyId = new ModelingSubmission();
case FileUploadSubmission ignored -> submissionWithOnlyId = new FileUploadSubmission();
case ProgrammingSubmission ignored -> submissionWithOnlyId = new ProgrammingSubmission();
default -> {
return;
}
}
submissionWithOnlyId.setId(originalSubmission.getId());
complaint.getResult().setSubmission(submissionWithOnlyId);
if (originalSubmission == null) {
return;
}

Submission submissionWithOnlyId = switch (originalSubmission) {
case TextSubmission ignored -> new TextSubmission();
case ModelingSubmission ignored -> new ModelingSubmission();
case FileUploadSubmission ignored -> new FileUploadSubmission();
case ProgrammingSubmission ignored -> new ProgrammingSubmission();
default -> null;
};

if (submissionWithOnlyId == null) {
return;
}

submissionWithOnlyId.setId(originalSubmission.getId());
complaint.getResult().setSubmission(submissionWithOnlyId);
}

private static Exercise getExercise(StudentParticipation originalParticipation) {
Exercise exerciseWithOnlyTitle = originalParticipation.getExercise();
if (exerciseWithOnlyTitle instanceof TextExercise) {
exerciseWithOnlyTitle = new TextExercise();
}
else if (exerciseWithOnlyTitle instanceof ModelingExercise) {
exerciseWithOnlyTitle = new ModelingExercise();
}
else if (exerciseWithOnlyTitle instanceof FileUploadExercise) {
exerciseWithOnlyTitle = new FileUploadExercise();
}
else if (exerciseWithOnlyTitle instanceof ProgrammingExercise) {
exerciseWithOnlyTitle = new ProgrammingExercise();
switch (exerciseWithOnlyTitle) {
case TextExercise ignored -> exerciseWithOnlyTitle = new TextExercise();
case ModelingExercise ignored -> exerciseWithOnlyTitle = new ModelingExercise();
case FileUploadExercise ignored -> exerciseWithOnlyTitle = new FileUploadExercise();
case ProgrammingExercise ignored -> exerciseWithOnlyTitle = new ProgrammingExercise();
case null, default -> log.error("Invalid exercise {} passed", exerciseWithOnlyTitle.getType());
}

exerciseWithOnlyTitle.setTitle(originalParticipation.getExercise().getTitle());
exerciseWithOnlyTitle.setId(originalParticipation.getExercise().getId());
return exerciseWithOnlyTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ private boolean createQuizAnswersExport(QuizExercise quizExercise, StudentPartic
for (var question : quizQuestions) {
var submittedAnswer = quizSubmission.getSubmittedAnswerForQuestion(question);
// if this question wasn't answered, the submitted answer is null
if (submittedAnswer != null) {
if (submittedAnswer instanceof DragAndDropSubmittedAnswer dragAndDropSubmittedAnswer) {
if (submittedAnswer == null) {
// go to next question
continue;
}

switch (submittedAnswer) {
case DragAndDropSubmittedAnswer dragAndDropSubmittedAnswer -> {
try {
dragAndDropQuizAnswerConversionService.convertDragAndDropQuizAnswerAndStoreAsPdf(dragAndDropSubmittedAnswer, outputDir, includeResults);
}
Expand All @@ -111,14 +116,15 @@ private boolean createQuizAnswersExport(QuizExercise quizExercise, StudentPartic
+ quizExercise.getTitle() + " with id " + quizExercise.getId()));
}
}
else if (submittedAnswer instanceof ShortAnswerSubmittedAnswer shortAnswerSubmittedAnswer) {
case ShortAnswerSubmittedAnswer shortAnswerSubmittedAnswer ->
shortAnswerQuestionsSubmissions.add(createExportForShortAnswerQuestion(shortAnswerSubmittedAnswer, includeResults));
}
else if (submittedAnswer instanceof MultipleChoiceSubmittedAnswer multipleChoiceSubmittedAnswer) {
case MultipleChoiceSubmittedAnswer multipleChoiceSubmittedAnswer ->
multipleChoiceQuestionsSubmissions.add(createExportForMultipleChoiceAnswerQuestion(multipleChoiceSubmittedAnswer, includeResults));
default -> {
}
}
}

if (!multipleChoiceQuestionsSubmissions.isEmpty()) {
try {
FileUtils.writeLines(outputDir.resolve("quiz_submission_" + submission.getId() + "_multiple_choice_questions_answers" + TXT_FILE_EXTENSION).toFile(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,33 +893,33 @@ private Map<Long, Double> calculateAchievedPointsForExercises(List<StudentPartic
* @return true if at least one submission is not empty else false
*/
private boolean hasNonEmptySubmission(Set<Submission> submissions, Exercise exercise) {
if (exercise instanceof ProgrammingExercise) {
return submissions.stream().anyMatch(submission -> submission.getType() == SubmissionType.MANUAL);
}
else if (exercise instanceof FileUploadExercise) {
FileUploadSubmission textSubmission = (FileUploadSubmission) submissions.iterator().next();
return textSubmission.getFilePath() != null && !textSubmission.getFilePath().isEmpty();
}
else if (exercise instanceof TextExercise) {
TextSubmission textSubmission = (TextSubmission) submissions.iterator().next();
return textSubmission.getText() != null && !textSubmission.getText().isBlank();
}
else if (exercise instanceof ModelingExercise) {
ModelingSubmission modelingSubmission = (ModelingSubmission) submissions.iterator().next();
try {
return !modelingSubmission.isEmpty(this.defaultObjectMapper);
switch (exercise) {
case ProgrammingExercise ignored -> {
return submissions.stream().anyMatch(submission -> submission.getType() == SubmissionType.MANUAL);
}
case FileUploadExercise ignored -> {
FileUploadSubmission fileUploadSubmission = (FileUploadSubmission) submissions.iterator().next();
return fileUploadSubmission.getFilePath() != null && !fileUploadSubmission.getFilePath().isEmpty();
}
case TextExercise ignored -> {
TextSubmission textSubmission = (TextSubmission) submissions.iterator().next();
return textSubmission.getText() != null && !textSubmission.getText().isBlank();
}
catch (Exception e) {
// Then the student most likely submitted something which breaks the model, if parsing fails
case ModelingExercise ignored -> {
ModelingSubmission modelingSubmission = (ModelingSubmission) submissions.iterator().next();
try {
return !modelingSubmission.isEmpty(this.defaultObjectMapper);
}
catch (Exception e) {
// Then the student most likely submitted something which breaks the model, if parsing fails
return true;
}
}
case QuizExercise ignored -> {
// NOTE: due to performance concerns, this is handled differently, search for quizSubmittedAnswerCounts to find out more
return true;
}
}
else if (exercise instanceof QuizExercise) {
// NOTE: due to performance concerns, this is handled differently, search for quizSubmittedAnswerCounts to find out more
return true;
}
else {
throw new IllegalArgumentException("The exercise type of the exercise with id " + exercise.getId() + " is not supported");
case null, default -> throw new IllegalArgumentException("The exercise type of the exercise with id " + exercise.getId() + " is not supported");
}
}

Expand Down
Loading
Loading