trace/traceviewer/com.nokia.trace.dictionary/src/com/nokia/trace/dictionary/model/decodeparameters/ArrayParameter.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Array parameter
       
    17  *
       
    18  */
       
    19 package com.nokia.trace.dictionary.model.decodeparameters;
       
    20 
       
    21 import java.nio.ByteBuffer;
       
    22 import java.util.ArrayList;
       
    23 import java.util.List;
       
    24 
       
    25 /**
       
    26  * Array parameter
       
    27  * 
       
    28  */
       
    29 public final class ArrayParameter extends DecodeParameter {
       
    30 
       
    31 	/**
       
    32 	 * Reference to a type that this array is full of
       
    33 	 */
       
    34 	private DecodeParameter parameterType;
       
    35 
       
    36 	/**
       
    37 	 * Length in bytes of length data in binary format
       
    38 	 */
       
    39 	private static final int LENGTH_OF_ARRAYLENGTH = 4;
       
    40 
       
    41 	/**
       
    42 	 * Datablock size
       
    43 	 */
       
    44 	private static final int DATABLOCK_SIZE = 4;
       
    45 
       
    46 	/**
       
    47 	 * Constructor
       
    48 	 * 
       
    49 	 * @param type
       
    50 	 *            type
       
    51 	 * @param hidden
       
    52 	 *            hidden value
       
    53 	 * @param parameterType
       
    54 	 *            parameterType
       
    55 	 */
       
    56 	public ArrayParameter(String type, boolean hidden,
       
    57 			DecodeParameter parameterType) {
       
    58 		super(type, hidden);
       
    59 		this.parameterType = parameterType;
       
    60 	}
       
    61 
       
    62 	/*
       
    63 	 * (non-Javadoc)
       
    64 	 * 
       
    65 	 * @see
       
    66 	 * com.nokia.trace.dictionary.model.decodeparameters.DecodeParameter#decode
       
    67 	 * (java.nio.ByteBuffer, int, java.lang.StringBuffer, int, int,
       
    68 	 * java.util.List)
       
    69 	 */
       
    70 	@Override
       
    71 	public int decode(ByteBuffer dataFrame, int offset,
       
    72 			StringBuffer traceString, int dataStart, int dataLength,
       
    73 			List<String> parameterList) {
       
    74 
       
    75 		// Check that there is enough data left in the buffer
       
    76 		int bytesRemaining = dataLength - (offset - dataStart);
       
    77 		if (bytesRemaining <= 0) {
       
    78 			postDataMissingEvent(traceString, 0, LENGTH_OF_ARRAYLENGTH);
       
    79 		}
       
    80 
       
    81 		int parameterLen = parameterType.getSize();
       
    82 		int arrayLength;
       
    83 
       
    84 		// If the only variable in the trace, length is the data left
       
    85 		if (isOnlyVariableInTrace) {
       
    86 			arrayLength = bytesRemaining / parameterLen;
       
    87 
       
    88 			// Read the length if this array is not the only variable in the
       
    89 			// trace
       
    90 		} else {
       
    91 
       
    92 			arrayLength = DecodeUtils.getIntegerFromBuffer(dataFrame, offset,
       
    93 					LENGTH_OF_ARRAYLENGTH);
       
    94 
       
    95 			offset = offset + LENGTH_OF_ARRAYLENGTH;
       
    96 			dataFrame.position(offset);
       
    97 		}
       
    98 
       
    99 		List<String> arrayParameterString = new ArrayList<String>();
       
   100 		StringBuffer arrayParameterBuf = new StringBuffer();
       
   101 
       
   102 		// Calculate how many elements there are in the array
       
   103 		int nrOfElements = arrayLength;
       
   104 
       
   105 		// Decode all the parameters from the array
       
   106 		for (int i = 0; i < nrOfElements
       
   107 				&& offset + parameterLen <= dataStart + dataLength; i++) {
       
   108 
       
   109 			offset = parameterType.decode(dataFrame, offset, traceString,
       
   110 					dataStart, dataLength, arrayParameterString);
       
   111 
       
   112 			// Add next array element to array parameter String
       
   113 			if (arrayParameterString.size() > 0) {
       
   114 				arrayParameterBuf.append(arrayParameterString.get(0));
       
   115 				arrayParameterString.clear();
       
   116 			}
       
   117 
       
   118 			// Append commas between elements
       
   119 			if (i != nrOfElements - 1) {
       
   120 				traceString.append(',');
       
   121 				arrayParameterBuf.append(',');
       
   122 			}
       
   123 		}
       
   124 
       
   125 		parameterList.add(arrayParameterBuf.toString());
       
   126 
       
   127 		// Check fillers
       
   128 		int fillerCount = DATABLOCK_SIZE
       
   129 				- ((arrayLength * parameterLen) % DATABLOCK_SIZE);
       
   130 		if (fillerCount == DATABLOCK_SIZE) {
       
   131 			// All good
       
   132 		} else {
       
   133 			// Add fillerCount to offset
       
   134 			offset = offset + fillerCount;
       
   135 		}
       
   136 
       
   137 		return offset;
       
   138 	}
       
   139 
       
   140 	/**
       
   141 	 * Gets parameter type
       
   142 	 * 
       
   143 	 * @return the parameterType
       
   144 	 */
       
   145 	public DecodeParameter getParameterType() {
       
   146 		return parameterType;
       
   147 	}
       
   148 
       
   149 	/**
       
   150 	 * Sets parameter type
       
   151 	 * 
       
   152 	 * @param parameterType
       
   153 	 *            the parameterType to set
       
   154 	 */
       
   155 	public void setParameterType(DecodeParameter parameterType) {
       
   156 		this.parameterType = parameterType;
       
   157 	}
       
   158 }