KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > javancss > Util


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the reusable ccl java library
15  * (http://www.kclee.com/clemens/java/ccl/).
16  *
17  * The Initial Developer of the Original Code is
18  * Chr. Clemens Lee.
19  * Portions created by Chr. Clemens Lee are Copyright (C) 2002
20  * Chr. Clemens Lee. All Rights Reserved.
21  *
22  * Contributor(s): Chr. Clemens Lee <clemens@kclee.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */

37
38 package net.sourceforge.cobertura.javancss;
39
40 import java.util.Vector JavaDoc;
41
42 /**
43  * A general purpose class with a variety of support and convenience methods.
44  *
45  * <p> There are different groups of methods in this class:
46  * <br>
47  * <br><a HREF="#print(char)">print methods</a> - convenience methods for System.out.print etc. that additionally make sure output is gets flushed immediately.
48  * <br><a HREF="#isEmpty(java.lang.String)">string methods</a>
49  * <br><a HREF="#concat(java.util.Vector)">string/vector converter methods</a>
50  * <br><a HREF="#getConstantObject()">miscellaneous methods</a>
51  * <p>
52  *
53  * Some basic but none the less the most used methods by myself are:<br>
54  * - {@link #isEmpty(java.lang.String) isEmpty}<br>
55  * - {@link #stringToLines(java.lang.String) stringToLines}<br>
56  * - {@link #sleep(int) sleep}<br>
57  * <p>
58  *
59  * @author <a HREF="http://www.kclee.com/clemens/">
60  * Chr. Clemens Lee</a>
61  * &lt;<a HREF="mailto:clemens@kclee.com">
62  * clemens@kclee.com
63  * </a>>
64  */

65 public class Util
66 {
67
68     public static final Object JavaDoc CONSTANT_OBJECT = new Object JavaDoc();
69
70     /**
71      * This is an utility class, there is (should be) no need
72      * for an instance of this class.
73      */

74     private Util()
75     {
76         super();
77     }
78
79     // -----------------------------------------------------
80
// debug methods and assertion stuff
81
// -----------------------------------------------------
82

83     /**
84      * panicIf <=> not assert. Throws ApplicationException if true.
85      * It's not necessary to catch this exception.
86      */

87     static void panicIf(boolean bPanic_)
88     {
89         if (bPanic_)
90         {
91             throw (new RuntimeException JavaDoc());
92         }
93     }
94
95     /**
96      * panicIf <=> not assert. Throws ApplicationException if true.
97      * It's not necessary to catch this exception.
98      *
99      * @param sMessage_ The error message for the Exception.
100      */

101     static void panicIf(boolean bPanic_, String JavaDoc sMessage_)
102     {
103         if (bPanic_)
104         {
105             throw (new RuntimeException JavaDoc(sMessage_));
106         }
107     }
108
109     /**
110      * Tests, if a given String equals null or "".
111      */

112     public static boolean isEmpty(String JavaDoc sTest_)
113     {
114         if (sTest_ == null || sTest_.equals(""))
115         {
116             return true;
117         }
118
119         return false;
120     }
121
122     /**
123      * This function takes a String and separates it into different
124      * lines. The last line does not need to have a separator character.
125      *
126      * @param lines_ The number of lines that should be extracted.
127      * Zero if maximum number of lines is requested.
128      * @param cCutter_ The character that separates pString_ into
129      * different lines
130      *
131      * @return The single lines do not contain the cCutter_
132      * character at the end.
133      */

134     private static Vector JavaDoc stringToLines(int lines_, String JavaDoc pString_, char cCutter_)
135     {
136         int maxLines = Integer.MAX_VALUE;
137         if (lines_ > 0)
138         {
139             maxLines = lines_;
140         }
141
142         Vector JavaDoc vRetVal = new Vector JavaDoc();
143         if (pString_ == null)
144         {
145             return vRetVal;
146         }
147
148         int startIndex = 0;
149         for (; maxLines > 0; maxLines--)
150         {
151             int endIndex = pString_.indexOf(cCutter_, startIndex);
152             if (endIndex == -1)
153             {
154                 if (startIndex < pString_.length())
155                 {
156                     endIndex = pString_.length();
157                 }
158                 else
159                 {
160                     break;
161                 }
162             }
163             String JavaDoc sLine = pString_.substring(startIndex, endIndex);
164             vRetVal.addElement(sLine);
165             startIndex = endIndex + 1;
166         }
167
168         return vRetVal;
169     }
170
171     /**
172      * This function takes a String and separates it into different
173      * lines. The last line does not need to have a separator character.
174      *
175      * @param cCutter_ The character that separates pString_ into
176      * different lines
177      *
178      * @return The single lines do not contain the cCutter_ character
179      * at the end.
180      */

181     private static Vector JavaDoc stringToLines(String JavaDoc pString_, char cCutter_)
182     {
183         return stringToLines(0, pString_, cCutter_);
184     }
185
186     /**
187      * This function takes a String and separates it into different
188      * lines. The last line does not need to have a '\n'. The function
189      * can't handle dos carriage returns.
190      *
191      * @return The single lines do not contain the '\n' character
192      * at the end.
193      */

194     public static Vector JavaDoc stringToLines(String JavaDoc pString_)
195     {
196         return stringToLines(pString_, '\n');
197     }
198
199     /**
200      * Current thread sleeps in seconds.
201      */

202     private static void sleep(int seconds_)
203     {
204         try
205         {
206             Thread.sleep(seconds_ * 1000);
207         }
208         catch (Exception JavaDoc pException)
209         {
210         }
211     }
212
213 }
214
Popular Tags