package com.ibm.nmon.data.transform;

import com.ibm.nmon.data.DataType;

/**
 * Adds <code>used</code> columns to AIX MEM statistics in addition to the default <code>free</code>
 * data.
 */
public final class AIXMemoryTransform implements DataTransform {
    @Override
    public DataType buildDataType(String id, String subId, String name, String... fields) {
        String[] newFields = new String[fields.length + 4];

        // Real Free % and Virtual free %
        newFields[0] = fields[0];
        newFields[1] = fields[1];

        newFields[2] = "Real used %";
        newFields[3] = "Virtual used %";

        // Real free(MB) and Virtual free(MB)
        newFields[4] = fields[2];
        newFields[5] = fields[3];

        newFields[6] = "Real used(MB)";
        newFields[7] = "Virtual used(MB)";

        for (int i = 4; i < fields.length; i++) {
            newFields[i + 4] = fields[i];
        }

        return new DataType(id, name, newFields);
    }

    @Override
    public double[] transform(DataType type, double[] data) {
        double[] newData = new double[data.length + 4];

        newData[0] = data[0];
        newData[1] = data[1];

        // free % = 100 - used
        newData[2] = 100 - data[0];
        newData[3] = 100 - data[1];

        newData[4] = data[2];
        newData[5] = data[3];

        // free = total - used
        newData[6] = data[4] - data[2];
        newData[7] = data[5] - data[3];

        newData[8] = data[4];
        newData[9] = data[5];

        return newData;
    }

    @Override
    public boolean isValidFor(String typeId, String subId) {
        return "MEM".equals(typeId);
    }
}

@@ -1502,29 +1502,47 @@
     
-newData%5B8%5D = data%5B4%5D;
+for (int i = 4; i %3C data.length; i++) %7B
 %0D%0A  
@@ -1547,24 +1547,28 @@
         
+    
 newData%5B
 9%5D = dat
@@ -1563,9 +1563,13 @@
 ata%5B
-9
+i + 4
 %5D = 
@@ -1577,12 +1577,23 @@
 ata%5B
-5
+i
 %5D;%0D
+%0A        %7D%0D
 %0A%0D%0A 

Fix issue #3: Changed transform method to copy data similar to buildDataType