public static List<Book> listBooksByTitleInfix(String titleInfix, int offset, int limit) { titleInfix = titleInfix.toLowerCase(); int curOffset = 0; ArrayList<Book> result = new ArrayList<Book>(limit); for (int i = 0; i < STORE.size(); i++) { Book book = STORE.get(i); if (book.getTitle().toLowerCase().contains(titleInfix)) { if (curOffset >= offset) { result.add(book); } curOffset++; } if (result.size() == limit) { break; } } return result; }
public static Book findBook(Long id) { for (Book b : STORE) { if (b.getId().equals(id)) { return b; } } return null; }
public static Integer countBooksByTitleInfix(String infix) { int count = 0; infix = infix.toLowerCase(); for (Book b : STORE) { if (b.getTitle().toLowerCase().contains(infix)) { count++; } } return count; }
public static Long save(Book book) { long id = book.getId() == null ? ++LAST_ADDED_ID : book.getId(); book.setId(id); book.increaseVersion(); int indexOf = STORE.indexOf(book); if (indexOf == -1) { STORE.add(book); } else { STORE.set(indexOf, book); } return id; }