Skip to content

Commit

Permalink
Using vars everywhere (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
velo authored Aug 2, 2024
2 parents 04f0a48 + fef8ca6 commit 7218ed2
Show file tree
Hide file tree
Showing 1,239 changed files with 9,910 additions and 7,299 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.querydsl.example.jpa.model;

import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -13,7 +17,7 @@ public class Tweet extends BaseEntity {
private User poster;

@ManyToMany(fetch = FetchType.EAGER)
private List<User> mentions = new ArrayList<User>();
private List<User> mentions = new ArrayList<>();

@ManyToOne private Location location;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.querydsl.example.jpa.model;

import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -11,7 +15,7 @@ public class User extends BaseEntity {
private String username;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "poster")
private Set<Tweet> tweets = new HashSet<Tweet>();
private Set<Tweet> tweets = new HashSet<>();

public User() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import jakarta.inject.Provider;
import jakarta.persistence.EntityManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,16 +21,16 @@ public abstract class AbstractPersistenceTest {
@Before
@Transactional
public void before() {
EntityManager entityManager = em.get();
var entityManager = em.get();
entityManager.getEntityManagerFactory().getCache().evictAll();
Session session = entityManager.unwrap(Session.class);
var session = entityManager.unwrap(Session.class);
session.doWork(
new Work() {
@Override
public void execute(Connection connection) throws SQLException {
List<String> tables = new ArrayList<String>();
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, "PUBLIC", null, new String[] {"TABLE"});
List<String> tables = new ArrayList<>();
var md = connection.getMetaData();
var rs = md.getTables(null, "PUBLIC", null, new String[] {"TABLE"});
try {
while (rs.next()) {
tables.add(rs.getString("TABLE_NAME"));
Expand All @@ -41,7 +39,7 @@ public void execute(Connection connection) throws SQLException {
rs.close();
}

try (java.sql.Statement stmt = connection.createStatement()) {
try (var stmt = connection.createStatement()) {
stmt.execute("SET REFERENTIAL_INTEGRITY FALSE");
for (String table : tables) {
stmt.execute("TRUNCATE TABLE " + table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public class TweetRepositoryTest extends AbstractPersistenceTest {

@Test
public void save_and_find_by_id() {
User poster = new User("dr_frank");
var poster = new User("dr_frank");
userRepository.save(poster);

String content = "I am alive! #YOLO";
Tweet tweet = new Tweet(poster, content, Collections.<User>emptyList(), null);
var content = "I am alive! #YOLO";
var tweet = new Tweet(poster, content, Collections.<User>emptyList(), null);
repository.save(tweet);
assertThat(repository.findById(tweet.getId()).getContent()).isEqualTo(content);
}

@Test
public void find_list_by_predicate() {
User poster = new User("dr_frank");
var poster = new User("dr_frank");
userRepository.save(poster);

repository.save(new Tweet(poster, "It is a alive! #YOLO", Collections.<User>emptyList(), null));
Expand All @@ -41,7 +41,7 @@ public void find_list_by_predicate() {

@Test
public void find_list_by_predicate_with_hibernate() {
User poster = new User("dr_frank");
var poster = new User("dr_frank");
userRepository.save(poster);

repository.save(new Tweet(poster, "It is a alive! #YOLO", Collections.<User>emptyList(), null));
Expand All @@ -57,9 +57,9 @@ public void find_list_by_complex_predicate() {
for (String username : usernames) {
users.add(userRepository.save(new User(username)));
}
User poster = new User("duplo");
var poster = new User("duplo");
userRepository.save(poster);
for (int i = 0; i < 100; i++) {
for (var i = 0; i < 100; i++) {
repository.save(new Tweet(poster, "spamming @dr_frank " + i, users.subList(0, 1), null));
}
assertThat(repository.findAll(tweet.mentions.contains(users.get(1)))).isEmpty();
Expand All @@ -78,9 +78,9 @@ public void find_list_by_complex_predicate_hibernate() {
for (String username : usernames) {
users.add(userRepository.save(new User(username)));
}
User poster = new User("duplo");
var poster = new User("duplo");
userRepository.save(poster);
for (int i = 0; i < 100; i++) {
for (var i = 0; i < 100; i++) {
repository.save(new Tweet(poster, "spamming @dr_frank " + i, users.subList(0, 1), null));
}
assertThat(repository.findAllWithHibernateQuery(tweet.mentions.contains(users.get(1))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class UserRepositoryTest extends AbstractPersistenceTest {

@Test
public void save_and_get_by_id() {
String username = "jackie";
User user = new User(username);
var username = "jackie";
var user = new User(username);
repository.save(user);
assertThat(repository.findById(user.getId()).getUsername()).isEqualTo(username);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.querydsl.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -48,7 +47,7 @@ public List<Fruit> get() {
@GET
@Path("{id}")
public Fruit getSingle(Integer id) {
Fruit entity = queryFactory.selectFrom(f).where(f.id.eq(id)).fetchOne();
var entity = queryFactory.selectFrom(f).where(f.id.eq(id)).fetchOne();
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
Expand All @@ -74,7 +73,7 @@ public Fruit update(Integer id, Fruit fruit) {
throw new WebApplicationException("Fruit Name was not set on request.", 422);
}

Fruit entity = getSingle(id);
var entity = getSingle(id);
entity.setName(fruit.getName());

return entity;
Expand All @@ -84,7 +83,7 @@ public Fruit update(Integer id, Fruit fruit) {
@Path("{id}")
@Transactional
public Response delete(Integer id) {
long modified = queryFactory.delete(f).where(f.id.eq(id)).execute();
var modified = queryFactory.delete(f).where(f.id.eq(id)).execute();
if (modified == 0L) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
Expand All @@ -100,12 +99,12 @@ public static class ErrorMapper implements ExceptionMapper<Exception> {
public Response toResponse(Exception exception) {
LOGGER.error("Failed to handle request", exception);

int code = 500;
var code = 500;
if (exception instanceof WebApplicationException applicationException) {
code = applicationException.getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
var exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("exceptionType", exception.getClass().getName());
exceptionJson.put("code", code);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CommandLineRunner demo(CustomerRepository repository) {
log.info("");

// fetch an individual customer by ID
Customer customer = repository.findById(1L);
var customer = repository.findById(1L);
log.info("Customer found with findById(1L):");
log.info("--------------------------------");
log.info(customer.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public PlatformTransactionManager transactionManager() throws SQLException {
@Bean
public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLException {

HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
var jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.H2);
jpaVendorAdapter.setGenerateDdl(true);

LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
var bean = new LocalContainerEntityManagerFactoryBean();
bean.setJpaVendorAdapter(jpaVendorAdapter);
bean.setPackagesToScan(Config.class.getPackage().getName());
bean.setDataSource(dataSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ default Customer findById(long id) {
return select(C).from(C).where(C.id.eq(id)).fetchOne();
}

@Override
default List<Customer> findAll() {
return select(C).from(C).fetch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,10 +32,10 @@ public class CustomerRepositoryTests {

@Test
public void testFindByLastName() {
Customer customer = new Customer("first", "last");
var customer = new Customer("first", "last");
customers.save(customer);

List<Customer> findByLastName = customers.findByLastName(customer.getLastName());
var findByLastName = customers.findByLastName(customer.getLastName());

assertThat(findByLastName)
.extracting(Customer::getLastName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@EnableR2dbcRepositories
class R2DBCConfiguration extends AbstractR2dbcConfiguration {

@Override
@Bean
public H2ConnectionFactory connectionFactory() {
return new H2ConnectionFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.querydsl.example.dto.CustomerAddress;
import com.querydsl.example.dto.Person;
import com.querydsl.r2dbc.R2DBCQueryFactory;
import com.querydsl.r2dbc.dml.R2DBCInsertClause;
import com.querydsl.r2dbc.group.ReactiveGroupBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -54,7 +53,7 @@ public Flux<Customer> findAll(Predicate... where) {

@Override
public Mono<Customer> save(Customer c) {
Long id = c.getId();
var id = c.getId();

if (id == null) {
return insert(c);
Expand Down Expand Up @@ -124,7 +123,7 @@ public Mono<Customer> update(Customer c) {
.execute()
.flatMap(
__ -> {
R2DBCInsertClause insert = queryFactory.insert(customerAddress);
var insert = queryFactory.insert(customerAddress);
return Flux.fromIterable(c.getAddresses())
.flatMap(
ca -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private <T extends ReactiveStoreClause<T>> T populate(T dml, Order o) {

@Override
public Mono<Order> save(Order o) {
Long id = o.getId();
var id = o.getId();

if (id == null) {
return insert(o);
Expand Down Expand Up @@ -93,7 +93,7 @@ public Mono<Order> insert(Order o) {
}

public Mono<Order> update(Order o) {
Long id = o.getId();
var id = o.getId();

return populate(queryFactory.update(customerOrder), o)
.where(customerOrder.id.eq(id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Flux<Person> findAll(Predicate... where) {

@Override
public Mono<Person> save(Person p) {
Long id = p.getId();
var id = p.getId();

if (id == null) {
return insert(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.querydsl.example.dto.ProductL10n;
import com.querydsl.example.dto.Supplier;
import com.querydsl.r2dbc.R2DBCQueryFactory;
import com.querydsl.r2dbc.dml.R2DBCInsertClause;
import com.querydsl.r2dbc.group.ReactiveGroupBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -57,7 +56,7 @@ private <T extends ReactiveStoreClause<T>> T populate(T dml, Product p) {

@Override
public Mono<Product> save(Product p) {
Long id = p.getId();
var id = p.getId();

if (id == null) {
return insert(p);
Expand All @@ -71,7 +70,7 @@ public Mono<Product> insert(Product p) {
.executeWithKey(product.id)
.flatMapIterable(
id -> {
R2DBCInsertClause insert = queryFactory.insert(productL10n);
var insert = queryFactory.insert(productL10n);
return p.getLocalizations().stream()
.map(
l10n ->
Expand All @@ -93,7 +92,7 @@ public Mono<Product> insert(Product p) {
}

public Mono<Product> update(Product p) {
Long id = p.getId();
var id = p.getId();

return populate(queryFactory.update(product), p)
.where(product.id.eq(id))
Expand All @@ -107,7 +106,7 @@ public Mono<Product> update(Product p) {
.execute()
.flatMapIterable(
___ -> {
R2DBCInsertClause insert = queryFactory.insert(productL10n);
var insert = queryFactory.insert(productL10n);
return p.getLocalizations().stream()
.map(
l10n ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Flux<Supplier> findAll(Predicate... where) {

@Override
public Mono<Supplier> save(Supplier p) {
Long id = p.getId();
var id = p.getId();

if (id == null) {
return insert(p);
Expand Down
Loading

0 comments on commit 7218ed2

Please sign in to comment.