Java 11 migrate c-d (remaining) (#1111)

* Moves converter pattern to Java 11

* Moves cqrs pattern to Java 11

* Moves dao pattern to Java 11

* Moves data-bus pattern to Java 11

* Moves data-locality pattern to Java 11

* Moves data-mapper pattern to Java 11

* Moves data-transfer-object pattern to Java 11

* Moves decorator pattern to Java 11

* Moves delegation pattern to Java 11

* Moves dependency-injection to Java 11

* Moves dirty-flag to Java 11

* Moves double-buffer to Java 11

* Moves double-checked-locking to Java 11

* Moves double-dispatch to Java 11

* Corrects with changes thats breaking test cases
This commit is contained in:
Anurag Agarwal
2019-12-15 00:02:45 +05:30
committed by Ilkka Seppälä
parent 5681684157
commit ea57934db6
75 changed files with 576 additions and 713 deletions

View File

@ -37,71 +37,36 @@ public final class StudentDataMapperImpl implements StudentDataMapper {
@Override
public Optional<Student> find(int studentId) {
/* Compare with existing students */
for (final Student student : this.getStudents()) {
/* Check if student is found */
if (student.getStudentId() == studentId) {
return Optional.of(student);
}
}
/* Return empty value */
return Optional.empty();
return this.getStudents().stream().filter(x -> x.getStudentId() == studentId).findFirst();
}
@Override
public void update(Student studentToBeUpdated) throws DataMapperException {
/* Check with existing students */
if (this.getStudents().contains(studentToBeUpdated)) {
/* Get the index of student in list */
final int index = this.getStudents().indexOf(studentToBeUpdated);
/* Update the student in list */
this.getStudents().set(index, studentToBeUpdated);
} else {
/* Throw user error after wrapping in a runtime exception */
throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found");
}
String name = studentToBeUpdated.getName();
Integer index = Optional.of(studentToBeUpdated)
.map(Student::getStudentId)
.flatMap(this::find)
.map(students::indexOf)
.orElseThrow(() -> new DataMapperException("Student [" + name + "] is not found"));
students.set(index, studentToBeUpdated);
}
@Override
public void insert(Student studentToBeInserted) throws DataMapperException {
/* Check with existing students */
if (!this.getStudents().contains(studentToBeInserted)) {
/* Add student in list */
this.getStudents().add(studentToBeInserted);
} else {
/* Throw user error after wrapping in a runtime exception */
throw new DataMapperException("Student already [" + studentToBeInserted
.getName() + "] exists");
Optional<Student> student = find(studentToBeInserted.getStudentId());
if (student.isPresent()) {
String name = studentToBeInserted.getName();
throw new DataMapperException("Student already [" + name + "] exists");
}
students.add(studentToBeInserted);
}
@Override
public void delete(Student studentToBeDeleted) throws DataMapperException {
/* Check with existing students */
if (this.getStudents().contains(studentToBeDeleted)) {
/* Delete the student from list */
this.getStudents().remove(studentToBeDeleted);
} else {
/* Throw user error after wrapping in a runtime exception */
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
if (!students.remove(studentToBeDeleted)) {
String name = studentToBeDeleted.getName();
throw new DataMapperException("Student [" + name + "] is not found");
}
}