use of atomics way too verbose

This commit is contained in:
Simon Massey 2018-10-30 22:57:04 +00:00
parent aedf3b5cb5
commit e3fc47dd53
1 changed files with 3 additions and 7 deletions

View File

@ -2,7 +2,6 @@ package com.baeldung.thymeleaf.controller;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -22,18 +21,15 @@ import com.baeldung.thymeleaf.service.BookService;
@Controller @Controller
public class BookController { public class BookController {
@Autowired @Autowired
private BookService bookService; private BookService bookService;
@RequestMapping(value = "/listBooks", method = RequestMethod.GET) @RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) { public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
AtomicInteger currentPage = new AtomicInteger(1); int currentPage = page.isPresent()?page.get():5;
AtomicInteger pageSize = new AtomicInteger(5); int pageSize = size.isPresent()?size.get():1;
page.ifPresent(p -> currentPage.set(p));
size.ifPresent(s -> pageSize.set(s));
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage.get() - 1, pageSize.get())); Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("bookPage", bookPage); model.addAttribute("bookPage", bookPage);