源码解读心得 - net.sf.hibernate.util.StringHelper
给这个类做了一下注释,不是很详细,而且还有一些地方不是很清楚。
//$Id: StringHelper.java,v 1.16.2.11 2004/01/10 03:14:04 oneovthafew Exp $
package net.sf.hibernate.util;
import java.util.Iterator;
import java.util.StringTokenizer;
public final class StringHelper {
/**
* 下面定义了一些常用的字符串常量
*/
public static final String EMPTY_STRING=""; // 空字符串
public static final char DOT='.'; // 点
public static final char UNDERSCORE='_'; // 下划线
public static final String COMMA_SPACE=", "; // 逗号+空格
public static final String COMMA = ","; // 逗号
public static final String OPEN_PAREN = "("; // 左括号
public static final String CLOSE_PAREN = ")"; // 右括号
public static final char SINGLE_QUOTE = '\''; // 单引号
/**
* 连接多个字符串
*/
public static String join(String seperator, String[] strings) {
int length = strings.length; // 取需要连接的字符串的数目
if (length==0) return EMPTY_STRING; //如果字符串个数为0,则返回空字符串
StringBuffer buf = new StringBuffer( length * strings[0].length() ) // 创建字符串缓存,以便进行字符串的连接
.append(strings[0]); //写入第一个字符串
// 循环写入后面的字符串
for (int i=1; i<length; i++) {
buf.append(seperator).append(strings[i]);
}
// 返回结果
return buf.toString();
}
public static String join(String seperator, Iterator objects) {
StringBuffer buf = new StringBuffer(); // 创建字符串缓存
if ( objects.hasNext() ) buf.append( objects.next() ); // 写入第一个对象
// 循环写入后面的对象
while ( objects.hasNext() ) {
buf.append(seperator).append( objects.next() );
}
// 返回结果
return buf.toString();
}
// 一一对应地连接两个字符串数组
public static String[] add(String[] x, String sep, String[] y) {
String[] result = new String[ x.length ]; // 新建用于返回的字符串数组
// 对各个字符串对进行连接
for ( int i=0; i<x.length; i++ ) {
result[i] = x[i] + sep + y[i];
}
return result;
}
// 生成重复字符串
public static String repeat(String string, int times) {
StringBuffer buf = new StringBuffer( string.length() * times );
for (int i=0; i<times; i++) buf.append(string);
return buf.toString();
}
// 替换字符串
public static String replace(String template, String placeholder, String replacement) {
return replace(template, placeholder, replacement, false);
}
// 替换字符串
public static String replace(String template, String placeholder, String replacement, boolean wholeWords) {
// 取待替换的字符串的位置
int loc = template.indexOf(placeholder);
// 如果没有找到,则返回原串 (递归的终点)
if (loc<0) {
return template;
}
else {
/** 此段代码有点难理解,重构如下:
final boolean actuallyReplace;
if ( wholeWords ) {
if ( loc + placeholder.length() == template.length() ) {
actuallyReplace = false;
}
else {
if ( Character.isJavaIdentifierPart( template.charAt( loc + placeholder.length() ) {
actuallyReplace = false;
}
else actuallyReplace = true;
}
}
else actuallyReplace = false;
*/
final boolean actuallyReplace = !wholeWords ||
loc + placeholder.length() == template.length() || // 待替换字符串是否处于原字符串的末尾
!Character.isJavaIdentifierPart( template.charAt( loc + placeholder.length() ) ); // 确保后面没有Java Identifier字符
//
String actualReplacement = actuallyReplace ? replacement : placeholder;
return new StringBuffer( template.substring(0, loc) ) // 先写入没有变动的
.append(actualReplacement) // 再写入被替换的部分
.append( replace( // 递归调用replace方法,替换后面全部的字符串
template.substring( loc + placeholder.length() ),
placeholder,
replacement,
wholeWords
) ).toString();
}
}
// 仅替换一次
public static String replaceOnce(String template, String placeholder, String replacement) {
int loc = template.indexOf(placeholder);
if ( loc<0 ) {
return template;
}
else {
return new StringBuffer( template.substring(0, loc) )
.append(replacement)
.append( template.substring( loc + placeholder.length() ) )
.toString();
}
}
// 将字符串按指定分隔符分开
public static String[] split(String seperators, String list) {
return split(seperators, list, false);
}
// 将字符串按指定分隔符分开
public static String[] split(String seperators, String list, boolean include) {
StringTokenizer tokens = new StringTokenizer(list, seperators, include);
String[] result = new String[ tokens.countTokens() ];
int i=0;
while ( tokens.hasMoreTokens() ) {
result[i++] = tokens.nextToken();
}
return result;
}
// 取文件名的后缀
public static String unqualify(String qualifiedName) {
return unqualify(qualifiedName, ".");
}
// 取符号 seperator 后面的字符串
public static String unqualify(String qualifiedName, String seperator) {
return qualifiedName.substring( qualifiedName.lastIndexOf(seperator) + 1 );
}
// 取不含后缀的文件名称
public static String qualifier(String qualifiedName) {
int loc = qualifiedName.lastIndexOf(".");
if ( loc<0 ) {
return EMPTY_STRING;
}
else {
return qualifiedName.substring(0, loc);
}
}
// 给字符串加后缀
public static String[] suffix( String[] columns, String suffix) {
if (suffix==null) return columns;
String[] qualified = new String[columns.length];
for ( int i=0; i<columns.length; i++ ) {
qualified[i] = suffix( columns[i], suffix);
}
return qualified;
}
// 给字符串加后缀
public static String suffix(String name, String suffix) {
return (suffix==null) ? name : name + suffix;
}
// 给字符串加前缀
public static String[] prefix( String[] columns, String prefix) {
if (prefix==null) return columns;
String[] qualified = new String[columns.length];
for ( int i=0; i<columns.length; i++ ) {
qualified[i] = prefix + columns[i];
}
return qualified;
}
// 返回字符串中.前面的部分
public static String root(String qualifiedName) {
int loc = qualifiedName.indexOf(".");
return (loc<0) ? qualifiedName : qualifiedName.substring(0, loc);
}
// 把字符串转换成boolean型
public static boolean booleanValue(String tfString) {
String trimmed = tfString.trim().toLowerCase();
return trimmed.equals("true") || trimmed.equals("t");
}
// 把数组转换成字符串
public static String toString(Object[] array) {
int len = array.length;
if (len==0) return StringHelper.EMPTY_STRING;
StringBuffer buf = new StringBuffer(len * 12);
for ( int i=0; i<len-1; i++ ) {
buf.append( array[i] ).append(StringHelper.COMMA_SPACE);
}
return buf.append( array[len-1] ).toString();
}
// 字符串替换的一个“笛卡儿积”(不明白有什么用)
public static String[] multiply(String string, Iterator placeholders, Iterator replacements) {
String[] result = new String[] { string };
while ( placeholders.hasNext() ) {
result = multiply( result, (String) placeholders.next(), (String[]) replacements.next() );
}
return result;
}
// 字符串替换的一个“积”,枚举所有可以替换的可能
private static String[] multiply(String[] strings, String placeholder, String[] replacements) {
String[] results = new String[ replacements.length * strings.length ];
int n=0;
for ( int i=0; i<replacements.length; i++ ) {
for ( int j=0; j<strings.length; j++ ) {
results[n++] = replaceOnce( strings[j], placeholder, replacements[i] );
}
}
return results;
}
/*public static String unQuote(String name) {
return ( Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) ?
name.substring(1, name.length()-1) :
name;
}
public static void unQuoteInPlace(String[] names) {
for ( int i=0; i<names.length; i++ ) names[i] = unQuote( names[i] );
}
public static String[] unQuote(String[] names) {
String[] unquoted = new String[ names.length ];
for ( int i=0; i<names.length; i++ ) unquoted[i] = unQuote( names[i] );
return unquoted;
}*/
// 计算指定字符出现的次数
public static int count(String string, char character) {
int n=0;
for ( int i=0; i<string.length(); i++ ) {
if ( string.charAt(i)==character ) n++;
}
return n;
}
// 计算未被包含在单引号对中的指定字符的个数
public static int countUnquoted(String string, char character) {
if ( SINGLE_QUOTE == character ) {
throw new IllegalArgumentException("Unquoted count of quotes is invalid");
}
// Impl note: takes advantage of the fact that an escpaed single quote
// embedded within a quote-block can really be handled as two seperate
// quote-blocks for the purposes of this method
int count=0;
int stringLength = string == null ? 0 : string.length();
boolean inQuote = false; // 是否被包含在单引号对之内
for ( int indx=0; indx<stringLength; indx++ ) {
if ( inQuote ) {
// 不断循环,直到遇到第二个单引号inQuote才变为false
if ( SINGLE_QUOTE == string.charAt(indx) ) {
inQuote = false;
}
}
else if ( SINGLE_QUOTE == string.charAt(indx) ) {
inQuote = true;
}
else if ( string.charAt(indx)==character ) {
count++;
}
}
return count;
}
// 判断字符串是否为空
public static boolean isNotEmpty(String string) {
return string!=null && string.length() > 0;
}
// 生成具有限定符“.”的字符串
public static String qualify(String prefix, String name) {
return new StringBuffer( prefix.length() + name.length() + 1 )
.append(prefix)
.append(DOT)
.append(name)
.toString();
}
// 生成具有限定符“.”的字符串
public static String[] qualify(String prefix, String[] names) {
if (prefix==null) return names;
int len = names.length;
String[] qualified = new String[len];
for ( int i=0; i<len; i++) {
qualified[i] = qualify( prefix, names[i] );
}
return qualified;
}
private StringHelper() { /* static methods only - hide constructor */ }
// 对求字符索引的一个扩展。求字符串string中包含的字符在sqlString中存在的最小位置
public static int firstIndexOfChar(String sqlString, String string, int startindex) {
int matchAt=-1; //此处 -1 可以理解为无穷大
for (int i = 0; i < string.length(); i++) {
int curMatch = sqlString.indexOf( string.charAt(i), startindex );
if (curMatch >=0) {
if (matchAt==-1) { // first time we find match!
matchAt = curMatch;
}
else {
matchAt = Math.min(matchAt, curMatch);
}
}
}
return matchAt;
}
// 取字符串的前面指定长度的字符串
public static String truncate(String string, int length) {
if (string.length()<=length) {
return string;
}
else {
return string.substring(0, length);
}
}
}
package net.sf.hibernate.util;
import java.util.Iterator;
import java.util.StringTokenizer;
public final class StringHelper {
/**
* 下面定义了一些常用的字符串常量
*/
public static final String EMPTY_STRING=""; // 空字符串
public static final char DOT='.'; // 点
public static final char UNDERSCORE='_'; // 下划线
public static final String COMMA_SPACE=", "; // 逗号+空格
public static final String COMMA = ","; // 逗号
public static final String OPEN_PAREN = "("; // 左括号
public static final String CLOSE_PAREN = ")"; // 右括号
public static final char SINGLE_QUOTE = '\''; // 单引号
/**
* 连接多个字符串
*/
public static String join(String seperator, String[] strings) {
int length = strings.length; // 取需要连接的字符串的数目
if (length==0) return EMPTY_STRING; //如果字符串个数为0,则返回空字符串
StringBuffer buf = new StringBuffer( length * strings[0].length() ) // 创建字符串缓存,以便进行字符串的连接
.append(strings[0]); //写入第一个字符串
// 循环写入后面的字符串
for (int i=1; i<length; i++) {
buf.append(seperator).append(strings[i]);
}
// 返回结果
return buf.toString();
}
public static String join(String seperator, Iterator objects) {
StringBuffer buf = new StringBuffer(); // 创建字符串缓存
if ( objects.hasNext() ) buf.append( objects.next() ); // 写入第一个对象
// 循环写入后面的对象
while ( objects.hasNext() ) {
buf.append(seperator).append( objects.next() );
}
// 返回结果
return buf.toString();
}
// 一一对应地连接两个字符串数组
public static String[] add(String[] x, String sep, String[] y) {
String[] result = new String[ x.length ]; // 新建用于返回的字符串数组
// 对各个字符串对进行连接
for ( int i=0; i<x.length; i++ ) {
result[i] = x[i] + sep + y[i];
}
return result;
}
// 生成重复字符串
public static String repeat(String string, int times) {
StringBuffer buf = new StringBuffer( string.length() * times );
for (int i=0; i<times; i++) buf.append(string);
return buf.toString();
}
// 替换字符串
public static String replace(String template, String placeholder, String replacement) {
return replace(template, placeholder, replacement, false);
}
// 替换字符串
public static String replace(String template, String placeholder, String replacement, boolean wholeWords) {
// 取待替换的字符串的位置
int loc = template.indexOf(placeholder);
// 如果没有找到,则返回原串 (递归的终点)
if (loc<0) {
return template;
}
else {
/** 此段代码有点难理解,重构如下:
final boolean actuallyReplace;
if ( wholeWords ) {
if ( loc + placeholder.length() == template.length() ) {
actuallyReplace = false;
}
else {
if ( Character.isJavaIdentifierPart( template.charAt( loc + placeholder.length() ) {
actuallyReplace = false;
}
else actuallyReplace = true;
}
}
else actuallyReplace = false;
*/
final boolean actuallyReplace = !wholeWords ||
loc + placeholder.length() == template.length() || // 待替换字符串是否处于原字符串的末尾
!Character.isJavaIdentifierPart( template.charAt( loc + placeholder.length() ) ); // 确保后面没有Java Identifier字符
//
String actualReplacement = actuallyReplace ? replacement : placeholder;
return new StringBuffer( template.substring(0, loc) ) // 先写入没有变动的
.append(actualReplacement) // 再写入被替换的部分
.append( replace( // 递归调用replace方法,替换后面全部的字符串
template.substring( loc + placeholder.length() ),
placeholder,
replacement,
wholeWords
) ).toString();
}
}
// 仅替换一次
public static String replaceOnce(String template, String placeholder, String replacement) {
int loc = template.indexOf(placeholder);
if ( loc<0 ) {
return template;
}
else {
return new StringBuffer( template.substring(0, loc) )
.append(replacement)
.append( template.substring( loc + placeholder.length() ) )
.toString();
}
}
// 将字符串按指定分隔符分开
public static String[] split(String seperators, String list) {
return split(seperators, list, false);
}
// 将字符串按指定分隔符分开
public static String[] split(String seperators, String list, boolean include) {
StringTokenizer tokens = new StringTokenizer(list, seperators, include);
String[] result = new String[ tokens.countTokens() ];
int i=0;
while ( tokens.hasMoreTokens() ) {
result[i++] = tokens.nextToken();
}
return result;
}
// 取文件名的后缀
public static String unqualify(String qualifiedName) {
return unqualify(qualifiedName, ".");
}
// 取符号 seperator 后面的字符串
public static String unqualify(String qualifiedName, String seperator) {
return qualifiedName.substring( qualifiedName.lastIndexOf(seperator) + 1 );
}
// 取不含后缀的文件名称
public static String qualifier(String qualifiedName) {
int loc = qualifiedName.lastIndexOf(".");
if ( loc<0 ) {
return EMPTY_STRING;
}
else {
return qualifiedName.substring(0, loc);
}
}
// 给字符串加后缀
public static String[] suffix( String[] columns, String suffix) {
if (suffix==null) return columns;
String[] qualified = new String[columns.length];
for ( int i=0; i<columns.length; i++ ) {
qualified[i] = suffix( columns[i], suffix);
}
return qualified;
}
// 给字符串加后缀
public static String suffix(String name, String suffix) {
return (suffix==null) ? name : name + suffix;
}
// 给字符串加前缀
public static String[] prefix( String[] columns, String prefix) {
if (prefix==null) return columns;
String[] qualified = new String[columns.length];
for ( int i=0; i<columns.length; i++ ) {
qualified[i] = prefix + columns[i];
}
return qualified;
}
// 返回字符串中.前面的部分
public static String root(String qualifiedName) {
int loc = qualifiedName.indexOf(".");
return (loc<0) ? qualifiedName : qualifiedName.substring(0, loc);
}
// 把字符串转换成boolean型
public static boolean booleanValue(String tfString) {
String trimmed = tfString.trim().toLowerCase();
return trimmed.equals("true") || trimmed.equals("t");
}
// 把数组转换成字符串
public static String toString(Object[] array) {
int len = array.length;
if (len==0) return StringHelper.EMPTY_STRING;
StringBuffer buf = new StringBuffer(len * 12);
for ( int i=0; i<len-1; i++ ) {
buf.append( array[i] ).append(StringHelper.COMMA_SPACE);
}
return buf.append( array[len-1] ).toString();
}
// 字符串替换的一个“笛卡儿积”(不明白有什么用)
public static String[] multiply(String string, Iterator placeholders, Iterator replacements) {
String[] result = new String[] { string };
while ( placeholders.hasNext() ) {
result = multiply( result, (String) placeholders.next(), (String[]) replacements.next() );
}
return result;
}
// 字符串替换的一个“积”,枚举所有可以替换的可能
private static String[] multiply(String[] strings, String placeholder, String[] replacements) {
String[] results = new String[ replacements.length * strings.length ];
int n=0;
for ( int i=0; i<replacements.length; i++ ) {
for ( int j=0; j<strings.length; j++ ) {
results[n++] = replaceOnce( strings[j], placeholder, replacements[i] );
}
}
return results;
}
/*public static String unQuote(String name) {
return ( Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) ?
name.substring(1, name.length()-1) :
name;
}
public static void unQuoteInPlace(String[] names) {
for ( int i=0; i<names.length; i++ ) names[i] = unQuote( names[i] );
}
public static String[] unQuote(String[] names) {
String[] unquoted = new String[ names.length ];
for ( int i=0; i<names.length; i++ ) unquoted[i] = unQuote( names[i] );
return unquoted;
}*/
// 计算指定字符出现的次数
public static int count(String string, char character) {
int n=0;
for ( int i=0; i<string.length(); i++ ) {
if ( string.charAt(i)==character ) n++;
}
return n;
}
// 计算未被包含在单引号对中的指定字符的个数
public static int countUnquoted(String string, char character) {
if ( SINGLE_QUOTE == character ) {
throw new IllegalArgumentException("Unquoted count of quotes is invalid");
}
// Impl note: takes advantage of the fact that an escpaed single quote
// embedded within a quote-block can really be handled as two seperate
// quote-blocks for the purposes of this method
int count=0;
int stringLength = string == null ? 0 : string.length();
boolean inQuote = false; // 是否被包含在单引号对之内
for ( int indx=0; indx<stringLength; indx++ ) {
if ( inQuote ) {
// 不断循环,直到遇到第二个单引号inQuote才变为false
if ( SINGLE_QUOTE == string.charAt(indx) ) {
inQuote = false;
}
}
else if ( SINGLE_QUOTE == string.charAt(indx) ) {
inQuote = true;
}
else if ( string.charAt(indx)==character ) {
count++;
}
}
return count;
}
// 判断字符串是否为空
public static boolean isNotEmpty(String string) {
return string!=null && string.length() > 0;
}
// 生成具有限定符“.”的字符串
public static String qualify(String prefix, String name) {
return new StringBuffer( prefix.length() + name.length() + 1 )
.append(prefix)
.append(DOT)
.append(name)
.toString();
}
// 生成具有限定符“.”的字符串
public static String[] qualify(String prefix, String[] names) {
if (prefix==null) return names;
int len = names.length;
String[] qualified = new String[len];
for ( int i=0; i<len; i++) {
qualified[i] = qualify( prefix, names[i] );
}
return qualified;
}
private StringHelper() { /* static methods only - hide constructor */ }
// 对求字符索引的一个扩展。求字符串string中包含的字符在sqlString中存在的最小位置
public static int firstIndexOfChar(String sqlString, String string, int startindex) {
int matchAt=-1; //此处 -1 可以理解为无穷大
for (int i = 0; i < string.length(); i++) {
int curMatch = sqlString.indexOf( string.charAt(i), startindex );
if (curMatch >=0) {
if (matchAt==-1) { // first time we find match!
matchAt = curMatch;
}
else {
matchAt = Math.min(matchAt, curMatch);
}
}
}
return matchAt;
}
// 取字符串的前面指定长度的字符串
public static String truncate(String string, int length) {
if (string.length()<=length) {
return string;
}
else {
return string.substring(0, length);
}
}
}
心得:
1、此类应该算是个算法类,所以有些地方的可读性真是不敢恭维;
2、不愧是“高级货”,程序的编写非常的规范,例如,所有的字符常量都定义常量来表示,各个方法也重构的很短。