//******************************************************************************
//
// File:    ArrayMultiDoc.java
// Package: org.jpwg.jini.print.data
// Unit:    Class org.jpwg.jini.print.data.ArrayMultiDoc
//
//******************************************************************************

package org.jpwg.jini.print.data;

/**
 * Class ArrayMultiDoc is an implementation of interface {@link 
 * net.jini.print.data.MultiDoc MultiDoc} which is layered on top of an array of 
 * doc objects. If an ArrayMultiDoc object is serialized and sent to a remote 
 * Java Virtual Machine (JVM), the array and all the doc objects are serialized 
 * and sent with it. 
 * <P>
 * <B>DRAFT STANDARD VERSION 1.0 (23-MAY-2000)</B>
 *
 * @author  Alan Kaminsky
 * @version 19-May-2000
 */
public class ArrayMultiDoc
	implements net.jini.print.data.MultiDoc
	{

// Hidden data members.

	/**
	 * Array of doc objects.
	 * @serial
	 */
	private net.jini.print.data.Doc[] myDocs;
	/**
	 * Index of the current doc object in <CODE>myDocs</CODE>.
	 * @serial
	 */
	private int myIndex;

// Exported constructors.

	/**
	 * Construct a new array multidoc object that provides access to the doc 
	 * objects from the given array. A clone of the given array is stored 
	 * internally so that modifications to the given array will not affect this 
	 * array multidoc object. 
	 *
	 * @param  theDocs  Array of one or more doc objects.
	 *
	 * @param  NullPointerException
	 *     (unchecked exception) Thrown if <CODE>theDocs</CODE> is null or any 
	 *     element of <CODE>theDocs</CODE> is null. 
	 * @param  IllegalArgumentException
	 *     (unchecked exception) Thrown if <CODE>theDocs</CODE> is zero-length.
	 */
	public ArrayMultiDoc
		(net.jini.print.data.Doc[] theDocs)
		{
		this ((net.jini.print.data.Doc[])(verify (theDocs) .clone()), 0);
		}

	private static net.jini.print.data.Doc[] verify
		(net.jini.print.data.Doc[] theDocs)
		{
		if (theDocs == null)
			{
			throw new NullPointerException
				("org.jpwg.jini.print.data.ArrayMultiDoc.ArrayMultiDoc(): theDocs is null");
			}
		int n = theDocs.length;
		if (n == 0)
			{
			throw new IllegalArgumentException
				("org.jpwg.jini.print.data.ArrayMultiDoc.ArrayMultiDoc(): theDocs is zero-length");
			}
		for (int i = 0; i < n; ++ i)
			{
			if (theDocs[i] == null)
				{
				throw new NullPointerException
					("org.jpwg.jini.print.data.ArrayMultiDoc.ArrayMultiDoc(): theDocs[" +
					 i + "] is null");
				}
			}
		return theDocs;
		}

// Hidden constructors.

	/**
	 * Construct a new array multidoc object with the given doc array and index. 
	 *
	 * @param  theDocs   Array of doc objects.
	 * @param  theIndex  Index of the current doc object.
	 */
	private ArrayMultiDoc
		(net.jini.print.data.Doc[] theDocs,
		 int theIndex)
		{
		myDocs = theDocs;
		myIndex = theIndex;
		}

// Exported operations inherited and overridden from interface MultiDoc.

	/**
	 * Obtain the current doc object. Although the return type is interface 
	 * {@link javax.print.data.Doc javax.print.data.Doc} as inherited from the 
	 * superinterface method, the returned doc object is actually an instance of 
	 * the subinterface {@link Doc net.jini.print.data.Doc}. 
	 *
	 * @return  Current doc object.
	 */
	public javax.print.Doc getDoc()
		{
		return myDocs[myIndex];
		}

	/**
	 * Go to the multidoc object that contains the next doc object in the 
	 * sequence of doc objects. Although the return type is interface {@link 
	 * javax.print.data.MultiDoc javax.print.data.MultiDoc} as inherited from 
	 * the superinterface method, the returned multidoc object is actually an 
	 * instance of the subinterface net.jini.print.data.MultiDoc. 
	 *
	 * @return  Multidoc object containing the next doc object, or null if there
	 *          are no further doc objects.
	 */
	public javax.print.MultiDoc next()
		{
		return
			myIndex + 1 == myDocs.length ?
				null :
				new ArrayMultiDoc (myDocs, myIndex + 1);
		}

	}