tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/model/TraceGroup.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     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 a logical collection of traces somehow related to each other
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.model;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collections;
       
    23 import java.util.Iterator;
       
    24 
       
    25 /**
       
    26  * Represents a logical collection of traces somehow related to each other. Each
       
    27  * trace group specifies a prefix that is attached to all trace lines made
       
    28  * within that group.
       
    29  * 
       
    30  */
       
    31 public class TraceGroup extends TraceObject implements Iterable<Trace> {
       
    32 
       
    33 	/**
       
    34 	 * List of traces, sorted by ID
       
    35 	 */
       
    36 	private ArrayList<Trace> tracesByID = new ArrayList<Trace>();
       
    37 
       
    38 	/**
       
    39 	 * List of traces, sorted by name
       
    40 	 */
       
    41 	private ArrayList<Trace> tracesByName = new ArrayList<Trace>();
       
    42 
       
    43 	/**
       
    44 	 * Creates a new trace group and associates it with given trace model.
       
    45 	 * 
       
    46 	 * @param model
       
    47 	 *            the trace model
       
    48 	 */
       
    49 	TraceGroup(TraceModel model) {
       
    50 		setModel(model);
       
    51 		model.addGroup(this);
       
    52 	}
       
    53 
       
    54 	/**
       
    55 	 * Adds a trace to this group. Creates objectAdded event to model listeners
       
    56 	 * 
       
    57 	 * @see TraceModelListener#objectAdded(TraceObject, TraceObject)
       
    58 	 * @param trace
       
    59 	 *            the trace to be added
       
    60 	 */
       
    61 	void addTrace(Trace trace) {
       
    62 		// Sorted when ID is set to trace
       
    63 		tracesByID.add(trace);
       
    64 		// Sorted when name is set to trace
       
    65 		tracesByName.add(trace);
       
    66 		getModel().notifyObjectAdded(this, trace);
       
    67 	}
       
    68 
       
    69 	/**
       
    70 	 * Removes a trace from this group. Creates objectRemoved event to model
       
    71 	 * listeners
       
    72 	 * 
       
    73 	 * @see TraceModelListener#objectRemoved(TraceObject, TraceObject)
       
    74 	 * @param trace
       
    75 	 *            the trace to be removed
       
    76 	 * @throws TraceCompilerException 
       
    77 	 */
       
    78 	public void removeTrace(Trace trace) throws TraceCompilerException {
       
    79 		int index = Collections.binarySearch(tracesByID, trace,
       
    80 				TraceObjectUtils.traceObjectIDComparator);
       
    81 		if (index >= 0) {
       
    82 			tracesByID.remove(index);
       
    83 			index = Collections.binarySearch(tracesByName, trace,
       
    84 					TraceObjectUtils.traceObjectNameComparator);
       
    85 			tracesByName.remove(index);
       
    86 			getModel().notifyObjectRemoved(this, trace);
       
    87 			trace.reset();
       
    88 		}
       
    89 	}
       
    90 
       
    91 	/**
       
    92 	 * Determines if this group has traces.
       
    93 	 * 
       
    94 	 * @return true if the group contains traces
       
    95 	 */
       
    96 	public boolean hasTraces() {
       
    97 		return !tracesByID.isEmpty();
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	 * Gets the number of traces in this group.
       
   102 	 * 
       
   103 	 * @return the number of traces
       
   104 	 */
       
   105 	public int getTraceCount() {
       
   106 		return tracesByID.size();
       
   107 	}
       
   108 
       
   109 	/**
       
   110 	 * Returns an iterator over the traces within this group.
       
   111 	 * 
       
   112 	 * @return the iterator
       
   113 	 */
       
   114 	public Iterator<Trace> getTraces() {
       
   115 		return tracesByID.iterator();
       
   116 	}
       
   117 
       
   118 	/*
       
   119 	 * (non-Javadoc)
       
   120 	 * 
       
   121 	 * @see java.lang.Iterable#iterator()
       
   122 	 */
       
   123 	public Iterator<Trace> iterator() {
       
   124 		return tracesByID.iterator();
       
   125 	}
       
   126 
       
   127 	/*
       
   128 	 * (non-Javadoc)
       
   129 	 * 
       
   130 	 * @see com.nokia.tracecompiler.model.TraceObject#reset()
       
   131 	 */
       
   132 	@Override
       
   133 	void reset() {
       
   134 		// Reset removes constant table references
       
   135 		for (Trace trace : tracesByID) {
       
   136 			// Model listeners are not notified on reset, but if the
       
   137 			// trace itself implements a delete notification interface,
       
   138 			// it must be called to do appropriate cleanup
       
   139 			notifyOnDelete(trace);
       
   140 			trace.reset();
       
   141 		}
       
   142 		tracesByID.clear();
       
   143 		tracesByName.clear();
       
   144 		super.reset();
       
   145 	}
       
   146 
       
   147 	/**
       
   148 	 * Returns highest trace ID + 1. Can be used to create an unique ID for a
       
   149 	 * new trace.
       
   150 	 * 
       
   151 	 * @return the next trace ID
       
   152 	 */
       
   153 	public int getNextTraceID() {
       
   154 		int ret;
       
   155 		if (tracesByID.size() > 0) {
       
   156 			ret = tracesByID.get(tracesByID.size() - 1).getID() + 1;
       
   157 		} else {
       
   158 			ret = 1;
       
   159 		}
       
   160 		return ret;
       
   161 	}
       
   162 
       
   163 	/**
       
   164 	 * Gets the trace which has given ID
       
   165 	 * 
       
   166 	 * @param id
       
   167 	 *            the trace ID
       
   168 	 * @return the trace or null
       
   169 	 */
       
   170 	public Trace findTraceByID(int id) {
       
   171 		int index = Collections.binarySearch(tracesByID, id,
       
   172 				TraceObjectUtils.traceToIDComparator);
       
   173 		Trace retval;
       
   174 		if (index >= 0) {
       
   175 			retval = tracesByID.get(index);
       
   176 		} else {
       
   177 			retval = null;
       
   178 		}
       
   179 		return retval;
       
   180 	}
       
   181 
       
   182 	/**
       
   183 	 * Called by the model when a trace property is updated
       
   184 	 * 
       
   185 	 * @param source
       
   186 	 *            the trace that was changed
       
   187 	 * @param property
       
   188 	 *            the property that was changed
       
   189 	 */
       
   190 	void tracePropertyUpdated(TraceObject source, int property) {
       
   191 		if (property == TraceModelListener.NAME) {
       
   192 			Collections.sort(tracesByName,
       
   193 					TraceObjectUtils.traceObjectNameComparator);
       
   194 		} else if (property == TraceModelListener.ID) {
       
   195 			traceIDsUpdated();
       
   196 		}
       
   197 	}
       
   198 
       
   199 	/**
       
   200 	 * Sorts the trace array based on new ID configuration
       
   201 	 */
       
   202 	void traceIDsUpdated() {
       
   203 		Collections.sort(tracesByID, TraceObjectUtils.traceObjectIDComparator);
       
   204 	}
       
   205 }