trace/tracebuilder/com.nokia.tracebuilder/src/com/nokia/tracebuilder/model/TraceObjectUtils.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     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 * Utility functions for checking properties of trace objects
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.model;
       
    20 
       
    21 import java.util.Comparator;
       
    22 
       
    23 /**
       
    24  * Utility functions for checking properties of trace objects.
       
    25  * 
       
    26  */
       
    27 public class TraceObjectUtils {
       
    28 
       
    29 	/**
       
    30 	 * Compares a trace to trace ID
       
    31 	 */
       
    32 	static Comparator<Object> traceToIDComparator = new Comparator<Object>() {
       
    33 
       
    34 		/**
       
    35 		 * Compares a trace to trace name
       
    36 		 * 
       
    37 		 * @param t1
       
    38 		 *            the trace
       
    39 		 * @param t2
       
    40 		 *            the trace name
       
    41 		 * @return the comparison result
       
    42 		 */
       
    43 		public int compare(Object t1, Object t2) {
       
    44 			int n1 = ((Trace) t1).getID();
       
    45 			int n2 = (Integer) t2;
       
    46 			return n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
       
    47 		}
       
    48 
       
    49 	};
       
    50 
       
    51 	/**
       
    52 	 * Compares a trace to trace name
       
    53 	 */
       
    54 	static Comparator<Object> traceToNameComparator = new Comparator<Object>() {
       
    55 
       
    56 		/**
       
    57 		 * Compares a trace to trace name
       
    58 		 * 
       
    59 		 * @param t1
       
    60 		 *            the trace
       
    61 		 * @param t2
       
    62 		 *            the trace name
       
    63 		 * @return the comparison result
       
    64 		 */
       
    65 		public int compare(Object t1, Object t2) {
       
    66 			String n1 = ((Trace) t1).getName();
       
    67 			String n2 = (String) t2;
       
    68 			if (n1 == null) {
       
    69 				n1 = ""; //$NON-NLS-1$
       
    70 			}
       
    71 			if (n2 == null) {
       
    72 				n2 = ""; //$NON-NLS-1$
       
    73 			}
       
    74 			return n1.compareTo(n2);
       
    75 		}
       
    76 
       
    77 	};
       
    78 
       
    79 	/**
       
    80 	 * Compares a trace to trace text
       
    81 	 */
       
    82 	static Comparator<Object> traceToTextComparator = new Comparator<Object>() {
       
    83 
       
    84 		/**
       
    85 		 * Compares a trace to trace text
       
    86 		 * 
       
    87 		 * @param t1
       
    88 		 *            the trace
       
    89 		 * @param t2
       
    90 		 *            the trace text
       
    91 		 * @return the comparison result
       
    92 		 */
       
    93 		public int compare(Object t1, Object t2) {
       
    94 			String n1 = ((Trace) t1).getTrace();
       
    95 			String n2 = (String) t2;
       
    96 			if (n1 == null) {
       
    97 				n1 = ""; //$NON-NLS-1$
       
    98 			}
       
    99 			if (n2 == null) {
       
   100 				n2 = ""; //$NON-NLS-1$
       
   101 			}
       
   102 			return n1.compareTo(n2);
       
   103 		}
       
   104 
       
   105 	};
       
   106 
       
   107 	/**
       
   108 	 * Compares trace objects by ID
       
   109 	 */
       
   110 	static Comparator<TraceObject> traceObjectIDComparator = new Comparator<TraceObject>() {
       
   111 
       
   112 		/**
       
   113 		 * Compares ID's of trace objects
       
   114 		 * 
       
   115 		 * @param t1
       
   116 		 *            trace 1
       
   117 		 * @param t2
       
   118 		 *            trace 2
       
   119 		 * @return the comparison result
       
   120 		 */
       
   121 		public int compare(TraceObject t1, TraceObject t2) {
       
   122 			int n1 = t1.getID();
       
   123 			int n2 = t2.getID();
       
   124 			return n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
       
   125 		}
       
   126 
       
   127 	};
       
   128 
       
   129 	/**
       
   130 	 * Compares trace objects by name
       
   131 	 */
       
   132 	static Comparator<TraceObject> traceObjectNameComparator = new Comparator<TraceObject>() {
       
   133 
       
   134 		/**
       
   135 		 * Compares names of trace objects
       
   136 		 * 
       
   137 		 * @param t1
       
   138 		 *            trace 1
       
   139 		 * @param t2
       
   140 		 *            trace 2
       
   141 		 * @return the comparison result
       
   142 		 */
       
   143 		public int compare(TraceObject t1, TraceObject t2) {
       
   144 			String n1 = t1.getName();
       
   145 			String n2 = t2.getName();
       
   146 			if (n1 == null) {
       
   147 				n1 = ""; //$NON-NLS-1$
       
   148 			}
       
   149 			if (n2 == null) {
       
   150 				n2 = ""; //$NON-NLS-1$
       
   151 			}
       
   152 			return n1.compareTo(n2);
       
   153 		}
       
   154 
       
   155 	};
       
   156 
       
   157 	/**
       
   158 	 * Prevents construction
       
   159 	 */
       
   160 	private TraceObjectUtils() {
       
   161 	}
       
   162 
       
   163 	/**
       
   164 	 * Checks if group name is already in use and changes if it is. The group
       
   165 	 * name is constructed by merging <code>prefix</code>, <code>base</code>
       
   166 	 * and <code>postfix</code> together. If a group with the name already
       
   167 	 * exists, a integer value starting from 1 is added to the <code>base</code>
       
   168 	 * and the name is re-constructed. The integer value is incremented until a
       
   169 	 * matching group is not found.
       
   170 	 * 
       
   171 	 * @param model
       
   172 	 *            the trace model
       
   173 	 * @param name
       
   174 	 *            the group name
       
   175 	 * @return the modifier interface
       
   176 	 */
       
   177 	public static TraceObjectModifier modifyDuplicateGroupName(
       
   178 			TraceModel model, String name) {
       
   179 		DuplicateGroupNameModifier modifier = new DuplicateGroupNameModifier(
       
   180 				model, name);
       
   181 		modifier.processName();
       
   182 		return modifier;
       
   183 	}
       
   184 
       
   185 	/**
       
   186 	 * Checks if trace name is already in use and changes if it is.
       
   187 	 * 
       
   188 	 * @param group
       
   189 	 *            the trace group to be checked
       
   190 	 * @param name
       
   191 	 *            the trace name
       
   192 	 * @return the modifier interface
       
   193 	 */
       
   194 	public static TraceObjectModifier modifyDuplicateTraceName(
       
   195 			TraceGroup group, String name) {
       
   196 		DuplicateTraceNameModifier modifier = new DuplicateTraceNameModifier(
       
   197 				group, name);
       
   198 		modifier.processName();
       
   199 		return modifier;
       
   200 	}
       
   201 
       
   202 	/**
       
   203 	 * Checks if trace name is already in use globally and changes if it is.
       
   204 	 * 
       
   205 	 * @param model
       
   206 	 *            the trace model
       
   207 	 * @param name
       
   208 	 *            the trace name
       
   209 	 * @return the modifier interface
       
   210 	 */
       
   211 	public static TraceObjectModifier modifyDuplicateTraceName(
       
   212 			TraceModel model, String name) {
       
   213 		DuplicateTraceNameModifier modifier = new DuplicateTraceNameModifier(
       
   214 				model, name);
       
   215 		modifier.processName();
       
   216 		return modifier;
       
   217 	}
       
   218 
       
   219 	/**
       
   220 	 * Checks if trace parameter name is already in use and changes if it is.
       
   221 	 * 
       
   222 	 * @param owner
       
   223 	 *            the owner of the parameter
       
   224 	 * @param name
       
   225 	 *            the parameter name
       
   226 	 * @return the modifier interface
       
   227 	 */
       
   228 	public static TraceObjectModifier modifyDuplicateParameterName(Trace owner,
       
   229 			String name) {
       
   230 		DuplicateParameterNameModifier modifier = new DuplicateParameterNameModifier(
       
   231 				owner, name);
       
   232 		modifier.processName();
       
   233 		return modifier;
       
   234 	}
       
   235 
       
   236 	/**
       
   237 	 * Checks if constant table name is already in use and changes if it is
       
   238 	 * 
       
   239 	 * @param model
       
   240 	 *            the trace model
       
   241 	 * @param name
       
   242 	 *            the table name
       
   243 	 * @return the modified name
       
   244 	 */
       
   245 	public static TraceObjectModifier modifyDuplicateConstantTableName(
       
   246 			TraceModel model, String name) {
       
   247 		DuplicateConstantTableNameModifier modifier = new DuplicateConstantTableNameModifier(
       
   248 				model, name);
       
   249 		modifier.processName();
       
   250 		return modifier;
       
   251 	}
       
   252 
       
   253 	/**
       
   254 	 * Checks if constant table entry name is already in use and changes if it
       
   255 	 * is.
       
   256 	 * 
       
   257 	 * @param table
       
   258 	 *            the constant table owning the entry
       
   259 	 * @param name
       
   260 	 *            the constant table entry name
       
   261 	 * @return the modifier interface
       
   262 	 */
       
   263 	public static TraceObjectModifier modifyDuplicateConstantName(
       
   264 			TraceConstantTable table, String name) {
       
   265 		DuplicateConstantNameModifier modifier = new DuplicateConstantNameModifier(
       
   266 				table, name);
       
   267 		modifier.processName();
       
   268 		return modifier;
       
   269 	}
       
   270 
       
   271 	/**
       
   272 	 * Gets the duplicate modifier from given text
       
   273 	 * 
       
   274 	 * @param text
       
   275 	 *            the text
       
   276 	 * @return the duplicate modifier
       
   277 	 */
       
   278 	public static String removeDuplicateModifier(String text) {
       
   279 		String retval;
       
   280 		String s = DuplicateValueModifier.getModifier(text);
       
   281 		if (s != null) {
       
   282 			retval = text.substring(s.length());
       
   283 		} else {
       
   284 			retval = text;
       
   285 		}
       
   286 		return retval;
       
   287 	}
       
   288 
       
   289 	/**
       
   290 	 * Finds a property from a trace object. This returns an empty string if not
       
   291 	 * found
       
   292 	 * 
       
   293 	 * @param object
       
   294 	 *            the object
       
   295 	 * @param name
       
   296 	 *            the property name
       
   297 	 * @return the property value
       
   298 	 */
       
   299 	public static String findProperty(TraceObject object, String name) {
       
   300 		String retval = null;
       
   301 		TraceObjectPropertyList propertyList = object
       
   302 				.getExtension(TraceObjectPropertyList.class);
       
   303 		if (propertyList != null) {
       
   304 			TraceObjectProperty property = propertyList.getProperty(name);
       
   305 			if (property != null) {
       
   306 				retval = property.getValue();
       
   307 			}
       
   308 		}
       
   309 		if (retval == null) {
       
   310 			retval = ""; //$NON-NLS-1$
       
   311 		}
       
   312 		return retval;
       
   313 	}
       
   314 
       
   315 }