All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nico.aoc.book.shop.BookShop Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package org.nico.aoc.book.shop;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.nico.aoc.book.Book;
import org.nico.aoc.throwable.BookStorageException;

public class BookShop {
	
	private static BookShop instance;
	
	private Map booksMap;

	private BookShop(){
		booksMap = new HashMap();
	}
	
	public static BookShop getInstance(){
		try {    
            if(instance != null){
            }else{  
                Thread.sleep(200);  
                synchronized (BookShop.class) {  
                    if(instance == null){
                        instance = new BookShop();  
                    }  
                }  
            }   
        } catch (InterruptedException e) {   
            e.printStackTrace();  
        }  
        return instance; 
	}
	
	public void put(Book book) throws BookStorageException{
		String id = book.getId().toLowerCase();
		if(booksMap.containsKey(id)){
			throw new BookStorageException(id);
		}
		booksMap.put(id, book);
	}
	
	public Book get(String id){
		return booksMap.get(id.toLowerCase());
	}
	
	public boolean containsBook(String id){
		return booksMap.containsKey(id.toLowerCase());
	}
	
	public Set getBooks(){
		Set books = new HashSet();
		books.addAll(booksMap.values());
		return books;
	}

	@Override
	public String toString() {
		return "BookShop [booksMap=" + booksMap + "]";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy