From e3e8615a401882a9751ce25306a24ee2578c6832 Mon Sep 17 00:00:00 2001 From: David Whitlock Date: Fri, 1 Jul 2016 19:44:49 -0700 Subject: [PATCH] Added some bullet-proofing against students who do not provide a first or last name. --- .../pdx/cs410J/grader/poa/POASubmission.java | 11 ++++++++ .../cs410J/grader/poa/StudentsPresenter.java | 28 +++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/grader/src/main/java/edu/pdx/cs410J/grader/poa/POASubmission.java b/grader/src/main/java/edu/pdx/cs410J/grader/poa/POASubmission.java index 66b8a7680..3697ea1b3 100644 --- a/grader/src/main/java/edu/pdx/cs410J/grader/poa/POASubmission.java +++ b/grader/src/main/java/edu/pdx/cs410J/grader/poa/POASubmission.java @@ -59,9 +59,20 @@ public static class Builder { private String content; public POASubmission create() { + assertNotNull("subject", subject); + assertNotNull("submitter", submitter); + assertNotNull("submitTime", submitTime); + assertNotNull("content", content); + return new POASubmission(subject, submitter, submitTime, content); } + private void assertNotNull(String description, Object object) { + if (object == null) { + throw new IllegalStateException(description + " cannot be null"); + } + } + public Builder setSubject(String subject) { this.subject = subject; return this; diff --git a/grader/src/main/java/edu/pdx/cs410J/grader/poa/StudentsPresenter.java b/grader/src/main/java/edu/pdx/cs410J/grader/poa/StudentsPresenter.java index 35d2a36c3..9bdca305c 100644 --- a/grader/src/main/java/edu/pdx/cs410J/grader/poa/StudentsPresenter.java +++ b/grader/src/main/java/edu/pdx/cs410J/grader/poa/StudentsPresenter.java @@ -62,14 +62,14 @@ public void populateViewWithStudents(GradeBookLoaded event) { private Comparator sortStudentsByLastNameFirstNameEmail() { return (student1, student2) -> { - String lastName1 = student1.getLastName(); - String lastName2 = student2.getLastName(); + String lastName1 = getLastNameOfStudent(student1); + String lastName2 = getLastNameOfStudent(student2); if (!lastName1.equals(lastName2)) { return lastName1.compareTo(lastName2); } else { - String firstName1 = student1.getFirstName(); - String firstName2 = student2.getFirstName(); + String firstName1 = getFirstNameOfStudent(student1); + String firstName2 = getFirstNameOfStudent(student2); if (!firstName1.equals(firstName2)) { return firstName1.compareTo(firstName2); @@ -80,8 +80,16 @@ private Comparator sortStudentsByLastNameFirstNameEmail() { }; } + private String getLastNameOfStudent(Student student) { + String lastName = student.getLastName(); + if (lastName == null) { + throw new IllegalStateException("Student " + student.getId() + " doesn't have a last name"); + } + return lastName; + } + private String getStudentDisplayText(Student student) { - return student.getFirstName() + " " + student.getLastName() + " <" + student.getEmail() + ">"; + return getFirstNameOfStudent(student) + " " + getLastNameOfStudent(student) + " <" + student.getEmail() + ">"; } @Subscribe @@ -109,7 +117,15 @@ private boolean submitterContainsStudentEmail(Student student, String submitter) } private boolean submitterContainsStudentName(Student student, String submitter) { - return submitter.contains(student.getFirstName()) && submitter.contains(student.getLastName()); + return submitter.contains(getFirstNameOfStudent(student)) && submitter.contains(getLastNameOfStudent(student)); + } + + private String getFirstNameOfStudent(Student student) { + String firstName = student.getFirstName(); + if (firstName == null) { + throw new IllegalStateException("Student " + student.getId() + " doesn't have a first name"); + } + return firstName; } }