package W3C.PICS;
import java.util.Vector;
import java.io.IOException;
/**
 * Range is the building block of Filter, representing the filtering
 * element for a category.
 *
 * @see W3C.PICS.Filter
 * @author Ching Law
 */
public class Range
{
/**
 *  Constructor for single value Rating
 * @param categorytransmit The transmit name for the category
 */

 public Range(String categorytransmit,double min, double max)
 {
    this.categorytransmit=categorytransmit;
    allowValuelist=new Vector();
    allowValuelist.addElement(new Double(min));
    allowValuelist.addElement(new Double(max));
    multivalue=false;
 }

/**
 * Contructs a Range from a PICSTokenizer. Used for parsing filter
 * description.
 */
 public Range(PICSTokenizer PT) throws PICSException, IOException
 {
    rangeValuelist=new Vector();
    int type;
    PT.nextTransmitas();
    categorytransmit=PT.sval;
    allowValuelist=PT.nextAllowBlockValue();
    blockValuelist=PT.nextAllowBlockValue();

    while (true)
        {type=PT.nextRangeValue();
         if (type==PICSTokenizer.T_ENDOFRANGE)
            {
             break;
            }
         else if (type==PICSTokenizer.T_OK)
            addRangeValue(new RangeValue(PT.nval,PT.nval2));
         else throw new PICSException("Range Constructor");
        }
    multivalue=true;
 }

/**
 *  Constructor for multi-value Range
 */
 public Range(String categorytransmit)
 {
    this.categorytransmit=categorytransmit;
    allowValuelist=new Vector();
    blockValuelist=new Vector();
    rangeValuelist=new Vector();
    multivalue=true;
 }

 public void addAllowValue(double value)
 {
  if (multivalue)
     allowValuelist.addElement(new Double(value));
  else throw new RuntimeException();
 }

 public void addBlockValue(double value)
 {
  if (multivalue)
     blockValuelist.addElement(new Double(value));
  else throw new RuntimeException();
 }

 public void addRangeValue(RangeValue value)
 {
     rangeValuelist.addElement(value);
 }

/**

 * @return The string representation of the Range object
 */
 public String toString()
 {


  String s="";
  if (multivalue)
    {s+="(";
     for (int i=0; i<allowValuelist.size();i++)
     {
      if (i!=0) s+=" ";
      s+=(Double)allowValuelist.elementAt(i);
     }
     s+=") (";

     for (int i=0; i<blockValuelist.size();i++)
     {
      if (i!=0) s+=" ";
      s+=(Double)blockValuelist.elementAt(i);
     }
     s+=")";

     s+=" ";
     for (int i=0; i<rangeValuelist.size();i++)
     {
      if (i!=0) s+=" ";
      s+=(RangeValue)rangeValuelist.elementAt(i);
     }
    }

    return "\"" + categorytransmit + "\" " + s;
 }

 public String getTransmit()
 {
  return categorytransmit;
 }

 public Vector getAllowValuelist()
 {
  return allowValuelist;
 }

 public Vector getBlockValuelist()
 {
  return blockValuelist;
 }

 public Vector getRangeValuelist()
 {
  return rangeValuelist;
 }

 private boolean multivalue;
 private String categorytransmit;
 private Vector allowValuelist;
 private Vector blockValuelist;
 private Vector rangeValuelist;
}
