| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Utils |
|
| 3.3333333333333335;3.333 |
| 1 | package us.daveread.utility.formatcheck.gui; | |
| 2 | ||
| 3 | import java.util.Date; | |
| 4 | import java.text.NumberFormat; | |
| 5 | import java.text.DecimalFormat; | |
| 6 | ||
| 7 | /** | |
| 8 | * <p>Title: Utils | |
| 9 | * <p>Description: Utility methods | |
| 10 | * <p>Copyright: Copyright (c) 2005 | |
| 11 | * <p>This program is free software; you can redistribute it and/or modify | |
| 12 | * it under the terms of the GNU General Public License as published by | |
| 13 | * the Free Software Foundation; either version 2 of the License, or | |
| 14 | * (at your option) any later version. | |
| 15 | * <p>This program is distributed in the hope that it will be useful, | |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | * GNU General Public License for more details. | |
| 19 | * <p>You should have received a copy of the GNU General Public License | |
| 20 | * along with this program; if not, write to the Free Software | |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | * </p> | |
| 23 | * | |
| 24 | * @author David Read | |
| 25 | * @version $Id: Utils.java,v 1.1.1.1 2006/05/22 02:14:09 daveread Exp $ | |
| 26 | */ | |
| 27 | ||
| 28 | public class Utils { | |
| 29 | ||
| 30 | /** Force two-digit output -- used by timeElapsedFormat */ | |
| 31 | 0 | static NumberFormat twoDigitFormat = new DecimalFormat("00"); |
| 32 | ||
| 33 | /** | |
| 34 | * No one should instanciate this class | |
| 35 | */ | |
| 36 | 0 | private Utils() { |
| 37 | 0 | } |
| 38 | ||
| 39 | /** | |
| 40 | * Ensure that a given character is inserted at intervals along a String. | |
| 41 | * This is typically used to force including a space or CR periodically. | |
| 42 | * The method will walk through the supplied data string, looking for characters | |
| 43 | * that are in the break characters String. If found, and the minimum number | |
| 44 | * of characters have been traversed, the insert string is inserted and the | |
| 45 | * character count reset. If the maximum section length is reached and no | |
| 46 | * break characters have been found, the insert string is inserted anyway. | |
| 47 | * | |
| 48 | * @param saData String The string to have characters inserted into | |
| 49 | * @param saInsert String The string to insert into the data string | |
| 50 | * @param iaMinSectionLength int The mimumum number of characters in the data | |
| 51 | * string, that must occur between inserted strings | |
| 52 | * @param iaMaxSectionLength int The maximum number of characters to allow | |
| 53 | * in the data string between inserted strings | |
| 54 | * @param saBreakCharacters String The characters that signal a possible | |
| 55 | * insertion point in the data string | |
| 56 | * | |
| 57 | * @return String The updated data string. If the data string is null, a null | |
| 58 | * String is returned. | |
| 59 | */ | |
| 60 | public final static String characterInsert(String saData, String saInsert, | |
| 61 | int iaMinSectionLength, | |
| 62 | int iaMaxSectionLength, | |
| 63 | String saBreakCharacters) { | |
| 64 | StringBuffer sblResult; | |
| 65 | char clData[]; | |
| 66 | int ilLen, ilPosit, ilSectionLength; | |
| 67 | ||
| 68 | 0 | if (saData != null) { |
| 69 | 0 | ilLen = saData.length(); |
| 70 | 0 | clData = new char[ilLen]; |
| 71 | 0 | saData.getChars(0, ilLen, clData, 0); |
| 72 | 0 | ilPosit = 0; |
| 73 | 0 | ilSectionLength = 0; |
| 74 | 0 | sblResult = new StringBuffer(ilLen * 2); |
| 75 | ||
| 76 | 0 | for (ilPosit = 0; ilPosit < ilLen; ++ilPosit) { |
| 77 | 0 | sblResult.append(clData[ilPosit]); |
| 78 | 0 | ++ilSectionLength; |
| 79 | 0 | if (saInsert.length() >= 1 && clData[ilPosit] == saInsert.charAt(0)) { |
| 80 | 0 | ilSectionLength = 0; |
| 81 | } | |
| 82 | 0 | else if ( (ilSectionLength >= iaMinSectionLength && |
| 83 | saBreakCharacters.indexOf(clData[ilPosit]) >= 0) || | |
| 84 | ilSectionLength >= iaMaxSectionLength) { | |
| 85 | 0 | sblResult.append(saInsert); |
| 86 | 0 | ilSectionLength = 0; |
| 87 | } | |
| 88 | } | |
| 89 | 0 | saData = sblResult.toString(); |
| 90 | } | |
| 91 | ||
| 92 | 0 | return saData; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Determine the number of elapsed seconds and convert to H:M:S display. | |
| 97 | * | |
| 98 | * @param aStart Date The starting time | |
| 99 | * @param aStop Date The ending time | |
| 100 | * | |
| 101 | * @return String The formatted elapsed time between start and stop | |
| 102 | */ | |
| 103 | public final static String timeElapsedFormat(Date aStart, Date aStop) { | |
| 104 | String result; | |
| 105 | long seconds; | |
| 106 | ||
| 107 | 0 | if (aStart == null || aStop == null) { |
| 108 | 0 | result = "Unknown"; |
| 109 | 0 | } else if (aStart.compareTo(aStop) > 0) { |
| 110 | 0 | result = "Starts After End"; |
| 111 | } else { | |
| 112 | 0 | seconds = (aStop.getTime() - aStart.getTime()) / 1000; |
| 113 | 0 | result = ""; |
| 114 | 0 | if (seconds >= 3600) { |
| 115 | 0 | result += twoDigitFormat.format(seconds / 3600) + ":"; |
| 116 | 0 | seconds %= 3600; |
| 117 | } | |
| 118 | 0 | result += twoDigitFormat.format(seconds / 60) + ":"; |
| 119 | 0 | seconds %= 60; |
| 120 | 0 | result += twoDigitFormat.format(seconds); |
| 121 | } | |
| 122 | ||
| 123 | 0 | return result; |
| 124 | } | |
| 125 | ||
| 126 | } |