Home | History | Annotate | Download | only in db
      1 /*******************************************************************************
      2  * Copyright (c) 2000, 2009 IBM Corporation and others.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *     IBM Corporation - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.test.internal.performance.results.db;
     12 
     13 import java.io.PrintStream;
     14 import java.util.ArrayList;
     15 import java.util.Iterator;
     16 import java.util.List;
     17 
     18 import org.eclipse.test.internal.performance.results.utils.Util;
     19 
     20 
     21 /**
     22  * Abstract class to store performance results.
     23  *
     24  * Each results gives access to specific children depending on model.
     25  */
     26 public abstract class AbstractResults implements Comparable {
     27 
     28 	public static final double[] INVALID_RESULTS = new double[] {2};
     29 	public static final double[] NO_BUILD_RESULTS = new double[0];
     30 	public static final int BUILD_VALUE_INDEX = 0;
     31 	public static final int BASELINE_VALUE_INDEX = 1;
     32 	public static final int DELTA_VALUE_INDEX = 2;
     33 	public static final int DELTA_ERROR_INDEX = 3;
     34 	public static final int BUILD_ERROR_INDEX = 4;
     35 	public static final int BASELINE_ERROR_INDEX = 5;
     36 	public static final int NUMBERS_LENGTH = 6;
     37 
     38 	AbstractResults parent;
     39 	int id = -1;
     40 	String name;
     41 	List children;
     42 	private static boolean NEW_LINE = true;
     43 	PrintStream printStream = null;
     44 
     45 AbstractResults(AbstractResults parent, String name) {
     46 	this.parent = parent;
     47 	this.children = new ArrayList();
     48 	this.name = name;
     49 }
     50 
     51 AbstractResults(AbstractResults parent, int id) {
     52 	this.parent = parent;
     53 	this.children = new ArrayList();
     54 	this.id = id;
     55 }
     56 
     57 /*
     58  * Add a child to current results, using specific sort
     59  * order if specified.
     60  */
     61 void addChild(Comparable child, boolean sort) {
     62 	if (sort) {
     63 		int size = this.children.size();
     64 		for (int i=0; i<size; i++) {
     65 			Object results = this.children.get(i);
     66 			if (child.compareTo(results) < 0) {
     67 				this.children.add(i, child);
     68 				return;
     69 			}
     70 		}
     71 	}
     72 	this.children.add(child);
     73 }
     74 
     75 /**
     76  * Compare the results to the given one using the name.
     77  *
     78  * @see java.lang.Comparable#compareTo(java.lang.Object)
     79  */
     80 public int compareTo(Object obj) {
     81 	if (obj instanceof AbstractResults) {
     82 		AbstractResults res = (AbstractResults) obj;
     83 		return getName().compareTo(res.getName());
     84 	}
     85 	return -1;
     86 }
     87 
     88 /**
     89  * Returns whether two results are equals using the name
     90  * to compare them.
     91  *
     92  * @param obj  The results to compare with
     93  * @return <code>true</code> if the name are equals,
     94  * 	<code>false</code> otherwise
     95  * @see java.lang.Comparable#compareTo(java.lang.Object)
     96  */
     97 public boolean equals(Object obj) {
     98 	if (obj instanceof AbstractResults) {
     99 		return this.name.equals(((AbstractResults)obj).getName());
    100 	}
    101 	return super.equals(obj);
    102 }
    103 
    104 /**
    105  * Return an array built on the current results children list.
    106  *
    107  * @return An array of the children list
    108  */
    109 public AbstractResults[] getChildren() {
    110 	AbstractResults[] elements = new AbstractResults[size()];
    111 	this.children.toArray(elements);
    112 	return elements;
    113 }
    114 
    115 ComponentResults getComponentResults() {
    116 	if (this.parent != null) {
    117 		return this.parent.getComponentResults();
    118 	}
    119 	return null;
    120 }
    121 
    122 int getId() {
    123 	return this.id;
    124 }
    125 
    126 /**
    127  * Returns the name of the results object.
    128  *
    129  * @return The name of the results
    130  */
    131 public String getName() {
    132 	return this.name;
    133 }
    134 
    135 /**
    136  * Returns the parent
    137  *
    138  * @return The parent
    139  */
    140 public AbstractResults getParent() {
    141 	return this.parent;
    142 }
    143 
    144 PerformanceResults getPerformance() {
    145 	if (this.parent != null) {
    146 		return this.parent.getPerformance();
    147 	}
    148 	return null;
    149 }
    150 
    151 String getPath() {
    152 	String path = this.parent==null || this.parent.parent==null ? "" : this.parent.getPath() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
    153 	return path+this.name;
    154 }
    155 
    156 /**
    157  * Return the children list of the current results.
    158  *
    159  * @return An iterator on the children list
    160  */
    161 public Iterator getResults() {
    162 	return this.children.iterator();
    163 }
    164 
    165 AbstractResults getResults(String resultName) {
    166 	int size = this.children.size();
    167 	for (int i=0; i<size; i++) {
    168 		AbstractResults searchedResults = (AbstractResults) this.children.get(i);
    169 		if (searchedResults.getName().equals(resultName)) {
    170 			return searchedResults;
    171 		}
    172 	}
    173 	return null;
    174 }
    175 
    176 AbstractResults getResults(int searchedId) {
    177 	int size = this.children.size();
    178 	for (int i=0; i<size; i++) {
    179 		AbstractResults searchedResults = (AbstractResults) this.children.get(i);
    180 		if (searchedResults.id == searchedId) {
    181 			return searchedResults;
    182 		}
    183 	}
    184 	return null;
    185 }
    186 
    187 public int hashCode() {
    188 	return this.name.hashCode();
    189 }
    190 
    191 void printTab() {
    192 	if (this.parent != null) {
    193 		if (this.printStream != null) this.printStream.print("\t"); //$NON-NLS-1$
    194 		this.parent.printTab();
    195 	}
    196 }
    197 void print(String text) {
    198 	if (this.printStream != null) {
    199 		if (NEW_LINE) printTab();
    200 		this.printStream.print(text);
    201 		NEW_LINE = false;
    202 	}
    203 }
    204 
    205 void printGlobalTime(long start) {
    206 	printGlobalTime(start, null);
    207 }
    208 
    209 void printGlobalTime(long start, String end) {
    210 	long time = System.currentTimeMillis();
    211 	String resultsName = getName();
    212 	StringBuffer buffer;
    213 	if (resultsName == null) {
    214 		buffer = new StringBuffer(" => time spent was "); //$NON-NLS-1$
    215 	} else {
    216 		buffer = new StringBuffer(" => time spent in '"); //$NON-NLS-1$
    217 		buffer.append(resultsName);
    218 		buffer.append("' was "); //$NON-NLS-1$
    219 	}
    220 	buffer.append(Util.timeString(time-start));
    221 	if (end != null) {
    222 		buffer.append(". "); //$NON-NLS-1$
    223 		buffer.append(end.trim());
    224 	}
    225 	println(buffer);
    226 }
    227 
    228 void println() {
    229 	if (this.printStream != null) {
    230 		this.printStream.println();
    231 		NEW_LINE = true;
    232 	}
    233 }
    234 
    235 void println(String text) {
    236 	if (this.printStream != null) {
    237 		if (NEW_LINE) printTab();
    238 		this.printStream.println(text);
    239 		NEW_LINE = true;
    240 	}
    241 }
    242 
    243 void println(StringBuffer buffer) {
    244 	println(buffer.toString());
    245 }
    246 
    247 public int size() {
    248 	return this.children == null ? 0 : this.children.size();
    249 }
    250 
    251 public String toString() {
    252 	return getPath();
    253 }
    254 
    255 }
    256