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

@ -23,7 +23,6 @@
package com.iluwatar.datamapper;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -51,10 +50,10 @@ public final class App {
public static void main(final String... args) {
/* Create new data mapper for type 'first' */
final StudentDataMapper mapper = new StudentDataMapperImpl();
final var mapper = new StudentDataMapperImpl();
/* Create new student */
Student student = new Student(1, "Adam", 'A');
var student = new Student(1, "Adam", 'A');
/* Add student in respectibe store */
mapper.insert(student);
@ -62,7 +61,7 @@ public final class App {
log.debug(STUDENT_STRING + student + ", is inserted");
/* Find this student */
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
final var studentToBeFound = mapper.find(student.getStudentId());
log.debug(STUDENT_STRING + studentToBeFound + ", is searched");

View File

@ -85,7 +85,7 @@ public final class Student implements Serializable {
isEqual = true;
} else if (inputObject != null && getClass() == inputObject.getClass()) {
final Student inputStudent = (Student) inputObject;
final var inputStudent = (Student) inputObject;
/* If student id matched */
if (this.getStudentId() == inputStudent.getStudentId()) {

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");
}
}

View File

@ -32,7 +32,6 @@ public final class AppTest {
@Test
public void test() {
final String[] args = {};
App.main(args);
App.main();
}
}

View File

@ -23,11 +23,11 @@
package com.iluwatar.datamapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
/**
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
* database. Its responsibility is to transfer data between the two and also to isolate them from
@ -46,11 +46,11 @@ public class DataMapperTest {
public void testFirstDataMapper() {
/* Create new data mapper of first type */
final StudentDataMapper mapper = new StudentDataMapperImpl();
final var mapper = new StudentDataMapperImpl();
/* Create new student */
int studentId = 1;
Student student = new Student(studentId, "Adam", 'A');
var studentId = 1;
var student = new Student(studentId, "Adam", 'A');
/* Add student in respectibe db */
mapper.insert(student);
@ -59,7 +59,7 @@ public class DataMapperTest {
assertEquals(studentId, mapper.find(student.getStudentId()).get().getStudentId());
/* Update existing student object */
String updatedName = "AdamUpdated";
var updatedName = "AdamUpdated";
student = new Student(student.getStudentId(), updatedName, 'A');
/* Update student in respectibe db */

View File

@ -23,12 +23,10 @@
package com.iluwatar.datamapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Tests {@link Student}.
@ -36,8 +34,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public final class StudentTest {
/**
* This API tests the equality behaviour of Student object
* Object Equality should work as per logic defined in equals method
* This API tests the equality behaviour of Student object Object Equality should work as per
* logic defined in equals method
*
* @throws Exception if any execution error during test
*/
@ -45,9 +43,9 @@ public final class StudentTest {
public void testEquality() throws Exception {
/* Create some students */
final Student firstStudent = new Student(1, "Adam", 'A');
final Student secondStudent = new Student(2, "Donald", 'B');
final Student secondSameStudent = new Student(2, "Donald", 'B');
final var firstStudent = new Student(1, "Adam", 'A');
final var secondStudent = new Student(2, "Donald", 'B');
final var secondSameStudent = new Student(2, "Donald", 'B');
/* Check equals functionality: should return 'true' */
assertEquals(firstStudent, firstStudent);