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;
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 public class Util
066 {
067
068 public static final Object CONSTANT_OBJECT = new Object();
069
070 /**
071 * This is an utility class, there is (should be) no need
072 * for an instance of this class.
073 */
074 private Util()
075 {
076 super();
077 }
078
079 // -----------------------------------------------------
080 // debug methods and assertion stuff
081 // -----------------------------------------------------
082
083 /**
084 * panicIf <=> not assert. Throws ApplicationException if true.
085 * It's not necessary to catch this exception.
086 */
087 static void panicIf(boolean bPanic_)
088 {
089 if (bPanic_)
090 {
091 throw (new RuntimeException());
092 }
093 }
094
095 /**
096 * panicIf <=> not assert. Throws ApplicationException if true.
097 * It's not necessary to catch this exception.
098 *
099 * @param sMessage_ The error message for the Exception.
100 */
101 static void panicIf(boolean bPanic_, String sMessage_)
102 {
103 if (bPanic_)
104 {
105 throw (new RuntimeException(sMessage_));
106 }
107 }
108
109 /**
110 * Tests, if a given String equals null or "".
111 */
112 public static boolean isEmpty(String 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 stringToLines(int lines_, String pString_, char cCutter_)
135 {
136 int maxLines = Integer.MAX_VALUE;
137 if (lines_ > 0)
138 {
139 maxLines = lines_;
140 }
141
142 Vector vRetVal = new Vector();
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 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 stringToLines(String 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 stringToLines(String 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 pException)
209 {
210 }
211 }
212
213 }