diff --git a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java index c65fa4f..b6452cb 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java @@ -2,17 +2,20 @@ import org.ntlab.citrusserver.entities.Account; import org.ntlab.citrusserver.entities.Book; +import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.regex.*; + +@Repository public class PublicBookManager implements IBookListener{ private final BookManager bookManager; - private HashSet publicBooks = new HashSet<>(); + private final HashMap> publicBooks = new HashMap<>(); public PublicBookManager(BookManager bookManager) { this.bookManager = bookManager; @@ -20,15 +23,21 @@ } public ArrayList getAllPublicBooks(){ - return new ArrayList<>(this.publicBooks); + ArrayList books = new ArrayList<>(); + for(HashSet bookSet : publicBooks.values()){ + books.addAll(bookSet); + } + return books; } public ArrayList searchBooksByTitle(String title){ ArrayList books = new ArrayList<>(); var ptn = Pattern.compile(title); - for(Book book : this.publicBooks){ - if(ptn.matcher(book.getTitle()).find()){ - books.add(book); + for(HashSet bookSet : publicBooks.values()){ + for(Book book : bookSet){ + if(ptn.matcher(book.getTitle()).find()){ + books.add(book); + } } } return books; @@ -40,9 +49,11 @@ public ArrayList searchBooksByAccount(String accountId){ ArrayList books = new ArrayList<>(); - for(Book book : this.publicBooks){ - if(book.getAccountId().equals(accountId)){ - books.add(book); + for(HashSet bookSet : publicBooks.values()){ + for(Book book : bookSet){ + if(book.getAccountId().equals(accountId)){ + books.add(book); + } } } return books; @@ -65,9 +76,12 @@ @Override public void bookChanged(Account account, Book book) { if(book.getPublicity()){ - this.publicBooks.add(book); + if(!publicBooks.containsKey(account.getId())){ + publicBooks.put(account.getId(), new HashSet<>()); + } + this.publicBooks.get(account.getId()).add(book); } else{ - this.publicBooks.remove(book); + this.publicBooks.get(account.getId()).remove(book); } } }