2.8.3: 2008-10-15

net.sf.basedb.core
Class StringUtil

java.lang.Object
  extended by net.sf.basedb.core.StringUtil

public class StringUtil
extends Object

Utility methods that will make it easier to implement data validation for string attributes.

Version:
2.0
Author:
Nicklas

Constructor Summary
StringUtil()
           
 
Method Summary
static boolean isEqualOrNull(String s1, String s2)
          Check if two strings are equal or both are null.
static boolean isEqualOrNull(String s1, String s2, boolean caseInsensitive)
          Check if two strings are equal or both are null.
static String join(Collection<?> values, String deliminator, boolean skipNull)
          Join a collection of values to a string, using the specified delimiator.
static String setNotNullString(String value, String name, int maxLength)
          Trim and check the length of a string.
static String setNullableString(String value, String name, int maxLength)
          Trim and check the length of a string.
static String trimString(String s, int length)
          Trim a string to a maximum length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

setNullableString

public static String setNullableString(String value,
                                       String name,
                                       int maxLength)
                                throws StringTooLongException
Trim and check the length of a string. Null values are allowed.

Parameters:
value - The string to check
name - The name of the attribute to use if an exception is thrown
maxLength - The maximum length of the string
Returns:
The original string, trimmed from white-space at the beginning and end
Throws:
StringTooLongException - If the string is too long

setNotNullString

public static String setNotNullString(String value,
                                      String name,
                                      int maxLength)
                               throws InvalidUseOfNullException,
                                      StringTooLongException
Trim and check the length of a string. Null values are not allowed.

Parameters:
value - The string to check
name - The name of the attribute to use if an exception is thrown
maxLength - The maximum length of the string
Returns:
The original string, trimmed from white-space at the beginning and end
Throws:
StringTooLongException - If the string is too long
InvalidUseOfNullException - If the string is null

isEqualOrNull

public static boolean isEqualOrNull(String s1,
                                    String s2)
Check if two strings are equal or both are null.

Parameters:
s1 - The first string
s2 - The second string
Returns:
TRUE if the strings are equal or both are null, FALSE otherwise

isEqualOrNull

public static boolean isEqualOrNull(String s1,
                                    String s2,
                                    boolean caseInsensitive)
Check if two strings are equal or both are null.

Parameters:
s1 - The first string
s2 - The second string
caseInsensitive - TRUE if the the comparison should ignore case, FALSE otherwise
Returns:
TRUE if the strings are equal or both are null, FALSE otherwise
Since:
2.4

join

public static final String join(Collection<?> values,
                                String deliminator,
                                boolean skipNull)
Join a collection of values to a string, using the specified delimiator.

Parameters:
values - The values to join
deliminator - The deliminator between the values
skipNull - If null values should be skipper or not
Returns:
The resulting string

trimString

public static final String trimString(String s,
                                      int length)
Trim a string to a maximum length. String longer than the specified length will be cut off and three dots are appended. If no trimming is needed the original string is returned.

Parameters:
s - The string to trim
length - The maximum length of the string
Returns:
The trimmed string

2.8.3: 2008-10-15