1 | package org.expeditee.stats;
|
---|
2 |
|
---|
3 | import java.sql.Time;
|
---|
4 | import java.util.Date;
|
---|
5 |
|
---|
6 | import org.expeditee.io.Logger;
|
---|
7 |
|
---|
8 | public abstract class Stats {
|
---|
9 | protected static Time _DarkTime = new Time(0);
|
---|
10 | protected static Date _StartTime = new Date();
|
---|
11 | protected static Date _FrameAccessTime = new Date();
|
---|
12 | protected static Date _FrameAccessDarkTime = new Date();
|
---|
13 |
|
---|
14 | protected static final long MILLISECONDS_PER_MINUTE = 60000;
|
---|
15 |
|
---|
16 | public static final char ROW_SEPARATOR_CHAR = '\u2500';
|
---|
17 |
|
---|
18 | public static final char COLUMN_SEPARATOR_CHAR = '\u2502';
|
---|
19 |
|
---|
20 | public static final char ROW_COLUMN_SEPARATOR_CHAR = '\u253C';
|
---|
21 |
|
---|
22 | public static final char BOTTOM_COLUMN_SEPARATOR_CHAR = '\u2534';
|
---|
23 |
|
---|
24 | public static final char TOP_COLUMN_SEPARATOR_CHAR = '\u252C';
|
---|
25 |
|
---|
26 | public static final String COLUMN_SEPARATOR = " " + COLUMN_SEPARATOR_CHAR
|
---|
27 | + ' ';
|
---|
28 |
|
---|
29 | public static final String ROW_COLUMN_SEPARATOR = "" + ROW_SEPARATOR_CHAR + ROW_COLUMN_SEPARATOR_CHAR + ROW_SEPARATOR_CHAR;
|
---|
30 |
|
---|
31 | public static final String DARK_TIME_ATTRIBUTE = "DarkTime:";
|
---|
32 |
|
---|
33 | public static final String ACTIVE_TIME_ATTRIBUTE = "ActiveTime:";
|
---|
34 |
|
---|
35 | protected static final int DEFAULT_RATE_WIDTH = 4;
|
---|
36 |
|
---|
37 | protected static final int DEFAULT_VALUE_WIDTH = 3;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Appends a single stats to a string buffer containing a collection of
|
---|
41 | * stats.
|
---|
42 | *
|
---|
43 | * @param stats
|
---|
44 | * The string buffer to append the stat onto
|
---|
45 | * @param name
|
---|
46 | * The name of the stat
|
---|
47 | * @param value
|
---|
48 | * The new value for the stat
|
---|
49 | */
|
---|
50 | protected static void appendStat(StringBuffer stats, String name, int value,
|
---|
51 | boolean newline, boolean ignoreZero, int minValueWidth,
|
---|
52 | int minRateWidth) {
|
---|
53 | // prevent divide by zero errors
|
---|
54 | if (ignoreZero && value <= 0)
|
---|
55 | return;
|
---|
56 |
|
---|
57 | String perHour = getRate(value);
|
---|
58 | while (perHour.length() < minRateWidth)
|
---|
59 | perHour = " " + perHour;
|
---|
60 | String valueString = "" + value;
|
---|
61 | while (valueString.length() < minValueWidth)
|
---|
62 | valueString = " " + valueString;
|
---|
63 | stats.append(valueString).append(" @ ").append(perHour.toString())
|
---|
64 | .append("/hour ").append(name);
|
---|
65 | if (newline)
|
---|
66 | stats.append("\n");
|
---|
67 | else
|
---|
68 | stats.append(" ");
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected static StringBuffer getCompactStat(String name, int value,
|
---|
73 | int minValueWidth, int minTotalWidth) {
|
---|
74 | StringBuffer stats = new StringBuffer();
|
---|
75 | // prevent divide by zero errors
|
---|
76 | if (value > 0) {
|
---|
77 | String perHour = getRate(value);
|
---|
78 | String valueString = "" + value;
|
---|
79 | while (valueString.length() < minValueWidth)
|
---|
80 | valueString = " " + valueString;
|
---|
81 | stats.append(valueString).append("@").append(perHour.toString())
|
---|
82 | .append("/h");
|
---|
83 | }
|
---|
84 | while (stats.length() < minTotalWidth) {
|
---|
85 | stats.append(' ');
|
---|
86 | }
|
---|
87 | return stats.append(COLUMN_SEPARATOR);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected static String getRate(int value) {
|
---|
91 | return "" + Math.round(value * 60 / getMinutesUsed());
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected static void appendStat(StringBuffer stats, String name, int value) {
|
---|
95 | appendStat(stats, name, value, true, false, DEFAULT_VALUE_WIDTH,
|
---|
96 | DEFAULT_RATE_WIDTH);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected static double getMinutesUsed() {
|
---|
100 | long elapsedTime = new Date().getTime() - _StartTime.getTime()
|
---|
101 | - _DarkTime.getTime();
|
---|
102 |
|
---|
103 | return (double) elapsedTime / MILLISECONDS_PER_MINUTE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static StringBuffer getDate() {
|
---|
107 | StringBuffer stats = new StringBuffer("Date: ");
|
---|
108 | stats.append(Logger.EasyDateFormat("ddMMMyyyy:HHmm")).append("\n");
|
---|
109 | return stats;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static StringBuffer getBufferedString(String string, int width) {
|
---|
113 | return getBufferedString(string, width, ' ');
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static StringBuffer getBufferedString(int width, char bufferChar) {
|
---|
117 | return getBufferedString("", width, bufferChar);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static StringBuffer getBufferedString(int width) {
|
---|
121 | return getBufferedString("", width);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static StringBuffer getBufferedString(String start, int width,
|
---|
125 | char bufferChar) {
|
---|
126 | StringBuffer sb = new StringBuffer(start);
|
---|
127 | while (sb.length() < width) {
|
---|
128 | sb.append(bufferChar);
|
---|
129 | }
|
---|
130 | return sb;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public static Time getFrameDarkTime() {
|
---|
134 | return new Time(_DarkTime.getTime() - _FrameAccessDarkTime.getTime());
|
---|
135 | }
|
---|
136 |
|
---|
137 | public static Time getFrameActiveTime() {
|
---|
138 | return new Time(getFrameTotalTime().getTime()
|
---|
139 | - getFrameDarkTime().getTime());
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static Time getFrameTotalTime() {
|
---|
143 | return new Time((new Date()).getTime() - _FrameAccessTime.getTime());
|
---|
144 | }
|
---|
145 | }
|
---|