//******************************************************************************
//
// File:    MediaEntry.java
// Package: net.jini.print.lookup
// Unit:    Class net.jini.print.lookup.MediaEntry
//
//******************************************************************************

package net.jini.print.lookup;

import javax.print.attribute.Attribute;

import javax.print.attribute.standard.*;

import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaSize;
//import javax.print.attribute.standard.MediaColor;
//import javax.print.attribute.standard.MediaOpacity;
//import javax.print.attribute.standard.MediaHoleCount;
//import javax.print.attribute.standard.MediaKind;
//import javax.print.attribute.standard.MediaWeightEnglish;
//import javax.print.attribute.standard.MediaWeightMetric;
//import javax.print.attribute.standard.MediaStock;
//import javax.print.attribute.standard.MediaRecycled;
//import javax.print.attribute.standard.MediaPrePrinted;
//import javax.print.attribute.standard.MediaTabs;
//import javax.print.attribute.standard.MediaOrderCount;
//import javax.print.attribute.standard.MediaBackCoating;
//import javax.print.attribute.standard.MediaFrontCoating;
//import javax.print.attribute.standard.MediaLoaded;
//import javax.print.attribute.standard.MediaDescription;


import net.jini.entry.AbstractEntry;

import net.jini.lookup.entry.ServiceControlled;

/**
 * Class MediaEntry is a Jini service attribute indicating a medium that a Jini 
 * Print Service instance supports.
 * <P>
 * A MediaEntry is used when a Jini Print Service instance registers with the 
 * Jini Lookup Service (JLUS). For each medium it supports, a Jini Print Service 
 * instance must register a service attribute of class MediaEntry in its service 
 * registration in the JLUS. The MediaEntry's fields contain the same media 
 * characteristics as the fields of a {@link 
 * javax.print.attribute.standard.Media Media} printing attribute. When used 
 * in a JLUS service registration, in general, all the MediaEntry's media 
 * characteristics are filled in with the proper values. Only the not-applicable 
 * media characteristics are set to null -- e.g., if the medium does not have a 
 * description, the <CODE>description</CODE> field would be left null. 
 * <P>
 * A MediaEntry is also used when a client searches the JLUS for Jini Print 
 * Service instances that support a given medium. When a MediaEntry is included 
 * in a service template, each media characteristic may be filled in with a 
 * value or may be wildcarded (null). In this way, the client can set up a JLUS 
 * search specifying only the media characteristics it cares about, and the JLUS 
 * will find Jini Print Service instances that support media that match the 
 * media characteristics the client specified, ignoring any wildcarded media 
 * characteristics. 
 * <P>
 * <B>Design Rationale</B>
 * <P>
 * A {@link javax.print.attribute.standard.Media Media} printing attribute is 
 * registered into the JLUS using a MediaEntry, instead of an {@link 
 * AttributeEntry AttributeEntry} like all the other printing attributes, so 
 * that the JLUS itself can perform wildcard matching of media characteristics 
 * during searches. If a {@link javax.print.attribute.standard.Media Media} 
 * attribute were registered into the JLUS using an {@link AttributeEntry 
 * AttributeEntry}, such wildcard matching would have to be done on the client 
 * side instead of in the JLUS. 
 * <P>
 * <B>DRAFT STANDARD VERSION 1.0 (23-MAY-2000)</B>
 *
 * @author  Alan Kaminsky
 * @version 01-May-2000
 */
public class MediaEntry
	extends AbstractEntry
	implements ServiceControlled
	{

// Exported fields.

	/**
	 * Media size, or null if not specified (wildcarded).
	 * @serial
	 */
	public MediaSize size = null;

	/**
	 * Media color, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaColor color = null;

	/**
	 * Media opacity, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaOpacity opacity = null;

	/**
	 * Media hole count, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaHoleCount holeCount = null;

	/**
	 * Media kind, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaKind kind = null;

	/**
	 * English media weight, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaWeightEnglish weightEnglish = null;

	/**
	 * Metric media weight, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaWeightMetric weightMetric = null;

	/**
	 * Media stock, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaStock stock = null;

	/**
	 * Media recycled, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaRecycled recycled = null;

	/**
	 * Media pre-printed, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaPrePrinted prePrinted = null;

	/**
	 * Media tabs, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaTabs tabs = null;

	/**
	 * Media order count, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaOrderCount orderCount = null;

	/**
	 * Media back coating, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaBackCoating backCoating = null;

	/**
	 * Media front coating, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaFrontCoating frontCoating = null;

	/**
	 * Media loaded state, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaLoaded loaded = null;

	/**
	 * Media description, or null if not specified (wildcarded).
	 * @serial
	 */
//	public MediaDescription description = null;

// Exported constructors.

	/**
	 * Constructs a new media entry with all characteristics null (unspecified 
	 * or wildcarded). 
	 */
	public MediaEntry()
		{
		}

	/**
	 * Constructs a new media entry with the given characteristics. Any argument 
	 * may be null to leave the corresponding characteristic unspecified 
	 * (wildcarded). 
	 */
	public MediaEntry
		(MediaSize size
//		 MediaColor color,
//		 MediaOpacity opacity,
//		 MediaHoleCount holeCount,
//		 MediaKind kind,
//		 MediaWeightEnglish weightEnglish,
//		 MediaWeightMetric weightMetric,
//		 MediaStock stock,
//		 MediaRecycled recycled,
//		 MediaPrePrinted prePrinted,
//		 MediaTabs tabs,
//		 MediaOrderCount orderCount,
//		 MediaBackCoating backCoating,
//		 MediaFrontCoating frontCoating,
//		 MediaLoaded loaded,
//		 MediaDescription description
		 )
		{
		this.size = size;
/*		this.color = color;
		this.opacity = opacity;
		this.holeCount = holeCount;
		this.kind = kind;
		this.weightEnglish = weightEnglish;
		this.weightMetric = weightMetric;
		this.stock = stock;
		this.recycled = recycled;
		this.prePrinted = prePrinted;
		this.tabs = tabs;
		this.orderCount = orderCount;
		this.backCoating = backCoating;
		this.frontCoating = frontCoating;
		this.loaded = loaded;
		this.description = description;
*/		}

	/**
	 * Constructs a new media entry with the characteristics from the given 
	 * array. All media characteristics not present in the given array are set 
	 * to null (wildcarded). 
	 *
	 * @param  theCharacteristics
	 *     Array of media characteristics. Each element in the array must be an 
	 *     instance of one of the media characteristic attributes. At most one 
	 *     instance of a particular media characteristic attribute may appear in 
	 *     the array. Null elements in the array are ignored. If 
	 *     <CODE>theCharacteristics</CODE> is null, a media entry with 
	 *     <I>all</I> characteristics null (wildcarded) is constructed. 
	 *
	 * @exception  IllegalArgumentException
	 *     (unchecked exception) Thrown if an element in 
	 *     <CODE>theCharacteristics</CODE> is not an instance of one of the 
	 *     media characteristic attributes, or if a particular media 
	 *     characteristic attribute appears more than once in 
	 *     <CODE>theCharacteristics</CODE>. 
	 */
	public MediaEntry
		(Attribute[] theCharacteristics)
		{
		int n = theCharacteristics == null ? 0 : theCharacteristics.length;
		for (int i = 0; i < n; ++ i)
			{
			Attribute theAttribute = theCharacteristics[i];
			if (theAttribute == null)
				{
				// Ignore it.
				}
			else if (theAttribute instanceof MediaSize)
				{
				size = (MediaSize)
					verifyNotDuplicated (size, theAttribute);
				}
/*			else if (theAttribute instanceof MediaColor)
				{
				color = (MediaColor)
					verifyNotDuplicated (color, theAttribute);
				}
			else if (theAttribute instanceof MediaOpacity)
				{
				opacity = (MediaOpacity)
					verifyNotDuplicated (opacity, theAttribute);
				}
			else if (theAttribute instanceof MediaHoleCount)
				{
				holeCount = (MediaHoleCount)
					verifyNotDuplicated (holeCount, theAttribute);
				}
			else if (theAttribute instanceof MediaKind)
				{
				kind = (MediaKind)
					verifyNotDuplicated (kind, theAttribute);
				}
			else if (theAttribute instanceof MediaWeightEnglish)
				{
				weightEnglish = (MediaWeightEnglish)
					verifyNotDuplicated (weightEnglish, theAttribute);
				}
			else if (theAttribute instanceof MediaWeightMetric)
				{
				weightMetric = (MediaWeightMetric)
					verifyNotDuplicated (weightMetric, theAttribute);
				}
			else if (theAttribute instanceof MediaStock)
				{
				stock = (MediaStock)
					verifyNotDuplicated (stock, theAttribute);
				}
			else if (theAttribute instanceof MediaRecycled)
				{
				recycled = (MediaRecycled)
					verifyNotDuplicated (recycled, theAttribute);
				}
			else if (theAttribute instanceof MediaPrePrinted)
				{
				prePrinted = (MediaPrePrinted)
					verifyNotDuplicated (prePrinted, theAttribute);
				}
			else if (theAttribute instanceof MediaTabs)
				{
				tabs = (MediaTabs)
					verifyNotDuplicated (tabs, theAttribute);
				}
			else if (theAttribute instanceof MediaOrderCount)
				{
				orderCount = (MediaOrderCount)
					verifyNotDuplicated (orderCount, theAttribute);
				}
			else if (theAttribute instanceof MediaBackCoating)
				{
				backCoating = (MediaBackCoating)
					verifyNotDuplicated (backCoating, theAttribute);
				}
			else if (theAttribute instanceof MediaFrontCoating)
				{
				frontCoating = (MediaFrontCoating)
					verifyNotDuplicated (frontCoating, theAttribute);
				}
			else if (theAttribute instanceof MediaLoaded)
				{
				loaded = (MediaLoaded)
					verifyNotDuplicated (loaded, theAttribute);
				}
			else if (theAttribute instanceof MediaDescription)
				{
				description = (MediaDescription)
					verifyNotDuplicated (description, theAttribute);
				}
*/			else
				{
				throw new IllegalArgumentException
					("net.jini.print.lookup.MediaEntry.MediaEntry(): Attribute not a media characteristic encountered");
				}
			}
		}

	private static Attribute verifyNotDuplicated
		(Attribute theField,
		 Attribute theAttribute)
		{
		if (theField != null)
			{
			throw new IllegalArgumentException
				("net.jini.print.lookup.MediaEntry.MediaEntry(): Duplicate media characteristic attribute encountered");
			}
		return theAttribute;
		}

	/**
	 * Constructs a new media entry with the same media characteristics as the 
	 * given media attribute. Any null (wildcarded) media characteristics in the 
	 * given media attribute are also null (wildcarded) in the media entry. 
	 *
	 * @param  theMedia  Media attribute to copy.
	 */
	public MediaEntry
		(Media theMedia)
		{
//		this.size = theMedia.getMediaSize();
/*		this.color = theMedia.getMediaColor();
		this.opacity = theMedia.getMediaOpacity();
		this.holeCount = theMedia.getMediaHoleCount();
		this.kind = theMedia.getMediaKind();
		this.weightEnglish = theMedia.getMediaWeightEnglish();
		this.weightMetric = theMedia.getMediaWeightMetric();
		this.stock = theMedia.getMediaStock();
		this.recycled = theMedia.getMediaRecycled();
		this.prePrinted = theMedia.getMediaPrePrinted();
		this.tabs = theMedia.getMediaTabs();
		this.orderCount = theMedia.getMediaOrderCount();
		this.backCoating = theMedia.getMediaBackCoating();
		this.frontCoating = theMedia.getMediaFrontCoating();
		this.loaded = theMedia.getMediaLoaded();
		this.description = theMedia.getMediaDescription();
*/		}

	}