Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 91583 invoked from network); 19 Jul 2003 20:22:40 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 19 Jul 2003 20:22:40 -0000 Received: (qmail 29735 invoked by uid 97); 19 Jul 2003 20:25:15 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 29728 invoked from network); 19 Jul 2003 20:25:15 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 19 Jul 2003 20:25:15 -0000 Received: (qmail 91394 invoked by uid 500); 19 Jul 2003 20:22:37 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 91381 invoked by uid 500); 19 Jul 2003 20:22:37 -0000 Received: (qmail 91375 invoked from network); 19 Jul 2003 20:22:37 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 19 Jul 2003 20:22:37 -0000 Received: (qmail 43040 invoked by uid 1529); 19 Jul 2003 20:22:36 -0000 Date: 19 Jul 2003 20:22:36 -0000 Message-ID: <20030719202236.43039.qmail@icarus.apache.org> From: scolebourne@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang WordWrapUtils.java StringEscapeUtils.java CharSetUtils.java SerializationUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N scolebourne 2003/07/19 13:22:36 Modified: lang/src/test/org/apache/commons/lang SerializationUtilsTest.java CharSetUtilsTest.java StringEscapeUtilsTest.java WordWrapUtilsTest.java lang/src/java/org/apache/commons/lang WordWrapUtils.java StringEscapeUtils.java CharSetUtils.java SerializationUtils.java Log: Update null behaviour for consistency and clarity Doument null behaviour Revision Changes Path 1.3 +5 -5 jakarta-commons/lang/src/test/org/apache/commons/lang/SerializationUtilsTest.java Index: SerializationUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/SerializationUtilsTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SerializationUtilsTest.java 23 Mar 2003 21:50:58 -0000 1.2 +++ SerializationUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.3 @@ -157,7 +157,7 @@ ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); try { SerializationUtils.serialize(iMap, null); - } catch (NullPointerException ex) { + } catch (IllegalArgumentException ex) { return; } fail(); @@ -167,7 +167,7 @@ ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); try { SerializationUtils.serialize(null, null); - } catch (NullPointerException ex) { + } catch (IllegalArgumentException ex) { return; } fail(); @@ -210,7 +210,7 @@ public void testDeserializeStreamNull() throws Exception { try { SerializationUtils.deserialize((InputStream) null); - } catch (NullPointerException ex) { + } catch (IllegalArgumentException ex) { return; } fail(); @@ -304,7 +304,7 @@ public void testDeserializeBytesNull() throws Exception { try { SerializationUtils.deserialize((byte[]) null); - } catch (NullPointerException ex) { + } catch (IllegalArgumentException ex) { return; } fail(); 1.8 +60 -66 jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java Index: CharSetUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CharSetUtilsTest.java 23 Mar 2003 21:47:30 -0000 1.7 +++ CharSetUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.8 @@ -63,10 +63,11 @@ * * @author Henri Yandell * @author Ringo De Smet + * @author Stephen Colebourne * @version $Id$ */ -public class CharSetUtilsTest extends TestCase -{ +public class CharSetUtilsTest extends TestCase { + public CharSetUtilsTest(String name) { super(name); } @@ -76,8 +77,8 @@ } public static Test suite() { - TestSuite suite = new TestSuite(CharSetUtilsTest.class); - suite.setName("CharSetUtils Tests"); + TestSuite suite = new TestSuite(CharSetUtilsTest.class); + suite.setName("CharSetUtils Tests"); return suite; } @@ -91,66 +92,59 @@ //----------------------------------------------------------------------- - public void testSqueeze() - { - assertEquals("squeeze(String,String[]) failed", - "helo", CharSetUtils.squeeze("hello", new String[] {"el"})); - assertEquals("squeeze(String,String[]) failed", - "", CharSetUtils.squeeze("", new String[] {"el"})); - assertEquals("squeeze(String,String[]) failed", - "hello", CharSetUtils.squeeze("hello", new String[] {"e"})); - assertEquals("squeeze(String,String[]) failed", - "fofof", CharSetUtils.squeeze("fooffooff", new String[] {"of"})); - assertEquals("squeeze(String,String[]) failed", - "fof", CharSetUtils.squeeze("fooooff", new String[] {"fo"})); - } - - public void testCount() - { - assertEquals("count(String,String[]) failed", - 3, CharSetUtils.count("hello", new String[] {"el"})); - assertEquals("count(String,String[]) failed", - 0, CharSetUtils.count("", new String[] {"el"})); - assertEquals("count(String,String[]) failed", - 0, CharSetUtils.count("hello", new String[] {"x"})); - assertEquals("count(String,String[]) failed", - 2, CharSetUtils.count("hello", new String[] {"e-i"})); - assertEquals("count(String,String[]) failed", - 5, CharSetUtils.count("hello", new String[] {"a-z"})); - assertEquals("count(String,String[]) failed", - 0, CharSetUtils.count("hello", new String[] {""})); - } - - public void testKeep() - { - assertEquals("keep(String,String[]) failed", - "ell", CharSetUtils.keep("hello", new String[] {"el"})); - assertEquals("keep(String,String[]) failed", - "hello", CharSetUtils.keep("hello", new String[] {"elho"})); - assertEquals("keep(String,String[]) failed", - "", CharSetUtils.keep("hello", new String[] {""})); - assertEquals("keep(String,String[]) failed", - "hello", CharSetUtils.keep("hello", new String[] {"a-z"})); - assertEquals("keep(String,String[]) failed", - "----", CharSetUtils.keep("----", new String[] {"-"})); - assertEquals("keep(String,String[]) failed", - "ll", CharSetUtils.keep("hello", new String[] {"l"})); - } - - public void testDelete() - { - assertEquals("delete(String,String[]) failed", - "ho", CharSetUtils.delete("hello", new String[] {"el"})); - assertEquals("delete(String,String[]) failed", - "", CharSetUtils.delete("hello", new String[] {"elho"})); - assertEquals("delete(String,String[]) failed", - "hello", CharSetUtils.delete("hello", new String[] {""})); - assertEquals("delete(String,String[]) failed", - "", CharSetUtils.delete("hello", new String[] {"a-z"})); - assertEquals("delete(String,String[]) failed", - "", CharSetUtils.delete("----", new String[] {"-"})); - assertEquals("delete(String,String[]) failed", - "heo", CharSetUtils.delete("hello", new String[] {"l"})); + public void testSqueeze() { + assertEquals(null, CharSetUtils.squeeze(null, (String[]) null)); + assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" })); + assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" })); + assertEquals("", CharSetUtils.squeeze("", new String[] { "el" })); + assertEquals("hello", CharSetUtils.squeeze("hello", new String[] { "e" })); + assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String[] { "of" })); + assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" })); + try { + CharSetUtils.squeeze("hello", (String[]) null); + } catch (NullPointerException ex) {} + } + + public void testCount() { + assertEquals(0, CharSetUtils.count(null, (String[]) null)); + assertEquals(0, CharSetUtils.count(null, new String[] { "el" })); + assertEquals(3, CharSetUtils.count("hello", new String[] { "el" })); + assertEquals(0, CharSetUtils.count("", new String[] { "el" })); + assertEquals(0, CharSetUtils.count("hello", new String[] { "x" })); + assertEquals(2, CharSetUtils.count("hello", new String[] { "e-i" })); + assertEquals(5, CharSetUtils.count("hello", new String[] { "a-z" })); + assertEquals(0, CharSetUtils.count("hello", new String[] { "" })); + try { + CharSetUtils.count("hello", (String[]) null); + } catch (NullPointerException ex) {} + } + + public void testKeep() { + assertEquals(null, CharSetUtils.keep(null, (String[]) null)); + assertEquals(null, CharSetUtils.keep(null, new String[] { "el" })); + assertEquals("ell", CharSetUtils.keep("hello", new String[] { "el" })); + assertEquals("hello", CharSetUtils.keep("hello", new String[] { "elho" })); + assertEquals("", CharSetUtils.keep("hello", new String[] { "" })); + assertEquals("hello", CharSetUtils.keep("hello", new String[] { "a-z" })); + assertEquals("----", CharSetUtils.keep("----", new String[] { "-" })); + assertEquals("ll", CharSetUtils.keep("hello", new String[] { "l" })); + try { + CharSetUtils.keep("hello", (String[]) null); + } catch (NullPointerException ex) {} + } + + public void testDelete() { + assertEquals(null, CharSetUtils.delete(null, (String[]) null)); + assertEquals(null, CharSetUtils.delete(null, new String[] { "el" })); + assertEquals("ho", CharSetUtils.delete("hello", new String[] { "el" })); + assertEquals("", CharSetUtils.delete("hello", new String[] { "elho" })); + assertEquals("hello", CharSetUtils.delete("hello", new String[] { "" })); + assertEquals("", CharSetUtils.delete("hello", new String[] { "a-z" })); + assertEquals("", CharSetUtils.delete("----", new String[] { "-" })); + assertEquals("heo", CharSetUtils.delete("hello", new String[] { "l" })); + try { + CharSetUtils.delete("hello", (String[]) null); + } catch (NullPointerException ex) {} } + } - 1.9 +49 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java Index: StringEscapeUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- StringEscapeUtilsTest.java 29 Jun 2003 03:05:45 -0000 1.8 +++ StringEscapeUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.9 @@ -87,6 +87,22 @@ //----------------------------------------------------------------------- public void testEscapeJava() throws IOException { + assertEquals(null, StringEscapeUtils.escapeJava(null)); + try { + StringEscapeUtils.escapeJava(null, null); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + try { + StringEscapeUtils.escapeJava(null, ""); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + assertEscapeJava("empty string", "", ""); assertEscapeJava(FOO, FOO); assertEscapeJava("tab", "\\t", "\t"); @@ -122,6 +138,22 @@ } public void testUnescapeJava() throws IOException { + assertEquals(null, StringEscapeUtils.unescapeJava(null)); + try { + StringEscapeUtils.unescapeJava(null, null); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + try { + StringEscapeUtils.unescapeJava(null, ""); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + assertUnescapeJava("", ""); assertUnescapeJava("test", "test"); assertUnescapeJava("\ntest\b", "\\ntest\\b"); @@ -154,6 +186,22 @@ } public void testEscapeJavaScript() { + assertEquals(null, StringEscapeUtils.escapeJavaScript(null)); + try { + StringEscapeUtils.escapeJavaScript(null, null); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + try { + StringEscapeUtils.escapeJavaScript(null, ""); + fail(); + } catch (IOException ex) { + fail(); + } catch (IllegalArgumentException ex) { + } + assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeJavaScript("He didn't say, \"stop!\"")); } 1.2 +6 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/WordWrapUtilsTest.java Index: WordWrapUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/WordWrapUtilsTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WordWrapUtilsTest.java 15 Apr 2003 14:28:41 -0000 1.1 +++ WordWrapUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.2 @@ -107,4 +107,9 @@ "Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns."; assertEquals("Text with tab at wrapping index didn't wrap correctly, ", expected, WordWrapUtils.wrapText(input, "\n", 20)); } + + public void testWrapText4() { + assertEquals(null, WordWrapUtils.wrapText(null, "\n", 20)); + assertEquals("", WordWrapUtils.wrapText("", "\n", 20)); + } } 1.7 +19 -8 jakarta-commons/lang/src/java/org/apache/commons/lang/WordWrapUtils.java Index: WordWrapUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/WordWrapUtils.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- WordWrapUtils.java 12 Jul 2003 03:06:23 -0000 1.6 +++ WordWrapUtils.java 19 Jul 2003 20:22:36 -0000 1.7 @@ -57,7 +57,11 @@ import java.util.StringTokenizer; /** - * WordWrapUtils is a utility class to assist with word wrapping. + *

WordWrapUtils is a utility class to assist with word wrapping.

+ * + *

This class tries to handle null input gracefully. + * An exception will not be thrown for a null input. + * Each method documents its behaviour in more detail.

* * @author Henri Yandell * @author Stephen Colebourne @@ -91,13 +95,20 @@ * since tabs are a single character but are displayed as 4 or 8 * spaces. Remove the tabs.

* - * @param str text which is in need of word-wrapping - * @param newline the characters that define a newline + * @param str text which is in need of word-wrapping, may be null + * @param newLineChars the characters that define a newline, null treated as \n * @param wrapColumn the column to wrap the words at - * @return the text with all the long lines word-wrapped + * @return the text with all the long lines word-wrapped, + * null if null string input */ - public static String wrapText(String str, String newline, int wrapColumn) { - StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true); + public static String wrapText(String str, String newLineChars, int wrapColumn) { + if (str == null) { + return null; + } + if (newLineChars == null) { + newLineChars = "\n"; + } + StringTokenizer lineTokenizer = new StringTokenizer(str, newLineChars, true); StringBuffer stringBuffer = new StringBuffer(); while (lineTokenizer.hasMoreTokens()) { @@ -106,7 +117,7 @@ if (nextLine.length() > wrapColumn) { // This line is long enough to be wrapped. - nextLine = wrapLine(nextLine, newline, wrapColumn); + nextLine = wrapLine(nextLine, newLineChars, wrapColumn); } stringBuffer.append(nextLine); 1.17 +77 -41 jakarta-commons/lang/src/java/org/apache/commons/lang/StringEscapeUtils.java Index: StringEscapeUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringEscapeUtils.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- StringEscapeUtils.java 8 Jul 2003 05:59:58 -0000 1.16 +++ StringEscapeUtils.java 19 Jul 2003 20:22:36 -0000 1.17 @@ -60,12 +60,9 @@ /** *

Escapes and unescapes Strings for Java, Java Script, HTML, XML, and SQL. * - *

Originally from - * Turbine and the - * GenerationJavaCore library and from - * Purple Technology - *

- * + * @author Apache Jakarta Turbine + * @author GenerationJavaCore library + * @author Purple Technology * @author Henri Yandell * @author Alexander Day Chaffee * @author Antony Riley @@ -115,9 +112,8 @@ * *

* - * @param str String to escape values in - * @return String with escaped values - * @throws NullPointerException if str is null + * @param str String to escape values in, may be null + * @return String with escaped values, null if null string input */ public static String escapeJava(String str) { return escapeJavaStyleString(str, false); @@ -127,10 +123,12 @@ *

Escapes the characters in a String using Java String rules to * a Writer.

* + *

A null string input has no effect.

+ * * @see #escapeJava(java.lang.String) - * @param out Writer to write escaped string into - * @param str String to escape values in - * @throws NullPointerException if str is null + * @param out Writer to write escaped string into + * @param str String to escape values in, may be null + * @throws IllegalArgumentException if the Writer is null * @throws IOException if error occurs on undelying Writer */ public static void escapeJava(Writer out, String str) throws IOException { @@ -155,9 +153,8 @@ * *

* - * @param str String to escape values in - * @return String with escaped values - * @throws NullPointerException if str is null + * @param str String to escape values in, may be null + * @return String with escaped values, null if null string input */ public static String escapeJavaScript(String str) { return escapeJavaStyleString(str, true); @@ -167,10 +164,12 @@ *

Escapes the characters in a String using JavaScript String rules * to a Writer.

* + *

A null string input has no effect.

+ * * @see #escapeJavaScript(java.lang.String) - * @param out Writer to write escaped string into - * @param str String to escape values in - * @throws NullPointerException if str is null + * @param out Writer to write escaped string into + * @param str String to escape values in, may be null + * @throws IllegalArgumentException if the Writer is null * @throws IOException if error occurs on undelying Writer **/ public static void escapeJavaScript(Writer out, String str) throws IOException { @@ -178,6 +177,9 @@ } private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) { + if (str == null) { + return null; + } try { StringPrintWriter writer = new StringPrintWriter(str.length() * 2); escapeJavaStyleString(writer, str, escapeSingleQuotes); @@ -190,6 +192,12 @@ } private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException { + if (out == null) { + throw new NullArgumentException("Writer"); + } + if (str == null) { + return; + } int sz; sz = str.length(); for (int i = 0; i < sz; i++) { @@ -271,10 +279,13 @@ * 'n' into a newline character, unless the '\' * is preceded by another '\'.

* - * @param str The String to unescape. - * @return A new unescaped String. + * @param str the String to unescape, may be null + * @return a new unescaped String, null if null string input */ public static String unescapeJava(String str) { + if (str == null) { + return null; + } try { StringPrintWriter writer = new StringPrintWriter(str.length()); unescapeJava(writer, str); @@ -294,10 +305,19 @@ * 'n' into a newline character, unless the '\' * is preceded by another '\'.

* - * @param out The Writer used to output unescaped characters. - * @param str The String to unescape. + *

A null string input has no effect.

+ * + * @param out the Writer used to output unescaped characters + * @param str the String to unescape, may be null + * @throws IllegalArgumentException if the Writer is null */ public static void unescapeJava(Writer out, String str) throws IOException { + if (out == null) { + throw new NullArgumentException("Writer"); + } + if (str == null) { + return; + } int sz = str.length(); StringBuffer unicode = new StringBuffer(4); boolean hadSlash = false; @@ -382,9 +402,9 @@ * into a newline character, unless the '\' is preceded by another * '\'.

* - * @param str The String to unescape. - * @return A new unescaped String. * @see #unescapeJava(String) + * @param str the String to unescape, may be null + * @return A new unescaped String, null if null string input */ public static String unescapeJavaScript(String str) { return unescapeJava(str); @@ -398,10 +418,12 @@ * into a newline character, unless the '\' is preceded by another * '\'.

* - * @param out The Writer used to output unescaped characters. - * @param str The String to unescape. - + *

A null string input has no effect.

+ * * @see #unescapeJava(Writer,String) + * @param out the Writer used to output unescaped characters + * @param str the String to unescape, may be null + * @throws IllegalArgumentException if the Writer is null */ public static void unescapeJavaScript(Writer out, String str) throws IOException { unescapeJava(out, str); @@ -419,8 +441,8 @@ * *

Supports all known HTML 4.0 entities, including funky accents.

* - * @param str The String to escape - * @return A new escaped String. + * @param str the String to escape, may be null + * @return a new escaped String, null if null string input * * @see Entities * @see #unescapeHtml(String) @@ -431,6 +453,9 @@ * @see
HTML 4.01 Code positions **/ public static String escapeHtml(String str) { + if (str == null) { + return null; + } //todo: add a version that takes a Writer //todo: rewrite underlying method to use a Writer instead of a StringBuffer return Entities.HTML40.escape(str); @@ -448,11 +473,14 @@ * verbatim into the result string. e.g. "&gt;&zzzz;x" will * become ">&zzzz;x".

* - * @param str The String to unescape - * @return A new unescaped String. + * @param str the String to unescape, may be null + * @return a new unescaped String, null if null string input * @see #escapeHtml(String) **/ public static String unescapeHtml(String str) { + if (str == null) { + return null; + } return Entities.HTML40.unescape(str); } @@ -466,11 +494,14 @@ *

Supports only the four basic XML entities (gt, lt, quot, amp). * Does not support DTDs or external entities.

* - * @param str The String to escape - * @return A new escaped String. + * @param str the String to escape, may be null + * @return a new escaped String, null if null string input * @see #unescapeXml(java.lang.String) **/ public static String escapeXml(String str) { + if (str == null) { + return null; + } return Entities.XML.escape(str); } @@ -482,11 +513,14 @@ *

Supports only the four basic XML entities (gt, lt, quot, amp). * Does not support DTDs or external entities.

* - * @param str The String to unescape - * @return A new unescaped String. + * @param str the String to unescape, may be null + * @return a new unescaped String, null if null string input * @see #escapeXml(String) **/ public static String unescapeXml(String str) { + if (str == null) { + return null; + } return Entities.XML.unescape(str); } @@ -505,12 +539,14 @@ * handle the cases of percent (%) or underscore (_) for use in LIKE clauses.

* * see http://www.jguru.com/faq/view.jsp?EID=8881 - * @param s the string to escape - * @return A new String, escaped for SQL + * @param str the string to escape, may be null + * @return a new String, escaped for SQL, null if null string input */ - public static String escapeSql(String s) - { - return StringUtils.replace(s, "'", "''"); + public static String escapeSql(String str) { + if (str == null) { + return null; + } + return StringUtils.replace(str, "'", "''"); } } 1.14 +66 -34 jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java Index: CharSetUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- CharSetUtils.java 16 Jul 2003 00:39:05 -0000 1.13 +++ CharSetUtils.java 19 Jul 2003 20:22:36 -0000 1.14 @@ -56,6 +56,10 @@ /** *

Numerous routines to manipulate a CharSet.

* + *

This class tries to handle null input gracefully. + * An exception will generally not be thrown for a null input. + * Each method documents its behaviour in more detail.

+ * * @author Henri Yandell * @author Stephen Colebourne * @since 1.0 @@ -84,8 +88,8 @@ *
  • "ej-m" implies e,j->m. e,j,k,l,m.
  • * * - * @param set - * @return CharSet + * @param set the set, must not be null + * @return a CharSet instance * @throws NullPointerException if any of set[i] is null or if set is null */ public static CharSet evaluateSet(String[] set) { @@ -102,12 +106,15 @@ * * @see #evaluateSet(java.lang.String[]) for set-syntax. * - * @param str the string to work from - * @param set the character set to use for manipulation - * @throws NullPointerException if str is null + * @param str the string to squeeze, may be null + * @param set the character set to use for manipulation, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null */ public static String squeeze(String str, String set) { + if (str == null) { + return null; + } String[] strs = new String[1]; strs[0] = set; return squeeze(str, strs); @@ -123,13 +130,16 @@ * * @see #evaluateSet(java.lang.String[]) for set-syntax. * - * @param str the string to work from - * @param set the character set to use for manipulation - * @throws NullPointerException if str is null + * @param str the string to squeeze, may be null + * @param set the character set to use for manipulation, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null * or any element is null */ public static String squeeze(String str, String[] set) { + if (str == null) { + return null; + } CharSet chars = evaluateSet(set); StringBuffer buffer = new StringBuffer(str.length()); char[] chrs = str.toCharArray(); @@ -158,12 +168,15 @@ *
  • count("hello", {"c-f", "o"}) returns 2.
  • * * - * @param str String target to count characters in - * @param set String set of characters to count - * @throws NullPointerException if str is null + * @param str String to count characters in, may be null + * @param set String set of characters to count, must not be null + * @return character count, zero if null string input * @throws NullPointerException if set is null */ public static int count(String str, String set) { + if (str == null) { + return 0; + } String[] strs = new String[1]; strs[0] = set; return count(str, strs); @@ -178,13 +191,16 @@ *
  • count("hello", {"c-f", "o"}) returns 2.
  • * * - * @param str String target to count characters in - * @param set String[] set of characters to count - * @throws NullPointerException if str is null + * @param str String to count characters in, may be null + * @param set String[] set of characters to count, must not be null + * @return character count, zero if null string input * @throws NullPointerException if set is null * or any element is null */ public static int count(String str, String[] set) { + if (str == null) { + return 0; + } CharSet chars = evaluateSet(set); int count = 0; char[] chrs = str.toCharArray(); @@ -206,12 +222,15 @@ *
  • keep("hello", {"c-fo"}) returns "hll"
  • * * - * @param str String target to keep characters from - * @param set String set of characters to keep - * @throws NullPointerException if str is null + * @param str String to keep characters from, may be null + * @param set String set of characters to keep, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null */ public static String keep(String str, String set) { + if (str == null) { + return null; + } String[] strs = new String[1]; strs[0] = set; return keep(str, strs); @@ -227,13 +246,16 @@ * returns "hll" * * - * @param str String target to keep characters from - * @param set String[] set of characters to keep - * @throws NullPointerException if str is null + * @param str String to keep characters from, may be null + * @param set String[] set of characters to keep, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null * or any element is null */ public static String keep(String str, String[] set) { + if (str == null) { + return null; + } return modify(str, set, true); } @@ -246,12 +268,15 @@ *
  • delete("hello", {"c-fo"}) returns "hll"
  • * * - * @param str String target to delete characters from - * @param set String set of characters to delete - * @throws NullPointerException if str is null + * @param str String to delete characters from, may be null + * @param set String set of characters to delete, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null */ public static String delete(String str, String set) { + if (str == null) { + return null; + } String[] strs = new String[1]; strs[0] = set; return delete(str, strs); @@ -267,13 +292,16 @@ * "hll" * * - * @param str String target to delete characters from - * @param set String[] set of characters to delete - * @throws NullPointerException if str is null + * @param str String to delete characters from, may be null + * @param set String[] set of characters to delete, must not be null + * @return modified String, null if null string input * @throws NullPointerException if set is null * or any element is null */ public static String delete(String str, String[] set) { + if (str == null) { + return null; + } return modify(str, set, false); } @@ -304,15 +332,19 @@ * length of characters to replace, then the last character is * used.

    * - * @param target String to replace characters in - * @param repl String to find that will be replaced - * @param with String to put into the target String - * @throws NullPointerException if target, with + * @param str String to replace characters in, may be null + * @param repl String to find that will be replaced, must not be null + * @param with String to put into the target String, must not be null + * @return translated String, null if null string input + * @throws NullPointerException if target, with * or repl is null */ - public static String translate(String target, String repl, String with) { - StringBuffer buffer = new StringBuffer(target.length()); - char[] chrs = target.toCharArray(); + public static String translate(String str, String repl, String with) { + if (str == null) { + return null; + } + StringBuffer buffer = new StringBuffer(str.length()); + char[] chrs = str.toCharArray(); char[] withChrs = with.toCharArray(); int sz = chrs.length; int withMax = with.length() - 1; 1.7 +20 -5 jakarta-commons/lang/src/java/org/apache/commons/lang/SerializationUtils.java Index: SerializationUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/SerializationUtils.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- SerializationUtils.java 23 Jun 2003 22:36:50 -0000 1.6 +++ SerializationUtils.java 19 Jul 2003 20:22:36 -0000 1.7 @@ -72,6 +72,9 @@ *
  • Deserialize managing finally and IOException * * + *

    This class throws exceptions for invalid null inputs. + * Each method documents its behaviour in more detail.

    + * * @author Nissim Karpenstein * @author Janek Bogucki * @author Daniel Rall @@ -120,11 +123,15 @@ *

    The stream passed in is not buffered internally within this method. * This is the responsibility of your application if desired.

    * - * @param obj the object to serialize to bytes - * @param outputStream the stream to write to + * @param obj the object to serialize to bytes, may be null + * @param outputStream the stream to write to, must not be null + * @throws IllegalArgumentException if outputStream is null * @throws SerializationException (runtime) if the serialization fails */ public static void serialize(Serializable obj, OutputStream outputStream) { + if (outputStream == null) { + throw new NullArgumentException("OutputStream"); + } ObjectOutputStream out = null; try { // stream closed in the finally @@ -168,11 +175,15 @@ *

    The stream passed in is not buffered internally within this method. * This is the responsibility of your application if desired.

    * - * @param inputStream the serialized object input stream + * @param inputStream the serialized object input stream, must not be null * @return the deserialized object + * @throws IllegalArgumentException if inputStream is null * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(InputStream inputStream) { + if (inputStream == null) { + throw new NullArgumentException("InputStream"); + } ObjectInputStream in = null; try { // stream closed in the finally @@ -197,11 +208,15 @@ /** *

    Deserializes a single Object from an array of bytes.

    * - * @param objectData the serialized object + * @param objectData the serialized object, must not be null * @return the deserialized object + * @throws IllegalArgumentException if objectData is null * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(byte[] objectData) { + if (objectData == null) { + throw new NullArgumentException("byte[]"); + } ByteArrayInputStream bais = new ByteArrayInputStream(objectData); return deserialize(bais); } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org