001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/util/Util.java,v $
056: * $Revision: 1.3 $
057: * $Author: dglasser $
058: * $Date: 2003/05/29 00:42:37 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.util;
063:
064: import java.util.*;
065:
066: /**
067: * This class provides miscellaneous trivial utility methods.
068: */
069: public class Util {
070:
071: /**
072: * Returns true if the given object is null, or if it's toString() method
073: * returns all whitespace.
074: */
075: public static boolean isNothing(Object object) {
076: return object == null || object.toString().trim().length() == 0;
077: }
078:
079: /**
080: * Trims a String without the need to null-check first. If s is null,
081: * null is returned, otherwise s.trim() is returned.
082: */
083: public static String trim(String s) {
084: if (s == null)
085: return null;
086: return s.trim();
087: }
088:
089: /**
090: * If s is null or all whitespace, null is returned, otherwise
091: * s.trim() is returned.
092: */
093: public static String trimToNull(String s) {
094: if (s == null || (s = s.trim()).length() == 0)
095: return null;
096: return s;
097: }
098:
099: /**
100: * If s is null or all whitespace, a zero-length String ("") is returned,
101: * otherwise s.trim() is returned.
102: */
103: public static String trimToString(String s) {
104: if (s == null || (s = s.trim()).length() == 0)
105: return "";
106: return s;
107: }
108:
109: private static Class[][] primitiveToWrapperClassMappings = {
110: { boolean.class, java.lang.Boolean.class },
111: { int.class, java.lang.Integer.class },
112: { float.class, java.lang.Float.class },
113: { double.class, java.lang.Double.class },
114: { long.class, java.lang.Long.class },
115: { char.class, java.lang.Character.class },
116: { short.class, java.lang.Short.class },
117: { byte.class, java.lang.Byte.class },
118: { void.class, java.lang.Void.class }
119:
120: };
121:
122: private static HashMap primitiveToWrapperClassMap = new HashMap();
123:
124: static {
125:
126: for (int j = 0; j < primitiveToWrapperClassMappings.length; j++) {
127: primitiveToWrapperClassMap.put(
128: primitiveToWrapperClassMappings[j][0],
129: primitiveToWrapperClassMappings[j][1]);
130: }
131:
132: }
133:
134: /**
135: * Given the Class for a primitive type, such as int.class or byte.class,
136: * this method will return the corresponding wrapper class, such
137: * as Integer.class or Byte.class.
138: */
139: public static Class getWrapperClass(Class primitiveClass) {
140: return (Class) primitiveToWrapperClassMap.get(primitiveClass);
141: }
142:
143: /**
144: * This determines if the currently executing Java version (read from the
145: * System property "java.version" is equal to or later than the version
146: * string passed in.
147: *
148: * @param minVersion a String representing a Java version, for example "1.3.1". The
149: * String may only contain digits and dots.
150: */
151: public static boolean isCurrentJavaVersionAtLeast(String minVersion) {
152:
153: String version = System.getProperty("java.version");
154: ArrayList list = new ArrayList();
155: StringTokenizer versionTokenizer = new StringTokenizer(version,
156: ".");
157: StringTokenizer minVersionTokenizer = new StringTokenizer(
158: minVersion, ".");
159:
160: while (minVersionTokenizer.hasMoreElements()) {
161:
162: // if the current version has fewer segments than the minimum
163: // version, we'll right-pad it with zeros.
164: int ver = 0;
165: if (versionTokenizer.hasMoreElements()) {
166: ver = Integer.parseInt(versionTokenizer.nextToken());
167: }
168:
169: int min = Integer.parseInt(minVersionTokenizer.nextToken());
170:
171: // each segment of the current version must be at least as
172: // much as the corresponding segment of the minimum version.
173: if (ver < min) {
174: return false;
175: }
176: }
177:
178: return true;
179:
180: }
181:
182: /**
183: * Given a string which may already contain linebreak characters, this method
184: * will separate it into its individual lines, and then pass each line to
185: * breakLine with the lineLength argument.
186: */
187: public static String wrapLines(String lines, int lineLength) {
188:
189: if (lines == null || lines.length() <= lineLength)
190: return lines;
191: StringTokenizer st = new StringTokenizer(lines, "\n", true);
192: int j = 0;
193: ArrayList list = new ArrayList();
194: while (st.hasMoreTokens()) {
195: String s = st.nextToken();
196: j++;
197: if (j % 2 != 0) { // this should be text
198: if (s.equals("\n")) { // blank line
199: list.add("");
200: j++;
201: } else {
202: list.add(breakLine(s, lineLength));
203: }
204: }
205: }
206:
207: StringBuffer buffer = new StringBuffer(lines.length()
208: + list.size() + 10);
209: for (int k = 0; k < list.size(); k++) {
210: buffer.append(list.get(k));
211: buffer.append("\n");
212: }
213:
214: return buffer.toString();
215:
216: }
217:
218: /**
219: * Breaks the given line into segments, separated by newline ('\n')
220: * characters. Each segment is at least "segLength" characters long.
221: * The break occurs at the first space character after seglength.
222: */
223: public static String breakLine(String line, int segLength) {
224: if (line == null || line.length() < segLength)
225: return line;
226: int breakpoint = line.indexOf(" ", segLength);
227: if (breakpoint == -1)
228: return line; // can't break it.
229: String firstSeg = line.substring(0, breakpoint);
230: // make sure the second segment isn't all whitespace
231: String check = line.substring(breakpoint);
232:
233: // if the second segment is whitespace, discard it and return the first
234: // segment.
235: if (check.trim().length() == 0)
236: return firstSeg;
237:
238: // otherwise, insert the '\n' and recursively call breakLine
239: // on the second segment. Return the result.
240: StringBuffer buffer = new StringBuffer(line.length() + 20);
241: buffer.append(firstSeg);
242: buffer.append("\n");
243: buffer.append(breakLine(line.substring(breakpoint + 1),
244: segLength));
245: return buffer.toString();
246: }
247:
248: public static void addMappings(Map map, Object[][] mappings) {
249: for (int j = 0; j < mappings.length; j++) {
250: map.put(mappings[j][0], mappings[j][1]);
251: }
252: }
253:
254: public static HashMap buildMap(Object[][] mappings) {
255: HashMap map = new HashMap(mappings.length + 5);
256: addMappings(map, mappings);
257: return map;
258: }
259:
260: }
|