Next: On-Demand Data Loading: The Up: The Horizon Metadata Model Previous: Protecting Metadata Via Defaults

## Handling Arrays: The Horizon `Metavector` Class

As was alluded to in the previous section, the `Metavector` class is a container for holding array metadata of any type. Instead of accessing data by name, one access them by integer index. It is very similar to the `java.util.Vector` class, sharing much of the same interface and methods. Elements can be added, inserted, and removed much like a `Vector`:

    Metavector mv = new Metavector();
    mv.addElement(new Double(1.2));          // adds element at position 0
    mv.setElementAt(new Double(4.0), 2);     // sets element at position 2
    mv.addElement(new Double(3));            // adds element at position 3
    mv.setElementAt(new Double(4.0), 1);     // sets element at position 1
    mv.insertElementAt(new Double(4.0), 1);  // inserts element at position 1,
                                             //   shifting elements 1-3 up by one
    mv.removeElementAt(0);                   // deletes element 0, shifting the
                                             //   remaining elements down by one
    mv.eraseElementAt(2);                    // sets element 2 to null

The size of the `Metavector` will grow as necessary. Accessing the values is also much like with the `Vector` class:

    int sz = mv.size();
    Double[] dval = new Double[sz];
    for(int i=0; i < sz; i++) {
        Double dval = (Double) mv.elementAt(i);
    }

As with the `Metadata` class, one needs to know ahead of time what type the `Metavector` holds so that the values can be cast to the proper type. As one can see from the above example, it usually makes sense to only store one type of data in a `Metavector` object; though, as with a `Vector`, there is nothing stopping you from living dangerously.

There are some important ways in which a `Metavector` is different from a regular `Vector` which are related to how it is like a `Metadata` object. Like the `Metadata`, a `Metavector` contains an internal `Metavector` that holds default values which are set at construction and can not be publicly updated. If a client object calls `elementAt(int)` and the value in the primary array is null, the value stored in the default `Metavector` at the same position is returned. Because of the existance of the default array, important restrictions are placed on the methods that update the array elements. First, one is not allowed to reduce the size of `Metavector` to less than the size of its default `Metavector`. Furthermore, one cannot remove an element at a position index that is less than the size of the default list. One can erase a value at any position, which sets the value to null, exposing possible default values. To reflect these restrictions, each update method returns a `boolean` indicating whether the update was allowed and successful.

The purpose of the `Metavector` class is to provide a convenient and safe way to store array data within a `Metadata` object. The convenience comes in the way the `Metadata` class treats `Metavector`s as a type of sub-metadata. For instance, suppose that a `Metavector` is stored an element of a `Metadata` object with the name ``Axes''; one can access an element of that `Metavector` directly from the `Metadata` object:

    Metadata md;
    ...
    Object element = md.getMetadatum("Axes[1]");

This returns the object at index 1 (the second element) of the Metavector called ``Axes''. Furthermore, if the elements held within the `Metavector` are of type `Metadata`, then access to further sub-sub-metadata is possible:

    String name = (String) md.getMetadatum("coordinates.Axes[1].name");

Thus, the ``sub-metadata'' syntax provides access to hierarchical metadata of arbitrary depth. (If the `Metavector` does not contain `Metadata` elements, the above statement will return `null`.) Remember, one cannot use the ``sub-metadata'' syntax to update hierarchical metadata.

Next: On-Demand Data Loading: The Up: The Horizon Metadata Model Previous: Protecting Metadata Via Defaults