Java 11 migrate remaining q-r (#1121)

* Moves queue-load-leveling to Java 11

* Moves reactor to Java 11

* Moves reader-writer-lock to Java 11

* Moves repository to Java 11

* Moves resource-acquisition-is-initialization to Java 11

* Moves retry to Java 11

* Moves role-object to Java 11
This commit is contained in:
Anurag Agarwal
2020-01-04 22:13:12 +05:30
committed by Ilkka Seppälä
parent cd2a2e7711
commit 20ea465b7f
52 changed files with 424 additions and 554 deletions

View File

@@ -24,7 +24,6 @@
package com.iluwatar.repository;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -55,14 +54,13 @@ public class App {
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
PersonRepository repository = context.getBean(PersonRepository.class);
var context = new ClassPathXmlApplicationContext("applicationContext.xml");
var repository = context.getBean(PersonRepository.class);
Person peter = new Person("Peter", "Sagan", 17);
Person nasta = new Person("Nasta", "Kuzminova", 25);
Person john = new Person("John", "lawrence", 35);
Person terry = new Person("Terry", "Law", 36);
var peter = new Person("Peter", "Sagan", 17);
var nasta = new Person("Nasta", "Kuzminova", 25);
var john = new Person("John", "lawrence", 35);
var terry = new Person("Terry", "Law", 36);
// Add new Person records
repository.save(peter);
@@ -74,17 +72,15 @@ public class App {
LOGGER.info("Count Person records: {}", repository.count());
// Print all records
List<Person> persons = (List<Person>) repository.findAll();
for (Person person : persons) {
LOGGER.info(person.toString());
}
var persons = (List<Person>) repository.findAll();
persons.stream().map(Person::toString).forEach(LOGGER::info);
// Update Person
nasta.setName("Barbora");
nasta.setSurname("Spotakova");
repository.save(nasta);
LOGGER.info("Find by id 2: {}", repository.findById(2L).get());
repository.findById(2L).ifPresent(p -> LOGGER.info("Find by id 2: {}", p));
// Remove record from Person
repository.deleteById(2L);
@@ -93,16 +89,15 @@ public class App {
LOGGER.info("Count Person records: {}", repository.count());
// find by name
Optional<Person> p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
LOGGER.info("Find by John is {}", p.get());
repository
.findOne(new PersonSpecifications.NameEqualSpec("John"))
.ifPresent(p -> LOGGER.info("Find by John is {}", p));
// find by age
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
LOGGER.info("Find Person with age between 20,40: ");
for (Person person : persons) {
LOGGER.info(person.toString());
}
persons.stream().map(Person::toString).forEach(LOGGER::info);
repository.deleteAll();

View File

@@ -25,7 +25,6 @@ package com.iluwatar.repository;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
@@ -55,7 +54,7 @@ public class AppConfig {
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
var basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName("org.h2.Driver");
basicDataSource.setUrl("jdbc:h2:~/databases/person");
basicDataSource.setUsername("sa");
@@ -68,13 +67,11 @@ public class AppConfig {
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager =
new LocalContainerEntityManagerFactoryBean();
var entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan("com.iluwatar");
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
entityManager.setJpaProperties(jpaProperties());
return entityManager;
}
@@ -82,7 +79,7 @@ public class AppConfig {
* Properties for Jpa.
*/
private static Properties jpaProperties() {
Properties properties = new Properties();
var properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
return properties;
@@ -93,7 +90,7 @@ public class AppConfig {
*/
@Bean
public JpaTransactionManager transactionManager() throws SQLException {
JpaTransactionManager transactionManager = new JpaTransactionManager();
var transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@@ -104,15 +101,13 @@ public class AppConfig {
* @param args command line args
*/
public static void main(String[] args) {
var context = new AnnotationConfigApplicationContext(AppConfig.class);
var repository = context.getBean(PersonRepository.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
AppConfig.class);
PersonRepository repository = context.getBean(PersonRepository.class);
Person peter = new Person("Peter", "Sagan", 17);
Person nasta = new Person("Nasta", "Kuzminova", 25);
Person john = new Person("John", "lawrence", 35);
Person terry = new Person("Terry", "Law", 36);
var peter = new Person("Peter", "Sagan", 17);
var nasta = new Person("Nasta", "Kuzminova", 25);
var john = new Person("John", "lawrence", 35);
var terry = new Person("Terry", "Law", 36);
// Add new Person records
repository.save(peter);
@@ -124,17 +119,15 @@ public class AppConfig {
LOGGER.info("Count Person records: {}", repository.count());
// Print all records
List<Person> persons = (List<Person>) repository.findAll();
for (Person person : persons) {
LOGGER.info(person.toString());
}
var persons = (List<Person>) repository.findAll();
persons.stream().map(Person::toString).forEach(LOGGER::info);
// Update Person
nasta.setName("Barbora");
nasta.setSurname("Spotakova");
repository.save(nasta);
LOGGER.info("Find by id 2: {}", repository.findById(2L).get());
repository.findById(2L).ifPresent(p -> LOGGER.info("Find by id 2: {}", p));
// Remove record from Person
repository.deleteById(2L);
@@ -143,16 +136,15 @@ public class AppConfig {
LOGGER.info("Count Person records: {}", repository.count());
// find by name
Optional<Person> p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
LOGGER.info("Find by John is {}", p.get());
repository
.findOne(new PersonSpecifications.NameEqualSpec("John"))
.ifPresent(p -> LOGGER.info("Find by John is {}", p));
// find by age
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
LOGGER.info("Find Person with age between 20,40: ");
for (Person person : persons) {
LOGGER.info(person.toString());
}
persons.stream().map(Person::toString).forEach(LOGGER::info);
context.close();

View File

@@ -92,9 +92,8 @@ public class Person {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
final var prime = 31;
var result = 1;
result = prime * result + age;
result = prime * result + (id == null ? 0 : id.hashCode());
result = prime * result + (name == null ? 0 : name.hashCode());
@@ -113,7 +112,7 @@ public class Person {
if (getClass() != obj.getClass()) {
return false;
}
Person other = (Person) obj;
var other = (Person) obj;
if (age != other.age) {
return false;
}
@@ -132,13 +131,9 @@ public class Person {
return false;
}
if (surname == null) {
if (other.surname != null) {
return false;
}
} else if (!surname.equals(other.surname)) {
return false;
return other.surname == null;
}
return true;
return surname.equals(other.surname);
}
}

View File

@@ -50,9 +50,7 @@ public class PersonSpecifications {
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.between(root.get("age"), from, to);
}
}
@@ -72,9 +70,7 @@ public class PersonSpecifications {
* Get predicate.
*/
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get("name"), this.name);
}
}