View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.util;
22  
23  import java.text.DecimalFormat;
24  import java.text.NumberFormat;
25  
26  import com.ebay.carad.os.vitalsigns.DataPoint;
27  
28  /***
29   * Home for miscellaneous reporting and DB utilities and constants.
30   * 
31   * @author Jeremy Kraybill
32   * @author Jeremy Thomerson
33   * @version $Id$
34   */
35  public abstract class ReportUtil {
36  
37  	/*** Number formatter. */
38  	private static final NumberFormat NUMBER_FORMATTER = new DecimalFormat("0.0");
39  	
40  	/***
41  	 * Attempts to convert an object into a float. If it is not
42  	 * convertible, it returns 0.0.
43  	 * 
44  	 * @param data the object to convert
45  	 * @return the float it converts to, or 0 if not convertible
46  	 */
47  	public static float getFloatFrom(Object data) {
48      	float curValue = 0.0f;
49      	if (data != null) {
50      		try {
51      			curValue = Float.parseFloat(data.toString());
52      		} catch (NumberFormatException e) {
53      		    // @PMD:REVIEWED:EmptyCatchBlock: by jthomerson on 7/09/06 1:39 AM
54      			// not convertible.
55      		}
56      	}
57      	return curValue;
58  	}
59  
60  	/***
61  	 * Taks a logtime / data row and returns a new map, with logtime shifted by the given amount of milliseconds. 
62  	 * Used for moving dataseries to the right.
63  	 * 
64  	 * @param row the row to transpose
65  	 * @param ms the number of milliseconds to move to the right.
66  	 * @return a new Map
67  	 */
68  	public static DataPoint shiftTime(DataPoint row, long ms) {
69          DataPoint newRow = new DataPoint();
70      	long cur = row.getLogTime();
71  		newRow.setData(row.getData());
72  		newRow.setLogTime(cur + ms);
73  		return newRow;
74  	}
75  	
76  	/***
77  	 * Takes a list of float deltas and writes an HTML string of the form "NN/NN/NN/..NN" where NN is expressed as a +/- percentage value.
78  	 *  
79  	 * @param delta a list of float % deltas (where 1.0 is +100%, -1.0 is -100% etc)
80  	 * @param moreIsBetter if true, negative values are red and positive are green; otherwise vice versa.
81  	 * @return a formatted HTML string
82  	 */
83  	public static String getDeltasAsHtml(float[] delta, boolean moreIsBetter) {
84  		StringBuffer ret = new StringBuffer();
85  		for (int delt = 0; delt < delta.length; delt++) {
86  			String deltString = ((delta[delt] == -1.0f) || (delta[delt] == 0.0f)) ? "--" : ((delta[delt] > 0 ? "+" : "") + NUMBER_FORMATTER.format(delta[delt] * 100) + "%");
87              String deltaClass = (delta[delt] >= 0) == (moreIsBetter) ? "goodDelta" : "badDelta"; 
88              deltString = "<span class=\"" + deltaClass + "\">" + deltString + "</span>";
89  			ret.append(deltString);
90  			ret.append((delt < 2) ? "/" : "");
91  		}
92  		return ret.toString();
93  	}
94  }