Java tutorial
/* * Copyright 2007-2012 Jiemamy Project and the Others. * Created on 2008/06/09 * * This file is part of Jiemamy. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.jiemamy.utils; import java.util.Locale; import java.util.regex.Pattern; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.Validate; import org.apache.commons.lang.text.StrBuilder; /** * {@link String}? * * <p>????????????</p> * * <h3>???</h3> * <dl> * <dt>Java??</dt> * <dd>Java?????????????? * Java?????????ex.{@code AgileDatabase}</dd> * <dt>Java??</dt> * <dd>Java?????????????????? * Java?????????ex.{@code agileDatabase}</dd> * <dt>SQL??</dt> * <dd>Database?????????(_)??????? * SQL?????????ex.{@code AGILE_DATABASE}</dd> * </dl> * * @version $Id$ * @author daisuke * @author wencheng * @author yamkazu */ public final class JmStringUtil { private static final int REPLACE_BUFF_SIZE = 100; /** ??? */ public static final String[] EMPTY_STRINGS = new String[0]; /** Java????? */ private static final Pattern JAVA_CLASS_NAME_PATTERN = Pattern.compile("^[A-Z][a-zA-Z0-9]*$"); /** Java?????????? */ private static final Pattern JAVA_CLASS_NAME_IGNORE_PATTERN = Pattern.compile("^[A-Z]+$"); /** Java????? */ private static final Pattern JAVA_NAME_PATTERN = Pattern.compile("^[a-z][a-zA-Z0-9]*$"); /** SQL????? */ private static final Pattern SQL_NAME_PATTERN = Pattern.compile("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"); /** * {@link StrBuilder}??{@code i}16????? * * @param buf ? * @param i * @throws IllegalArgumentException {@code buf}?{@code null}??? */ public static void appendHex(StringBuilder buf, byte i) { Validate.notNull(buf); buf.append(Character.forDigit((i >> 4) & 0x0F, 16)); buf.append(Character.forDigit(i & 0x0F, 16)); // CHECKSTYLE IGNORE THIS LINE } /** * {@link StrBuilder}??{@code i}16????? * * @param buf ? * @param i * @throws IllegalArgumentException {@code buf}?{@code null}??? */ public static void appendHex(StringBuilder buf, int i) { Validate.notNull(buf); for (int shift = 24; shift >= 0; shift -= 8) { // CHECKSTYLE IGNORE THIS LINE String str = Integer.toHexString((i >> shift) & 0xFF); // CHECKSTYLE IGNORE THIS LINE if (str.length() == 1) { buf.append('0'); } buf.append(str); } } /** * SQL?????? * * <p>{@code prefix}?{@code _}????????{@code prefix}?{@code _}????</p> * * @param sqlName ?SQL?? * @param prefix ? * @return ???SQL??{@code sqlName}???{@code prefix}????{@code sqlName} */ public static String appendSqlPrefix(String sqlName, String prefix) { if (isEmpty(sqlName) || isEmpty(prefix)) { return sqlName; } return prefix.replaceFirst("[^_]$", "$0_").toUpperCase(Locale.getDefault()) + sqlName; } /** * ?1?? * * @param str * @return ?{@code str}????????? */ public static String capitalize(String str) { if (isEmpty(str)) { return str; } char[] ch = str.toCharArray(); ch[0] = Character.toUpperCase(ch[0]); return new String(ch); } /** * JavaBeans???????? * * <p>{@link #decapitalizeAsJavaBeans(String)}???2?????? * ????????{@link #capitalizeAsJavaBeans(String)}?? * ???????????1??</p> * * @param name ??? * @return ???{@code name}????????? */ public static String capitalizeAsJavaBeans(String name) { // ?????????? return capitalize(name); } /** * ???????????????? * * @param array ? * @param stringToFind ? * @return ?????{@code true}?????????{@code false} */ public static boolean containsIgnoreCase(String[] array, String stringToFind) { return indexOfIgnoreCase(array, stringToFind) != ArrayUtils.INDEX_NOT_FOUND; } /** * ?1??? * * @param str * @return ?{@code str}????????? */ public static String decapitalize(String str) { if (isEmpty(str)) { return str; } char[] ch = str.toCharArray(); ch[0] = Character.toLowerCase(ch[0]); return new String(ch); } /** * JavaBeans???????? * * <p>?2??????????????</p> * * @param name ??? * @return ???{@code name}????????? */ public static String decapitalizeAsJavaBeans(String name) { if (isEmpty(name)) { return name; } char[] chars = name.toCharArray(); if (chars.length >= 2 && Character.isUpperCase(chars[0]) && Character.isUpperCase(chars[1])) { return name; } chars[0] = Character.toLowerCase(chars[0]); return new String(chars); } /** * ????????????? * * @param target * @param suffix ? * @return ?????????{@code true}?????????{@code false} */ public static boolean endsWithIgnoreCase(String target, String suffix) { if (target == null || suffix == null) { return false; } int targetLength = target.length(); int suffixLength = suffix.length(); if (targetLength < suffixLength) { return false; } String s1 = target.substring(targetLength - suffixLength); return s1.equalsIgnoreCase(suffix); } /** * ?????????????{@code null}????{@code true}? * * @param target1 1 * @param target2 2 * @return ??????{@code true}?????????{@code false} */ public static boolean equals(String target1, String target2) { return (target1 == null) ? (target2 == null) : target1.equals(target2); } /** * ??????????????null????{@code true}??? * * @param target1 1 * @param target2 2 * @return ???????{@code true}?????????{@code false} */ public static boolean equalsIgnoreCase(String target1, String target2) { return (target1 == null) ? (target2 == null) : target1.equalsIgnoreCase(target2); } /** * ???????????? * * @param array ? * @param stringToFind ? * @return ? */ public static int indexOfIgnoreCase(String[] array, String stringToFind) { return indexOfIgnoreCase(array, stringToFind, 0); } /** * ???????????? * * <p>????????????????</p> * * @param array ? * @param stringToFind ? * @param startIndex * @return ?????????{@code -1} */ public static int indexOfIgnoreCase(String[] array, String stringToFind, int startIndex) { if (array == null) { return ArrayUtils.INDEX_NOT_FOUND; } if (startIndex < 0) { startIndex = 0; } if (stringToFind == null) { for (int i = startIndex; i < array.length; i++) { if (array[i] == null) { return i; } } } else { for (int i = startIndex; i < array.length; i++) { if (stringToFind.equalsIgnoreCase(array[i])) { return i; } } } return ArrayUtils.INDEX_NOT_FOUND; } /** * ?????? * * <p>???????????????????</p> * * <ul> * <li>{@code null}</li> * <li>{@code ""}</li> * <li>???????{@link Character#isWhitespace(char)}???</li> * </ul> * * @param str * @return ???{@code true}?????????{@code false} */ public static boolean isBlank(String str) { if (isEmpty(str)) { return true; } for (int i = 0; i < str.length(); i++) { if (Character.isWhitespace(str.charAt(i)) == false) { return false; } } return true; } /** * ?{@code null}????{@code true}? * * @param text * @return ?{@code null}??????{@code true}?????????{@code false} */ public static boolean isEmpty(String text) { return text == null || text.length() == 0; } /** * Java?????? * * <p>??{@code ^[A-Z][a-zA-Z0-9]*$}??????????</p> * * <p>??????????????{@code false}?</p> * * @param name ? * @return Java??????{@code true}?????????{@code false}{@code name}????{@code false} */ public static boolean isJavaClassName(String name) { if (isEmpty(name)) { return false; } // THINK ????????????? return JAVA_CLASS_NAME_PATTERN.matcher(name).matches() && JAVA_CLASS_NAME_IGNORE_PATTERN.matcher(name).matches() == false; } /** * Java?????? * * <p>??{@code ^[a-z][a-zA-Z0-9]*$}??????????</p> * * @param name ? * @return Java??????{@code true}?????????{@code false}{@code name}????{@code false} */ public static boolean isJavaName(String name) { if (isEmpty(name)) { return false; } // THINK ????????????? return JAVA_NAME_PATTERN.matcher(name).matches(); } /** * ????????? * * <p>{@link #isBlank(String)}???</p> * * @param str * @return ??????{@code true}?????????{@code false} * @see #isBlank(String) */ public static boolean isNotBlank(String str) { return isBlank(str) == false; } /** * ?{@code null}?????{@code true}? * * @param text * @return ?{@code null}??????{@code true}?????????{@code false} */ public static boolean isNotEmpty(String text) { return isEmpty(text) == false; } /** * ?{@code 0-9}???????????? * * @param s * @return ?????????{@code true}?????????{@code false} */ public static boolean isNumber(String s) { if (s == null || s.length() == 0) { return false; } int size = s.length(); for (int i = 0; i < size; i++) { char chr = s.charAt(i); if (chr < '0' || '9' < chr) { return false; } } return true; } /** * SQL?????? * * <p>??{@code ^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$}??????????</p> * * @param name ? * @return SQL??????{@code true}?????????{@code false}{@code name}????{@code false} */ public static boolean isSqlName(String name) { if (isEmpty(name)) { return false; } // THINK ????????????? return SQL_NAME_PATTERN.matcher(name).matches(); } /** * ?? * * @param text * @return ?? */ public static String ltrim(String text) { return ltrim(text, null); } /** * ???? * * @param text * @param trimText * @return ?? */ public static String ltrim(String text, String trimText) { if (text == null) { return null; } if (trimText == null) { trimText = " "; } int pos = 0; for (; pos < text.length(); pos++) { if (trimText.indexOf(text.charAt(pos)) < 0) { break; } } return text.substring(pos); } /** * SQL???? * * <p>{@code prefix}?{@code _}????????{@code prefix}?{@code _}????</p> * * @param sqlName ?SQL?? * @param prefix ? * @return ??SQL??{@code sqlName}???{@code prefix}???{@code sqlName} */ public static String removeSqlPrefix(String sqlName, String prefix) { if (isEmpty(sqlName) || isEmpty(prefix)) { return sqlName; } return sqlName.replaceFirst(prefix + "_?", ""); } /** * {@code text}???{@code fromText}?{@code toText}????? * * @param text * @param fromText ????? * @param toText ???? * @return ? */ public static String replace(String text, String fromText, String toText) { if (text == null || fromText == null || toText == null) { return text; } StringBuilder buf = new StringBuilder(REPLACE_BUFF_SIZE); int pos = 0; int pos2 = 0; while (true) { pos = text.indexOf(fromText, pos2); if (pos == 0) { buf.append(toText); pos2 = fromText.length(); } else if (pos > 0) { buf.append(text.substring(pos2, pos)); buf.append(toText); pos2 = pos + fromText.length(); } else { buf.append(text.substring(pos2)); break; } } return buf.toString(); } /** * ??? * * @param text * @return ?? */ public static String rtrim(String text) { return rtrim(text, null); } /** * ????? * * @param text * @param trimText * @return ?? */ public static String rtrim(String text, String trimText) { if (text == null) { return null; } if (trimText == null) { trimText = " "; } int pos = text.length() - 1; for (; pos >= 0; pos--) { if (trimText.indexOf(text.charAt(pos)) < 0) { break; } } return text.substring(0, pos + 1); } /** * ? * * @param str * @param delim ???? * @return ???? */ public static String[] split(String str, String delim) { if (isEmpty(str)) { return EMPTY_STRINGS; } if (isEmpty(delim)) { return new String[] { str }; } return str.split(delim); } /** * ??????????????? * * @param target * @param prefix ? * @return ???????????{@code true}?????????{@code false} */ public static boolean startsWithIgnoreCase(String target, String prefix) { if (target == null || prefix == null) { return false; } int targetLength = target.length(); int prefixLength = prefix.length(); if (targetLength < prefixLength) { return false; } String s1 = target.substring(0, prefix.length()); return s1.equalsIgnoreCase(prefix); } /** * ??????????? * * @param str * @param separator * @return ?? */ public static String substringFromLast(String str, String separator) { if (isEmpty(str) || isEmpty(separator)) { return str; } int pos = str.lastIndexOf(separator); if (pos == -1) { return str; } return str.substring(0, pos); } /** * ??????????? * * @param str * @param separator * @return ?? */ public static String substringToLast(String str, String separator) { if (isEmpty(str) || isEmpty(separator)) { return str; } int pos = str.lastIndexOf(separator); if (pos == -1) { return str; } return str.substring(pos + 1, str.length()); } /** * 16???? * * @param bytes ??? * @return 16? */ public static String toHex(byte[] bytes) { if (bytes == null) { return ""; } StringBuilder sb = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; ++i) { appendHex(sb, bytes[i]); } return sb.toString(); } /** * 16???? * * @param i int * @return 16? */ public static String toHex(int i) { StringBuilder buf = new StringBuilder(); appendHex(buf, i); return buf.toString(); } /** * SQL??????Java???Java???? * * <p>??SQL???Java???Java???????{@link String#toUpperCase(Locale)} * ???????SQL?????????</p> * * <p>Java?????????????????????? * ????????SQL??????????{@code FOO}????????SQL????? * ??{@code Foo}???</p> * * @param str SQL?????Java?? * @return Java???{@code str}????????? */ public static String toJavaClassName(String str) { if (isEmpty(str)) { return str; } return capitalize(toJavaName(str)); } /** * SQL??????Java???Java???? * * <p>??SQL???Java???Java???????{@link String#toUpperCase(Locale)} * ???????SQL?????????</p> * * <p>Java?????????????</p> * * @param str SQL?????Java?? * @return Java???{@code str}????????? */ public static String toJavaName(String str) { if (isEmpty(str)) { return str; } if (isJavaName(str)) { return str; } if (isJavaClassName(str)) { return decapitalizeAsJavaBeans(str); } String upper; if (isSqlName(str) == false) { upper = str.toUpperCase(Locale.getDefault()); } else { upper = str; } StringBuilder sb = new StringBuilder(upper.toLowerCase(Locale.getDefault())); for (int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); if (c == '_') { sb.deleteCharAt(i); sb.setCharAt(i, Character.toUpperCase(sb.charAt(i))); } } return sb.toString(); } /** * Java???Java???SLQ???? * * <p>SQL?????????????</p> * * @param str Java?????Java?? * @return SQL???{@code str}????????? */ public static String toSqlName(String str) { if (isEmpty(str)) { return str; } if (isSqlName(str)) { return str; } if ((isJavaClassName(str) || isJavaName(str)) == false) { return str.toUpperCase(Locale.getDefault()); } StringBuilder sb = new StringBuilder(str); for (int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); if (Character.isUpperCase(c) && i != 0) { sb.insert(i++, "_"); } else { sb.setCharAt(i, Character.toUpperCase(c)); } } return sb.toString(); } /** * ? * * <p>{@code text}?{@code prefix}?????????</p> * * @param text * @param prefix * @return ?? */ public static String trimPrefix(String text, String prefix) { if (text == null) { return null; } if (prefix == null) { return text; } if (text.startsWith(prefix)) { return text.substring(prefix.length()); } return text; } /** * * * <p>{@code text}?{@code suffix}?????????</p> * * @param text * @param suffix * @return ?? */ public static String trimSuffix(String text, String suffix) { if (text == null) { return null; } if (suffix == null) { return text; } if (text.endsWith(suffix)) { return text.substring(0, text.length() - suffix.length()); } return text; } private JmStringUtil() { } }