tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/model/DuplicateValueModifier.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 * Base class for value modifiers
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.model;
       
    20 
       
    21 /**
       
    22  * Base class for value modifiers
       
    23  * 
       
    24  */
       
    25 abstract class DuplicateValueModifier implements TraceObjectModifier {
       
    26 
       
    27 	/**
       
    28 	 * Duplicate tag for values
       
    29 	 */
       
    30 	private static final String DUPLICATE_TAG = "DUP"; //$NON-NLS-1$
       
    31 
       
    32 	/**
       
    33 	 * Duplicate tag separator
       
    34 	 */
       
    35 	private static final char DUPLICATE_SEPARATOR = '_';
       
    36 
       
    37 	/**
       
    38 	 * Value
       
    39 	 */
       
    40 	private String value;
       
    41 
       
    42 	/**
       
    43 	 * Changed flag
       
    44 	 */
       
    45 	private boolean changed;
       
    46 
       
    47 	/**
       
    48 	 * Constructor
       
    49 	 * 
       
    50 	 * @param value
       
    51 	 *            the value
       
    52 	 */
       
    53 	DuplicateValueModifier(String value) {
       
    54 		this.value = value;
       
    55 	}
       
    56 
       
    57 	/*
       
    58 	 * (non-Javadoc)
       
    59 	 * 
       
    60 	 * @see com.nokia.tracecompiler.model.TraceObjectUtils.TraceObjectModifier#getData()
       
    61 	 */
       
    62 	public String getData() {
       
    63 		return value;
       
    64 	}
       
    65 
       
    66 	/*
       
    67 	 * (non-Javadoc)
       
    68 	 * 
       
    69 	 * @see com.nokia.tracecompiler.model.TraceObjectUtils.TraceObjectModifier#hasChanged()
       
    70 	 */
       
    71 	public boolean hasChanged() {
       
    72 		return changed;
       
    73 	}
       
    74 
       
    75 	/**
       
    76 	 * Gets the separator character
       
    77 	 * 
       
    78 	 * @return the char
       
    79 	 */
       
    80 	protected char getSeparator() {
       
    81 		return DUPLICATE_SEPARATOR;
       
    82 	}
       
    83 
       
    84 	/**
       
    85 	 * Processes the name and modifies if there are duplicates
       
    86 	 */
       
    87 	void processName() {
       
    88 		int index = getStartIndex(value);
       
    89 		TraceObject object;
       
    90 		do {
       
    91 			object = findObject(value);
       
    92 			if (object != null) {
       
    93 				changed = true;
       
    94 				value = appendIndexToString(value, index);
       
    95 				index++;
       
    96 			}
       
    97 		} while (object != null);
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	 * Gets the start index from given text
       
   102 	 * 
       
   103 	 * @param value
       
   104 	 *            the value to be parsed
       
   105 	 * @return the start index
       
   106 	 */
       
   107 	private int getStartIndex(String value) {
       
   108 		String valueStr = null;
       
   109 		if (value.startsWith(DUPLICATE_TAG)) {
       
   110 			for (int i = DUPLICATE_TAG.length(); i < value.length(); i++) {
       
   111 				char c = value.charAt(i);
       
   112 				if (!Character.isDigit(c)) {
       
   113 					valueStr = value.substring(DUPLICATE_TAG.length(), i);
       
   114 					i = value.length();
       
   115 				}
       
   116 			}
       
   117 		}
       
   118 		int retval = -1;
       
   119 		if (valueStr != null && valueStr.length() > 0) {
       
   120 			retval = Integer.parseInt(valueStr);
       
   121 		}
       
   122 		return retval;
       
   123 	}
       
   124 
       
   125 	/**
       
   126 	 * Appends an index value to given string replacing existing index if it
       
   127 	 * exists
       
   128 	 * 
       
   129 	 * @param value
       
   130 	 *            the string
       
   131 	 * @param index
       
   132 	 *            the index
       
   133 	 * @return the changed string
       
   134 	 */
       
   135 	private String appendIndexToString(String value, int index) {
       
   136 		StringBuffer newValue = new StringBuffer();
       
   137 		if (index >= 0) {
       
   138 			String indexStr = String.valueOf(index);
       
   139 			newValue.append(DUPLICATE_TAG);
       
   140 			newValue.append(String.valueOf(index + 1));
       
   141 			newValue.append(getSeparator());
       
   142 			newValue.append(value.substring(DUPLICATE_TAG.length()
       
   143 					+ indexStr.length() + 1)); // Add separator
       
   144 		} else {
       
   145 			newValue.append(DUPLICATE_TAG);
       
   146 			newValue.append("1"); //$NON-NLS-1$
       
   147 			newValue.append(getSeparator());
       
   148 			newValue.append(value);
       
   149 		}
       
   150 		return newValue.toString();
       
   151 	}
       
   152 
       
   153 	/**
       
   154 	 * Gets the duplicate modifier from beginning of given text
       
   155 	 * 
       
   156 	 * @param text
       
   157 	 *            the text
       
   158 	 * @return the modifier
       
   159 	 */
       
   160 	static String getModifier(String text) {
       
   161 		String retval = null;
       
   162 		if (text.startsWith(DUPLICATE_TAG)) {
       
   163 			for (int i = DUPLICATE_TAG.length(); i < text.length(); i++) {
       
   164 				char c = text.charAt(i);
       
   165 				if (!Character.isDigit(c)) {
       
   166 					retval = text.substring(0, i + 1); // Add separator
       
   167 					i = text.length();
       
   168 				}
       
   169 			}
       
   170 		}
       
   171 		return retval;
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * Finds the object from model
       
   176 	 * 
       
   177 	 * @param value
       
   178 	 *            the value
       
   179 	 * @return the object or null if not found
       
   180 	 */
       
   181 	abstract TraceObject findObject(String value);
       
   182 
       
   183 }