//Sven Thmomesen June 26, 2001 private email

//  Paul,

//  I have appended below a re-named and spiffed-up version of the demo 
//  program, with a couple of extra comments added to it. Feel free to post on 
//  your web site, please.

// Sven

/*
  * HDFAppArchiverDemo mgd/pj/snt 06-26-01
  */

/*
  * Rule 1: only data declared 'public' will be saved/restored
  *         by the hdfAppArchiver
  *
  * Rule 2: the 'putShallow$object' method cannot be used to save
  *         an array of primitive types. Use 'putDeep$object' instead.
  */

/*
  * Instead of using the hdfAppArchiver, which is 'built-in',
  * you can create your own archiver object:
  *
  *   String archiverPath = "/my_path/my_archiveName";
  *   HDF5Archiver dataArchiver = new HDF5ArchiverImpl(getZone(), 
archiverPath);
  *   ...
  *   dataArchiver.putDeep$object(dataKey, dataRecord);
  */

/*
  * If your application is built as a subtree using the java 'package'
  * concept, the DataRecord class needs to be defined in its own source
  * file as a public class, with the appropriate 'package' statement
  * at the top.
  */

/*
  * It appears that for the time being, even 'putDeep$object' can only handle
  * arrays of dimension 1; it seems to croak on higher-dimensional objects.
  */

import swarm.Globals;

class DeepDataRecord {

    public int count;
    public int step;
    public int valArray[];

    DeepDataRecord () {
    }

    DeepDataRecord (int count, int step) {

       this.count = count;
       this.step = step;

       valArray = new int[count];
       valArray[0] = 0;
       for (int i = 1; i < count; i++)
           valArray[i] = valArray[i - 1] + step;
    }

}

class ShallowDataRecord {

    public int count;
    public int step;
    int valArray[];

    ShallowDataRecord () {
    }

    ShallowDataRecord (int count, int step) {
       this.count = count;
       this.step = step;

       valArray = new int[count];
       valArray[0] = 0;
       for (int i = 1; i < count; i++)
          valArray[i] = valArray[i - 1] + step;
    }
}


public class HDFAppArchiverDemo {

    HDFAppArchiverDemo () {
    }

    void testDeep () {

       DeepDataRecord deepDataRecord;

       deepDataRecord =
          (DeepDataRecord) Globals.env.hdf5AppArchiver.getObject ("deepdata");

       if (deepDataRecord == null) {

             deepDataRecord = new DeepDataRecord (8, 2);
             Globals.env.hdf5AppArchiver.putDeep$object
                 ("deepdata", deepDataRecord);

       } else {

             System.out.println ("Deep count: " + deepDataRecord.count);
             System.out.println ("Deep step:  " + deepDataRecord.step);

             if (deepDataRecord.valArray == null) {
                     System.out.println("Deep: array is null");
             } else {
                   int lastIndex = deepDataRecord.count - 1;
                   int lastVal = deepDataRecord.valArray[lastIndex];

                   System.out.println
                      ("Deep valArray[" + lastIndex + "]: " + lastVal);
             }
       }
    }


    void testShallow () {

       ShallowDataRecord shallowDataRecord;

       shallowDataRecord =
      (ShallowDataRecord) Globals.env.hdf5AppArchiver.getObject 
("shallowdata");

       if (shallowDataRecord == null) {

             shallowDataRecord = new ShallowDataRecord (6, 3);
             Globals.env.hdf5AppArchiver.putShallow$object
                 ("shallowdata", shallowDataRecord);

       } else {

             System.out.println ("Shallow count: " + shallowDataRecord.count);
             System.out.println ("Shallow step: " + shallowDataRecord.step);

             if (shallowDataRecord.valArray == null) {
                System.out.println("Shallow: array pointer is null");
             } else {
                int lastIndex = shallowDataRecord.count - 1;
                int lastVal = shallowDataRecord.valArray[lastIndex];

                System.out.println
                  ("Shallow valArray[" + lastIndex + "]: " + lastVal);
             }
       }
    }


    static void main (String args[]) {

       Globals.env.initSwarm
          ("HDFAppArchiverDemo", "0.0", "bug-swarm@swarm.org", args);

       HDFAppArchiverDemo demo = new HDFAppArchiverDemo ();

       demo.testDeep ();
       demo.testShallow ();
    }
}


