
// Copyright (c) 2001
package uk.co.patrickhaston.mars;

import uk.co.patrickhaston.pdharray2d.PdhArray2d;
import uk.co.patrickhaston.pdhdata.PdhArray;
import java.util.Vector;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

/**
 * A Class class.
 * <P>
 * @author Patrick Haston
 */
public abstract class MarsArray extends PdhArray
{
  protected String CollectionName;
  public MarsModel theModel;


  /**
   * Constructor
   */
  public MarsArray()
  {
    setCollectionName("[MarsArray]");
  }

  public MarsArray(MarsModel model)
  {
    setCollectionName("[MarsArray]");
    theModel = model;
  }

  public int readIndex(String line)
  {
    //String restOfLine;
    int idx = line.indexOf("=");
    Integer val = null;

    if (idx > 0)
    {
      val = new Integer(line.substring(0, idx));
      return val.intValue();
    }

    return -1;
  }


  public abstract void readData(String line);

  public void writeData(BufferedWriter bOut)
  {
    try
    {
      bOut.write(CollectionName);
      bOut.newLine();

      if (this.size()>1)
      {
        for (int i=1; i<size(); i++)
        {
          bOut.write("" + i + "=" + ((MarsData) this.elementAt(i)).toFile() );
          bOut.newLine();
        }
      }
      bOut.newLine();
    }
    catch (IOException i)
    {
      //
    }
  }

  public void setCollectionName(String c)
  {
    CollectionName = c; 
  }

  public void addElement(Object obj)
  {
    //((MarsData) p0).datum_active = true;
    if(size() == 0)
    {
      super.addElement(null);
    }
    super.addElement(obj);
  }

  public void setElementAt(Object obj, int index)
  {
    //((MarsData) p0).datum_active = true;
    while (index>=size())
    {
      super.addElement(null);
    }
    super.setElementAt(obj, index);
  }

  public void update()
  {
    // Update the array for today
    if (this.size() > 1)
    {
      for(int i=1; i<this.size(); i++)
      {
        try
        {
          ((MarsData)this.elementAt(i)).update(theModel);
        }
        catch (Exception e)
        {
          theModel.logError("Exception in MarsArray.update() for: " + this.CollectionName 
            + " element: " + Integer.toString(i) + " details: " + e.toString());
        }
      }
    }
  }

}


