Assignment #10: Garbage Collection

A Library class is used to store a collection of Books.

You may assume that a Book has an author, title, ISBN number, and publication date with appropriate accesssors and an overridden method toString. ISBN numbers are unique.

Equals for books compares ISBN numbers. However, since books are created by the Library, will have unique ISBN numbers (make sure this is enforced) and are immutable, you don't have to worry about equals, since only identical books can have the same ISBN numbers.

A Library has a constructor that will initialize it with a collection of books. You can simply create a few predefined books in this constructor, rather than going to the effort of trying to read info from a data file, but you should at least have code available to make sure that duplicate ISBN numbers are not stored. Additionally, the Library provides two public methods:

  1. public boolean isAvailable( String isbn ): returns true if book with this ISBN number exists and is not checked out.
  2. public Book checkout( String isbn ): method to checkout an existing book. Returns null if book is not available.
Of course, once a book has been checked out of the library, it cannot be checked out until it has been returned. A book is automatically returned to the library if there are no valid references to it.

Implement this spec using weak references (to reclaim books), along the lines of the Foo pool demonstrated in class. This means that Book is an interface, the behavior of books is implemented in BookImpl, and the checkout method actually returns a BookProxy. However, note that you can have more than one library, and the methods that are specified above are INSTANCE methods, not static methods. This means you cannot simply blindly copy and paste from the example in class. Both checking out a book and the reclamation of a book should be constant-time operations. Write a test program that illustrates the behavior of your library class.