tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/model/Trace.java
branchRCL_3
changeset 20 ca8a1b6995f6
equal deleted inserted replaced
19:07b41fa8d1dd 20:ca8a1b6995f6
       
     1 /*
       
     2 * Copyright (c) 2007 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 * Represents the mapping of a trace ID into actual trace string
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.model;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Iterator;
       
    23 
       
    24 /**
       
    25  * Represents the mapping of a trace ID into actual trace string. When a trace
       
    26  * is decoded, the trace ID found from the binary trace file is converted to the
       
    27  * text stored in this trace.
       
    28  * 
       
    29  */
       
    30 public class Trace extends TraceObject implements Iterable<TraceParameter> {
       
    31 
       
    32 	/**
       
    33 	 * The trace string
       
    34 	 */
       
    35 	private String trace = ""; //$NON-NLS-1$
       
    36 
       
    37 	/**
       
    38 	 * List of TraceParameter objects.
       
    39 	 */
       
    40 	private ArrayList<TraceParameter> parameters = new ArrayList<TraceParameter>();
       
    41 
       
    42 	/**
       
    43 	 * The trace group this trace belongs to
       
    44 	 */
       
    45 	private TraceGroup group;
       
    46 
       
    47 	/**
       
    48 	 * Creates a new trace and associates it with given TraceGroup
       
    49 	 * 
       
    50 	 * @param group
       
    51 	 *            the trace group
       
    52 	 */
       
    53 	Trace(TraceGroup group) {
       
    54 		setModel(group.getModel());
       
    55 		this.group = group;
       
    56 		group.addTrace(this);
       
    57 	}
       
    58 
       
    59 	/*
       
    60 	 * (non-Javadoc)
       
    61 	 * 
       
    62 	 * @see com.nokia.tracecompiler.model.TraceObject#reset()
       
    63 	 */
       
    64 	@Override
       
    65 	void reset() {
       
    66 		super.reset();
       
    67 		for (TraceParameter parameter : parameters) {
       
    68 			// Model listeners are not notified on reset, but if the
       
    69 			// parameter itself implements a delete notification interface,
       
    70 			// it must be called to do appropriate cleanup
       
    71 			notifyOnDelete(parameter);
       
    72 			parameter.reset();
       
    73 		}
       
    74 		parameters.clear();
       
    75 	}
       
    76 
       
    77 	/**
       
    78 	 * Sets the trace string. Generates propertyUpdated event to model listeners
       
    79 	 * if the string changes
       
    80 	 * 
       
    81 	 * @see TraceModelListener#propertyUpdated(TraceObject, int)
       
    82 	 * @param trace
       
    83 	 *            the trace string
       
    84 	 * @throws TraceCompilerException 
       
    85 	 */
       
    86 	public void setTrace(String trace) throws TraceCompilerException {
       
    87 		if (trace == null) {
       
    88 			trace = ""; //$NON-NLS-1$
       
    89 		}
       
    90 		if (!trace.equals(this.trace)) {
       
    91 			this.trace = trace;
       
    92 			getModel().notifyPropertyUpdated(this, TraceModelListener.TRACE);
       
    93 		}
       
    94 	}
       
    95 
       
    96 	/**
       
    97 	 * Returns the trace string. This never returns null
       
    98 	 * 
       
    99 	 * @return the trace string
       
   100 	 */
       
   101 	public String getTrace() {
       
   102 		return trace;
       
   103 	}
       
   104 
       
   105 	/**
       
   106 	 * Returns the group to which this trace belongs
       
   107 	 * 
       
   108 	 * @return trace group
       
   109 	 */
       
   110 	public TraceGroup getGroup() {
       
   111 		return group;
       
   112 	}
       
   113 
       
   114 	/**
       
   115 	 * Adds a new parameter to the end of parameter list. Generates objectAdded
       
   116 	 * event to model listeners
       
   117 	 * 
       
   118 	 * @see TraceModelListener#objectAdded(TraceObject, TraceObject)
       
   119 	 * @param parameter
       
   120 	 *            the new parameter
       
   121 	 */
       
   122 	public void addParameter(TraceParameter parameter) {
       
   123 		parameters.add(parameter);
       
   124 		getModel().notifyObjectAdded(this, parameter);
       
   125 	}
       
   126 
       
   127 	/**
       
   128 	 * Inserts a new parameter to the given location. Generates objectAdded
       
   129 	 * event to model listeners
       
   130 	 * 
       
   131 	 * @see TraceModelListener#objectAdded(TraceObject, TraceObject)
       
   132 	 * @param index
       
   133 	 *            the index for new parameter
       
   134 	 * @param parameter
       
   135 	 *            the new parameter
       
   136 	 */
       
   137 	void insertParameter(int index, TraceParameter parameter) {
       
   138 		parameters.add(index, parameter);
       
   139 		getModel().notifyObjectAdded(this, parameter);
       
   140 	}
       
   141 
       
   142 	/**
       
   143 	 * Removes a parameter from this trace object. Generates a objectRemoved
       
   144 	 * event to model listeners if the parameter is actually removed
       
   145 	 * 
       
   146 	 * @see TraceModelListener#objectRemoved(TraceObject, TraceObject)
       
   147 	 * @param parameter
       
   148 	 *            the parameter to be removed
       
   149 	 * @throws TraceCompilerException 
       
   150 	 */
       
   151 	public void removeParameter(TraceParameter parameter) throws TraceCompilerException {
       
   152 		if (parameters.remove(parameter)) {
       
   153 			getModel().notifyObjectRemoved(this, parameter);
       
   154 			parameter.reset();
       
   155 		}
       
   156 	}
       
   157 
       
   158 	/**
       
   159 	 * Removes parameter at given index
       
   160 	 * 
       
   161 	 * @param index
       
   162 	 *            the index of the parameter
       
   163 	 * @throws TraceCompilerException 
       
   164 	 */
       
   165 	public void removeParameterAt(int index) throws TraceCompilerException {
       
   166 		if (index >= 0 && index < parameters.size()) {
       
   167 			TraceParameter parameter = parameters.get(index);
       
   168 			parameters.remove(index);
       
   169 			getModel().notifyObjectRemoved(this, parameter);
       
   170 			parameter.reset();
       
   171 		}
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * Determines if this object has parameters.
       
   176 	 * 
       
   177 	 * @return true if this object has parameters
       
   178 	 */
       
   179 	public boolean hasParameters() {
       
   180 		return !parameters.isEmpty();
       
   181 	}
       
   182 
       
   183 	/**
       
   184 	 * Gets the number of parameters
       
   185 	 * 
       
   186 	 * @return the number of parameters
       
   187 	 */
       
   188 	public int getParameterCount() {
       
   189 		return parameters.size();
       
   190 	}
       
   191 
       
   192 	/**
       
   193 	 * Returns highest parameter ID + 1. Can be used to create an unique ID for
       
   194 	 * a new parameter.
       
   195 	 * 
       
   196 	 * @return the next parameter ID
       
   197 	 */
       
   198 	public int getNextParameterID() {
       
   199 		int max = 0;
       
   200 		for (TraceParameter parameter : parameters) {
       
   201 			int id = parameter.getID();
       
   202 			if (id > max) {
       
   203 				max = id;
       
   204 			}
       
   205 		}
       
   206 		return max + 1;
       
   207 	}
       
   208 
       
   209 	/**
       
   210 	 * Gets the parameter at given index
       
   211 	 * 
       
   212 	 * @param index
       
   213 	 *            the parameter index
       
   214 	 * @return the parameter at the index
       
   215 	 */
       
   216 	public TraceParameter getParameter(int index) {
       
   217 		return parameters.get(index);
       
   218 	}
       
   219 
       
   220 	/**
       
   221 	 * Returns the parameters of this object
       
   222 	 * 
       
   223 	 * @return iterator over the parameters
       
   224 	 */
       
   225 	public Iterator<TraceParameter> getParameters() {
       
   226 		return parameters.iterator();
       
   227 	}
       
   228 
       
   229 	/*
       
   230 	 * (non-Javadoc)
       
   231 	 * 
       
   232 	 * @see java.lang.Iterable#iterator()
       
   233 	 */
       
   234 	public Iterator<TraceParameter> iterator() {
       
   235 		return parameters.iterator();
       
   236 	}
       
   237 
       
   238 	/**
       
   239 	 * Gets a parameter by ID
       
   240 	 * 
       
   241 	 * @param id
       
   242 	 *            the parameter ID
       
   243 	 * @return the parameter or null
       
   244 	 */
       
   245 	public TraceParameter findParameterByID(int id) {
       
   246 		TraceParameter retval = null;
       
   247 		for (TraceParameter parameter : parameters) {
       
   248 			if (parameter.getID() == id) {
       
   249 				retval = parameter;
       
   250 				break;
       
   251 			}
       
   252 		}
       
   253 		return retval;
       
   254 	}
       
   255 
       
   256 	/**
       
   257 	 * Gets a parameter by name
       
   258 	 * 
       
   259 	 * @param name
       
   260 	 *            the name of a parameter
       
   261 	 * @return the parameter or null
       
   262 	 */
       
   263 	public TraceParameter findParameterByName(String name) {
       
   264 		TraceParameter retval = null;
       
   265 		for (TraceParameter parameter : parameters) {
       
   266 			if (parameter.getName().equals(name)) {
       
   267 				retval = parameter;
       
   268 				break;
       
   269 			}
       
   270 		}
       
   271 		return retval;
       
   272 	}
       
   273 }