-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDao.java
More file actions
127 lines (109 loc) · 3.9 KB
/
Copy pathBookDao.java
File metadata and controls
127 lines (109 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ab;
import jersey.repackaged.com.google.common.util.concurrent.ListenableFuture;
import jersey.repackaged.com.google.common.util.concurrent.ListeningExecutorService;
import jersey.repackaged.com.google.common.util.concurrent.MoreExecutors;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
/**
* @author Arpit Bhardwaj
*/
public class BookDao {
private Map<String,Book> books;
private ListeningExecutorService executorService;
public BookDao() {
/*books = new HashMap<>();
Book book1 = new Book();
book1.setId("1");
book1.setTitle("Title1");
book1.setAuthor("Author1");
book1.setIsbn("1234");
book1.setPublished(new Date());
Book book2 = new Book();
book2.setId("2");
book2.setTitle("Title2");
book2.setAuthor("Author2");
book2.setIsbn("2345");
book2.setPublished(new Date());
books.put(book1.getId(),book1);
books.put(book2.getId(),book2);*/
books = new ConcurrentHashMap<>();
executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
}
public Collection<Book> getBooks(){
return books.values();
}
public ListenableFuture<Collection<Book>> getBooksAsync(){
ListenableFuture<Collection<Book>> listenableFuture = executorService.submit(new Callable<Collection<Book>>() {
@Override
public Collection<Book> call() throws Exception {
return getBooks();
}
});
return listenableFuture;
}
public Book getBook(String id) throws BookNotFoundException{
if (books.containsKey(id)){
return books.get(id);
}
else{
throw new BookNotFoundException("Book " + id + " is not found");
}
}
public ListenableFuture<Book> getBookAsync(final String id){
ListenableFuture<Book> listenableFuture = executorService.submit(new Callable<Book>() {
@Override
public Book call() throws Exception {
return getBook(id);
}
});
return listenableFuture;
}
public Book addBook(Book book){
book.setId(UUID.randomUUID().toString());
books.put(book.getId(),book);
return book;
}
public ListenableFuture<Book> addBookAsync(final Book book){
ListenableFuture<Book> listenableFuture = executorService.submit(new Callable<Book>() {
@Override
public Book call() throws Exception {
return addBook(book);
}
});
return listenableFuture;
}
public Book updateBook(String id, Book updates) throws BookNotFoundException{
if (books.containsKey(id)){
Book book = books.get(id);
if (updates.getTitle() != null){
book.setTitle(updates.getTitle());
}
if (updates.getAuthor() != null){
book.setAuthor(updates.getAuthor());
}
if (updates.getPublished() != null) {
book.setPublished(updates.getPublished());
}
if (updates.getExtras() != null){
for (String key:
updates.getExtras().keySet()) {
book.setExtras(key, updates.getExtras().get(key));
}
}
return book;
}else{
throw new BookNotFoundException("Book " + id + " is not found");
}
}
public ListenableFuture<Book> updateBookAsync(final String id, final Book book){
ListenableFuture<Book> listenableFuture = executorService.submit(new Callable<Book>() {
@Override
public Book call() throws Exception {
return updateBook(id, book);
}
});
return listenableFuture;
}
}