001 /* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is the reusable ccl java library
015 * (http://www.kclee.com/clemens/java/ccl/).
016 *
017 * The Initial Developer of the Original Code is
018 * Chr. Clemens Lee.
019 * Portions created by Chr. Clemens Lee are Copyright (C) 2002
020 * Chr. Clemens Lee. All Rights Reserved.
021 *
022 * Contributor(s): Chr. Clemens Lee <clemens@kclee.com>
023 *
024 * Alternatively, the contents of this file may be used under the terms of
025 * either the GNU General Public License Version 2 or later (the "GPL"), or
026 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
027 * in which case the provisions of the GPL or the LGPL are applicable instead
028 * of those above. If you wish to allow use of your version of this file only
029 * under the terms of either the GPL or the LGPL, and not to allow others to
030 * use your version of this file under the terms of the MPL, indicate your
031 * decision by deleting the provisions above and replace them with the notice
032 * and other provisions required by the GPL or the LGPL. If you do not delete
033 * the provisions above, a recipient may use your version of this file under
034 * the terms of any one of the MPL, the GPL or the LGPL.
035 *
036 * ***** END LICENSE BLOCK ***** */
037
038 package net.sourceforge.cobertura.javancss.ccl;
039
040 import java.util.Vector;
041
042 /**
043 * A general purpose class with a variety of support and convenience methods.
044 *
045 * <p> There are different groups of methods in this class:
046 * <br>
047 * <br><a href="#print(char)">print methods</a> - convenience methods for System.out.print etc. that additionally make sure output is gets flushed immediately.
048 * <br><a href="#isEmpty(java.lang.String)">string methods</a>
049 * <br><a href="#concat(java.util.Vector)">string/vector converter methods</a>
050 * <br><a href="#getConstantObject()">miscellaneous methods</a>
051 * <p>
052 *
053 * Some basic but none the less the most used methods by myself are:<br>
054 * - {@link #isEmpty(java.lang.String) isEmpty}<br>
055 * - {@link #stringToLines(java.lang.String) stringToLines}<br>
056 * - {@link #sleep(int) sleep}<br>
057 * <p>
058 *
059 * @author <a href="http://www.kclee.com/clemens/">
060 * Chr. Clemens Lee</a>
061 * <<a href="mailto:clemens@kclee.com">
062 * clemens@kclee.com
063 * </a>>
064 */
065
066
067
068 /*
069 * cobertura - this file was moved from net.sourceforge.cobertura.javancss package.
070 *
071 * Mark Doliner apparently got the source from somewhere, but it is not available now.
072 */
073
074 public class Util
075 {
076
077 public static final Object CONSTANT_OBJECT = new Object();
078
079 /**
080 * This is an utility class, there is (should be) no need
081 * for an instance of this class.
082 */
083 private Util()
084 {
085 super();
086 }
087
088 // -----------------------------------------------------
089 // debug methods and assertion stuff
090 // -----------------------------------------------------
091 /**
092 * panicIf <=> not assert. Throws ApplicationException if true.
093 * It's not necessary to catch this exception.
094 */
095 public static void panicIf(boolean bPanic_)
096 {
097 if (bPanic_)
098 {
099 throw (new RuntimeException());
100 }
101 }
102
103 /**
104 * panicIf <=> not assert. Throws ApplicationException if true.
105 * It's not necessary to catch this exception.
106 *
107 * @param sMessage_ The error message for the Exception.
108 */
109 public static void panicIf(boolean bPanic_, String sMessage_)
110 {
111 if (bPanic_)
112 {
113 throw (new RuntimeException(sMessage_));
114 }
115 }
116
117 /**
118 * Tests, if a given String equals null or "".
119 */
120 public static boolean isEmpty(String sTest_)
121 {
122 if (sTest_ == null || sTest_.equals(""))
123 {
124 return true;
125 }
126
127 return false;
128 }
129
130 /**
131 * This function takes a String and separates it into different
132 * lines. The last line does not need to have a separator character.
133 *
134 * @param lines_ The number of lines that should be extracted.
135 * Zero if maximum number of lines is requested.
136 * @param cCutter_ The character that separates pString_ into
137 * different lines
138 *
139 * @return The single lines do not contain the cCutter_
140 * character at the end.
141 */
142 private static Vector stringToLines(int lines_, String pString_, char cCutter_)
143 {
144 int maxLines = Integer.MAX_VALUE;
145 if (lines_ > 0)
146 {
147 maxLines = lines_;
148 }
149
150 Vector vRetVal = new Vector();
151 if (pString_ == null)
152 {
153 return vRetVal;
154 }
155
156 int startIndex = 0;
157 for (; maxLines > 0; maxLines--)
158 {
159 int endIndex = pString_.indexOf(cCutter_, startIndex);
160 if (endIndex == -1)
161 {
162 if (startIndex < pString_.length())
163 {
164 endIndex = pString_.length();
165 }
166 else
167 {
168 break;
169 }
170 }
171 String sLine = pString_.substring(startIndex, endIndex);
172 vRetVal.addElement(sLine);
173 startIndex = endIndex + 1;
174 }
175
176 return vRetVal;
177 }
178
179 /**
180 * This function takes a String and separates it into different
181 * lines. The last line does not need to have a separator character.
182 *
183 * @param cCutter_ The character that separates pString_ into
184 * different lines
185 *
186 * @return The single lines do not contain the cCutter_ character
187 * at the end.
188 */
189 public static Vector stringToLines(String pString_, char cCutter_)
190 {
191 return stringToLines(0, pString_, cCutter_);
192 }
193
194 /**
195 * This function takes a String and separates it into different
196 * lines. The last line does not need to have a '\n'. The function
197 * can't handle dos carriage returns.
198 *
199 * @return The single lines do not contain the '\n' character
200 * at the end.
201 */
202 public static Vector stringToLines(String pString_)
203 {
204 return stringToLines(pString_, '\n');
205 }
206
207 /**
208 * Current thread sleeps in seconds.
209 */
210 private static void sleep(int seconds_)
211 {
212 try
213 {
214 Thread.sleep(seconds_ * 1000);
215 }
216 catch (Exception pException)
217 {
218 }
219 }
220
221 public static Vector concat(Vector vFirst_, Vector vSecond_)
222 {
223 //cobertura Mark Doliner appears to have simplified this.
224 vFirst_.addAll(vSecond_);
225 return vFirst_;
226 }
227
228
229 public static Vector<Object> sort(Vector<Object> input)
230 {
231 //cobertura - apparently Mark Doliner didn't think we need to sort.
232 return input;
233 }
234
235 // cobertura - gotten from decompile of ccl.jar.
236 public static void debug(Object oMessage_)
237 {
238 if(_bDebug)
239 printlnErr(oMessage_.toString());
240 }
241
242
243 public static Object getConstantObject()
244 {
245 return CONSTANT_OBJECT;
246 }
247
248 // cobertura - gotten from decompile of ccl.jar.
249 public static void setDebug(boolean bDebug_)
250 {
251 _bDebug = bDebug_;
252 }
253
254 // cobertura = gotten from decompile of ccl.jar.
255 public static boolean isDebug()
256 {
257 return _bDebug;
258 }
259
260 // cobertura = gotten from decompile of ccl.jar.
261 public static void println(String pString_)
262 {
263 System.out.println(pString_);
264 System.out.flush();
265 }
266
267 // cobertura = gotten from decompile of ccl.jar.
268 public static void printErr(char c_)
269 {
270 System.err.print(c_);
271 System.err.flush();
272 }
273
274 // cobertura = gotten from decompile of ccl.jar.
275 public static void printErr(String pString_)
276 {
277 System.err.print(pString_);
278 System.err.flush();
279 }
280
281 // cobertura = gotten from decompile of ccl.jar.
282 public static void printlnErr()
283 {
284 printErr('\n');
285 }
286
287 // cobertura = gotten from decompile of ccl.jar.
288 public static void printlnErr(String sMessage_)
289 {
290 printErr(sMessage_);
291 printlnErr();
292 }
293
294 // cobertura = gotten from decompile of ccl.jar.
295 private static boolean _bDebug = false;
296
297 }