#1284 Use local variable inference
This commit is contained in:
parent
2332520d67
commit
97e3a3debc
@ -78,7 +78,7 @@ public class BookRepository {
|
||||
throw new BookNotFoundException("Not found book with id: " + book.getId());
|
||||
}
|
||||
|
||||
Book latestBook = collection.get(book.getId());
|
||||
var latestBook = collection.get(book.getId());
|
||||
if (book.getVersion() != latestBook.getVersion()) {
|
||||
throw new VersionMismatchException(
|
||||
"Tried to update stale version " + book.getVersion()
|
||||
@ -107,10 +107,10 @@ public class BookRepository {
|
||||
Here's the concurrency control in action:
|
||||
|
||||
```java
|
||||
long bookId = 1;
|
||||
var bookId = 1;
|
||||
// Alice and Bob took the book concurrently
|
||||
final Book aliceBook = bookRepository.get(bookId);
|
||||
final Book bobBook = bookRepository.get(bookId);
|
||||
final var aliceBook = bookRepository.get(bookId);
|
||||
final var bobBook = bookRepository.get(bookId);
|
||||
|
||||
aliceBook.setTitle("Kama Sutra"); // Alice has updated book title
|
||||
bookRepository.update(aliceBook); // and successfully saved book in database
|
||||
|
@ -53,17 +53,17 @@ public class App {
|
||||
BookDuplicateException,
|
||||
BookNotFoundException,
|
||||
VersionMismatchException {
|
||||
long bookId = 1;
|
||||
var bookId = 1;
|
||||
|
||||
BookRepository bookRepository = new BookRepository();
|
||||
Book book = new Book();
|
||||
var bookRepository = new BookRepository();
|
||||
var book = new Book();
|
||||
book.setId(bookId);
|
||||
bookRepository.add(book); // adding a book with empty title and author
|
||||
LOGGER.info("An empty book with version {} was added to repository", book.getVersion());
|
||||
|
||||
// Alice and Bob took the book concurrently
|
||||
final Book aliceBook = bookRepository.get(bookId);
|
||||
final Book bobBook = bookRepository.get(bookId);
|
||||
final var aliceBook = bookRepository.get(bookId);
|
||||
final var bobBook = bookRepository.get(bookId);
|
||||
|
||||
aliceBook.setTitle("Kama Sutra"); // Alice has updated book title
|
||||
bookRepository.update(aliceBook); // and successfully saved book in database
|
||||
|
@ -56,7 +56,7 @@ public class BookRepository {
|
||||
throw new BookNotFoundException("Not found book with id: " + book.getId());
|
||||
}
|
||||
|
||||
Book latestBook = collection.get(book.getId());
|
||||
var latestBook = collection.get(book.getId());
|
||||
if (book.getVersion() != latestBook.getVersion()) {
|
||||
throw new VersionMismatchException(
|
||||
"Tried to update stale version " + book.getVersion()
|
||||
|
@ -33,17 +33,17 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
class BookRepositoryTest {
|
||||
@Test
|
||||
void testBookRepository() throws BookDuplicateException, BookNotFoundException, VersionMismatchException {
|
||||
final long bookId = 1;
|
||||
final var bookId = 1;
|
||||
|
||||
BookRepository bookRepository = new BookRepository();
|
||||
Book book = new Book();
|
||||
var bookRepository = new BookRepository();
|
||||
var book = new Book();
|
||||
book.setId(bookId);
|
||||
bookRepository.add(book);
|
||||
|
||||
assertEquals(0, book.getVersion());
|
||||
|
||||
final Book aliceBook = bookRepository.get(bookId);
|
||||
final Book bobBook = bookRepository.get(bookId);
|
||||
final var aliceBook = bookRepository.get(bookId);
|
||||
final var bobBook = bookRepository.get(bookId);
|
||||
|
||||
assertEquals(aliceBook.getTitle(), bobBook.getTitle());
|
||||
assertEquals(aliceBook.getAuthor(), bobBook.getAuthor());
|
||||
|
Loading…
x
Reference in New Issue
Block a user