alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.test.performance.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.test.internal.performance.PerformanceTestPlugin;
import org.eclipse.test.internal.performance.db.Variations;


public class Utils {

	public final static double STANDARD_ERROR_THRESHOLD = 0.03; // 3%
	static final NumberFormat PERCENT_FORMAT = NumberFormat.getPercentInstance();
	static {
		PERCENT_FORMAT.setMaximumFractionDigits(1);
	}
	static final DecimalFormat DEVIATION_FORMAT = (DecimalFormat) NumberFormat.getPercentInstance();
	static {
		DEVIATION_FORMAT.setMaximumFractionDigits(1);
		DEVIATION_FORMAT.setMinimumFractionDigits(1);
		DEVIATION_FORMAT.setPositivePrefix("+");
		DEVIATION_FORMAT.setNegativePrefix("- ");
	}
	static final DecimalFormat STDERR_FORMAT = (DecimalFormat) NumberFormat.getNumberInstance();
	static {
		STDERR_FORMAT.setMaximumFractionDigits(1);
		STDERR_FORMAT.setMinimumFractionDigits(1);
		STDERR_FORMAT.setMultiplier(100);
	}
	public final static String STANDARD_ERROR_THRESHOLD_STRING = PERCENT_FORMAT.format(STANDARD_ERROR_THRESHOLD);
	public final static String UNKNOWN_IMAGE="Unknown.gif";
	public final static String OK_IMAGE="OK.gif";
	public final static String OK_IMAGE_WARN="OK_caution.gif";
	public final static String FAIL_IMAGE="FAIL.gif";
	public final static String FAIL_IMAGE_WARN="FAIL_caution.gif";
	public final static String FAIL_IMAGE_EXPLAINED="FAIL_greyed.gif";
	public final static int OK = 0;
	public final static int NAN = 0x1;
	public final static int ERR = 0x2;

	/**
	 * Return <html><head><meta http-equiv="Content-Type"
	 *         content="text/html; charset=iso-8859-1">
	 */
	public static String HTML_OPEN = "<html>";

	/**
	 * Return "</html>".
	 */
	public static String HTML_CLOSE = "</html>";

	/**
	 * Default style-sheet used on eclipse.org
	 */
	public static String HTML_DEFAULT_CSS = "<style type=\"text/css\">" + "p, table, td, th {  font-family: arial, helvetica, geneva; font-size: 10pt}\n"
			+ "pre {  font-family: \"Courier New\", Courier, mono; font-size: 10pt}\n" + "h2 { font-family: arial, helvetica, geneva; font-size: 18pt; font-weight: bold ; line-height: 14px}\n"
			+ "code {  font-family: \"Courier New\", Courier, mono; font-size: 10pt}\n" + "sup {  font-family: arial,helvetica,geneva; font-size: 10px}\n"
			+ "h3 {  font-family: arial, helvetica, geneva; font-size: 14pt; font-weight: bold}\n" + "li {  font-family: arial, helvetica, geneva; font-size: 10pt}\n"
			+ "h1 {  font-family: arial, helvetica, geneva; font-size: 28px; font-weight: bold}\n"
			+ "body {  font-family: arial, helvetica, geneva; font-size: 10pt; clip:   rect(   ); margin-top: 5mm; margin-left: 3mm}\n"
			+ ".indextop { font-size: x-large;; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold}\n"
			+ ".indexsub { font-size: xx-small;; font-family: Arial, Helvetica, sans-serif; color: #8080FF}\n" + "</style>";

	/**
	 * Creates a Variations object using build id pattern, config and jvm.
	 *
	 * @param buildIdPattern
	 * @param config
	 * @param jvm
	 */
	public static Variations getVariations(String buildIdPattern, String config, String jvm) {
		String buildIdPatterns = buildIdPattern.replace(',', '%');
		Variations variations = new Variations();
		variations.put(PerformanceTestPlugin.CONFIG, config);
		variations.put(PerformanceTestPlugin.BUILD, buildIdPatterns);
		variations.put("jvm", jvm);
		return variations;
	}

	/**
	 * Utility method to copy a file.
	 *
	 * @param src the source file.
	 * @param dest the destination.
	 */
	private static void copyFile(File src, File dest) {

		try {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void copyImages(File images, File output) {
		copyFile(new File(images, FAIL_IMAGE), new File(output, FAIL_IMAGE));
		copyFile(new File(images, FAIL_IMAGE_EXPLAINED), new File(output, FAIL_IMAGE_EXPLAINED));
		copyFile(new File(images, FAIL_IMAGE_WARN), new File(output, FAIL_IMAGE_WARN));
		copyFile(new File(images, OK_IMAGE), new File(output, OK_IMAGE));
		copyFile(new File(images, OK_IMAGE_WARN), new File(output, OK_IMAGE_WARN));
		copyFile(new File(images, UNKNOWN_IMAGE), new File(output, UNKNOWN_IMAGE));
	}
	public static void copyScripts(File scripts, File output) {
		copyFile(new File(scripts, "ToolTip.css"), new File(output, "ToolTip.css"));
		copyFile(new File(scripts, "ToolTip.js"), new File(output, "ToolTip.js"));
	}

	/**
	 * Downsample Image to 8 bit depth format so that the resulting image data
	 * can be saved to GIF. Note. If the source image contains photo quality
	 * content with more than 256 colours, resulting data will look very poor.
	 */
	static int closest(RGB[] rgbs, int n, RGB rgb) {
		int minDist = 256 * 256 * 3;
		int minIndex = 0;
		for (int i = 0; i < n; ++i) {
			RGB rgb2 = rgbs[i];
			int da = rgb2.red - rgb.red;
			int dg = rgb2.green - rgb.green;
			int db = rgb2.blue - rgb.blue;
			int dist = da * da + dg * dg + db * db;
			if (dist < minDist) {
				minDist = dist;
				minIndex = i;
			}
		}
		return minIndex;
	}

	static class ColorCounter implements Comparable {
		RGB rgb;

		int count;

		public int compareTo(Object o) {
			return ((ColorCounter) o).count - count;
		}
	}

	public static ImageData downSample(Image image) {
		ImageData data = image.getImageData();
		if (!data.palette.isDirect && data.depth <= 8)
			return data;

		// compute a histogram of color frequencies
		HashMap freq = new HashMap();
		int width = data.width;
		int[] pixels = new int[width];
		int[] maskPixels = new int[width];
		for (int y = 0, height = data.height; y < height; ++y) {
			data.getPixels(0, y, width, pixels, 0);
			for (int x = 0; x < width; ++x) {
				RGB rgb = data.palette.getRGB(pixels[x]);
				ColorCounter counter = (ColorCounter) freq.get(rgb);
				if (counter == null) {
					counter = new ColorCounter();
					counter.rgb = rgb;
					freq.put(rgb, counter);
				}
				counter.count++;
			}
		}

		// sort colors by most frequently used
		ColorCounter[] counters = new ColorCounter[freq.size()];
		freq.values().toArray(counters);
		Arrays.sort(counters);

		// pick the most frequently used 256 (or fewer), and make a palette
		ImageData mask = null;
		if (data.transparentPixel != -1 || data.maskData != null) {
			mask = data.getTransparencyMask();
		}
		int n = Math.min(256, freq.size());
		RGB[] rgbs = new RGB[n + (mask != null ? 1 : 0)];
		for (int i = 0; i < n; ++i)
			rgbs[i] = counters[i].rgb;
		if (mask != null) {
			rgbs[rgbs.length - 1] = data.transparentPixel != -1 ? data.palette.getRGB(data.transparentPixel) : new RGB(255, 255, 255);
		}
		PaletteData palette = new PaletteData(rgbs);

		// create a new image using the new palette:
		// for each pixel in the old image, look up the best matching
		// index in the new palette
		ImageData newData = new ImageData(width, data.height, 8, palette);
		if (mask != null)
			newData.transparentPixel = rgbs.length - 1;
		for (int y = 0, height = data.height; y < height; ++y) {
			data.getPixels(0, y, width, pixels, 0);
			if (mask != null)
				mask.getPixels(0, y, width, maskPixels, 0);
			for (int x = 0; x < width; ++x) {
				if (mask != null && maskPixels[x] == 0) {
					pixels[x] = rgbs.length - 1;
				} else {
					RGB rgb = data.palette.getRGB(pixels[x]);
					pixels[x] = closest(rgbs, n, rgb);
				}
			}
			newData.setPixels(0, y, width, pixels, 0);
		}
		return newData;
	}

	/**
	 * Returns the date/time from the build id in format yyyymmddhm
	 *
	 * @param buildId
	 * @return date/time in format YYYYMMDDHHMM, ie. 200504060010
	 */
	public static long getDateFromBuildID(String buildId) {
		return getDateFromBuildID(buildId, false);
	}

	public static long getDateFromBuildID(String buildId, boolean matchLast) {
		Calendar calendar = Calendar.getInstance();

		if (buildId.indexOf('_') != -1) {
			String[] buildIdParts = buildId.split("_");

			int buildIdSegment = 1;
			if (matchLast)
				buildIdSegment = buildIdParts.length - 1;
			// if release build, expect <release>_
			// use test date and time for plotting
			int year = Integer.parseInt(buildIdParts[buildIdSegment].substring(0, 4));
			int month = Integer.parseInt(buildIdParts[buildIdSegment].substring(4, 6)) - 1;
			int date = Integer.parseInt(buildIdParts[buildIdSegment].substring(6, 8));
			int hours = Integer.parseInt(buildIdParts[buildIdSegment].substring(8, 10));
			int min = Integer.parseInt(buildIdParts[buildIdSegment].substring(10, 12));

			calendar.set(year, month, date, hours, min);
			return calendar.getTimeInMillis();

		} else if (buildId.indexOf('-') != -1) {
			// if regular build, expect <buildType>-
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.