package nettext.document; import java.util.List; import java.util.Vector; /** * Class DocumentList will contain a list of all documents on the server. * *
 * Revision History:
 *     v1.0 (Feb. 15, 2004) - Created the DocumentList class
 * 
* * @author Daniyar Zhanbekov * @version Version 1.0, Feb. 15, 2004 */ public class DocumentList { /** * This is the list of all documents. */ private List documents; /** * Creates a new DocumentList instance. */ public DocumentList() { documents = new Vector(); } /** * Goes through the list of already existing docs and if the * doc with the same docId already exists, does not add this * new doc to the list, and returns false. If no doc with the * same docId already exists, then adds this new doc to the * list and returns true. * * @param doc a Document value * @return a boolean value */ public final boolean addDocument(final Document doc) { if (size() == 0) { documents.add(doc); return true; } for (int i = 0; i < size(); i++) { if (((Document) documents.get(i)).getDocumentId() == doc.getDocumentId()) { return false; } } documents.add(doc); return true; } /** * Returns the object corresponding to the document, whose name is * passed in. * * @param name of a doca String value * @return a Document value */ public final Document getDocument(final String name) { DocumentListIterator listIter = new DocumentListIterator(this); while (listIter.hasNext()) { Document current = listIter.next(); if (current.getDocumentName().equals(name)) { return current; } } return null; } /** * Returns the object corresponding to the doc, whose docId is * passed in. * * @param id a int value * @return a Document value */ public final Document getDocument(final int id) { DocumentListIterator listIter = new DocumentListIterator(this); while (listIter.hasNext()) { Document current = listIter.next(); if (current.getDocumentId() == id) { return current; } } return null; } /** * Simply removes all the docs from the list. Doesn't notify * anybody or anything, just calls clear on the doc vector. */ public final void clear() { documents.clear(); } /** * Returns a Document list iterator that points to the first element * of the vector. * * @return a DocumentListIterator value */ public final DocumentListIterator getIterator() { return new DocumentListIterator(this); } /** * Tests if there are no docs. * * @return a boolean value */ public final boolean isEmpty() { return documents.isEmpty(); } /** * Removes the doc passed in from the list if it is present * there. * * @param doc a Document value */ public final void removeDocument(final Document doc) { int index = documents.indexOf(doc); if (index >= 0) { documents.remove(index); } } /** * Returns the number of docs currently available. * * @return an int value */ public final int size() { return documents.size(); } /** * Returns the doc at the specified index in the vector. * * @param index an int value * @return a Document value */ protected final Document elementAt(final int index) { return (Document) documents.get(index); } }