Files
GTSchoolShit/CS2335/lab5/src/nettext/document/DocumentList.java
2025-06-07 01:59:34 -04:00

160 lines
3.8 KiB
Java

package nettext.document;
import java.util.List;
import java.util.Vector;
/**
* Class DocumentList will contain a list of all documents on the server.
*
* <PRE>
* Revision History:
* v1.0 (Feb. 15, 2004) - Created the DocumentList class
* </PRE>
*
* @author <A HREF="mailto:gtg563g@mail.gatech.edu">Daniyar Zhanbekov</A>
* @version Version 1.0, Feb. 15, 2004
*/
public class DocumentList {
/**
* This is the list of all documents.
*/
private List documents;
/**
* Creates a new <code>DocumentList</code> 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 <code>Document</code> value
* @return a <code>boolean</code> 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 <code>String</code> value
* @return a <code>Document</code> 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 <code>int</code> value
* @return a <code>Document</code> 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 <code>DocumentListIterator</code> value
*/
public final DocumentListIterator getIterator() {
return new DocumentListIterator(this);
}
/**
* Tests if there are no docs.
*
* @return a <code>boolean</code> value
*/
public final boolean isEmpty() {
return documents.isEmpty();
}
/**
* Removes the doc passed in from the list if it is present
* there.
*
* @param doc a <code>Document</code> 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 <code>int</code> value
*/
public final int size() {
return documents.size();
}
/**
* Returns the doc at the specified index in the vector.
*
* @param index an <code>int</code> value
* @return a <code>Document</code> value
*/
protected final Document elementAt(final int index) {
return (Document) documents.get(index);
}
}