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