Skip to content

Instantly share code, notes, and snippets.

@AlexGabor
Created March 18, 2016 07:24
Show Gist options
  • Save AlexGabor/324b347a78caf811e8d0 to your computer and use it in GitHub Desktop.
Save AlexGabor/324b347a78caf811e8d0 to your computer and use it in GitHub Desktop.
package ro.oneandonly.bookstore.service;
import ro.oneandonly.bookstore.domain.Book;
import ro.oneandonly.bookstore.domain.Client;
import ro.oneandonly.bookstore.domain.validators.ValidatorException;
import ro.oneandonly.bookstore.repository.Repository;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class BookService {
private final Repository<Long, Book> repository;
public BookService(Repository<Long, Book> repository) {
this.repository = repository;
}
public void addBook(Book book) throws ValidatorException {
repository.save(book);
}
public Set<Book> getAllBooks() {
Iterable<Book> books = repository.findAll();
return StreamSupport.stream(books.spliterator(), false).collect(Collectors.toSet());
}
public void updateBook(Book book) throws ValidatorException {
repository.update(book);
}
public void deleteBook(Long id) {
repository.delete(id);
}
private Set<Book> filter(Predicate<Book> predicate) {
Stream<Book> stream = StreamSupport.stream(repository.findAll().spliterator(), false);
return stream.filter(predicate).collect(Collectors.toSet());
}
public Set<Book> filterByAuthor(String author) {
String finalAuthor = author.toLowerCase();
return filter(book -> book.getAuthor().toLowerCase().contains(finalAuthor));
}
public Set<Book> filterByTitle(String title) {
String finalTitle = title.toLowerCase();
return filter(book -> book.getTitle().toLowerCase().contains(finalTitle));
}
public Set<Book> filterByIsbn(String isbn) {
return filter(book -> book.getIsbn().equals(isbn));
}
private List<Book> sort(Comparator<Book> comparator, boolean reverse) {
if (reverse)
comparator = comparator.reversed();
Stream<Book> stream = StreamSupport.stream(repository.findAll().spliterator(), false);
return stream.sorted(comparator).collect(Collectors.toList());
}
public List<Book> sortByTitle(boolean reverse) {
return sort(Comparator.comparing(Book::getTitle, Comparator.nullsLast(Comparator.naturalOrder())), reverse);
}
public List<Book> sortByAuthor(boolean reverse) {
return sort(Comparator.comparing(Book::getAuthor, Comparator.nullsLast(Comparator.naturalOrder())), reverse);
}
public List<Book> sortByPrice(boolean reverse) {
return sort(Comparator.comparing(Book::getPrice, Comparator.nullsLast(Comparator.naturalOrder())), reverse);
}
public List<Book> sortByPublisher(boolean reverse) {
Comparator<Book> comparator = (c1, c2) -> {
if (c1.getPublisher().equals("Manning") && c2.getPublisher().equals("Manning")) {
return c1.getTitle().compareTo(c2.getTitle());
}
if (c2.getPublisher().equals("Manning"))
return 1;
return -1;
};
return sort(comparator, reverse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment