[Schmitzm-commits] r2215 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Fri Feb 1 10:32:45 CET 2013
Author: alfonx
Date: 2013-02-01 10:32:45 +0100 (Fri, 01 Feb 2013)
New Revision: 2215
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
StringBuilder statt StringBuffer
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2013-02-01 09:32:13 UTC (rev 2214)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2013-02-01 09:32:45 UTC (rev 2215)
@@ -199,46 +199,45 @@
return getSignumStr(value, "");
}
- /**
- * Replaces all umlaut occurrences (and 'ß') by 'ae', 'oe' or 'ue'.
- */
- public static String replaceUmlauts(String str) {
- if ( str == null )
- return null;
- boolean upcase = str.equals( str.toUpperCase() );
- str = str.replaceAll("ä", "ae");
- str = str.replaceAll("ö", "oe");
- str = str.replaceAll("ü", "ue");
- str = str.replaceAll("ß", "ss");
- str = str.replaceAll("Ä", upcase ? "AE" : "Ae");
- str = str.replaceAll("Ö", upcase ? "OE" : "Oe");
- str = str.replaceAll("Ü", upcase ? "UE" : "Ue");
- return str;
- }
+ /**
+ * Replaces all umlaut occurrences (and 'ß') by 'ae', 'oe' or 'ue'.
+ */
+ public static String replaceUmlauts(String str) {
+ if (str == null)
+ return null;
+ boolean upcase = str.equals(str.toUpperCase());
+ str = str.replaceAll("ä", "ae");
+ str = str.replaceAll("ö", "oe");
+ str = str.replaceAll("ü", "ue");
+ str = str.replaceAll("ß", "ss");
+ str = str.replaceAll("Ä", upcase ? "AE" : "Ae");
+ str = str.replaceAll("Ö", upcase ? "OE" : "Oe");
+ str = str.replaceAll("Ü", upcase ? "UE" : "Ue");
+ return str;
+ }
- /**
- * Replaces all 'ae', 'oe', 'ue' and 'ss' occurrences by umlauts.
- * Note: This method may not be very save, because grammer is not taken into account!
- * E.g. a word-splitting 'ae' is also replaced by 'ä' or 'ss' in 'Wasser' is replaced by
- * 'ß', although the result is not correctly spelled!
- */
- public static String createUmlauts(String str) {
- if ( str == null )
- return null;
- str = str.replaceAll("ae","ä");
- str = str.replaceAll("oe","ö");
- str = str.replaceAll("ue","ü");
- str = str.replaceAll("ss","ß");
- str = str.replaceAll("AE","Ä");
- str = str.replaceAll("OE","Ö");
- str = str.replaceAll("UE","Ü");
- str = str.replaceAll("Ae","Ä");
- str = str.replaceAll("Oe","Ö");
- str = str.replaceAll("Ue","Ü");
- return str;
- }
+ /**
+ * Replaces all 'ae', 'oe', 'ue' and 'ss' occurrences by umlauts. Note: This method may not be very save, because
+ * grammer is not taken into account! E.g. a word-splitting 'ae' is also replaced by 'ä' or 'ss' in 'Wasser' is
+ * replaced by 'ß', although the result is not correctly spelled!
+ */
+ public static String createUmlauts(String str) {
+ if (str == null)
+ return null;
+ str = str.replaceAll("ae", "ä");
+ str = str.replaceAll("oe", "ö");
+ str = str.replaceAll("ue", "ü");
+ str = str.replaceAll("ss", "ß");
+ str = str.replaceAll("AE", "Ä");
+ str = str.replaceAll("OE", "Ö");
+ str = str.replaceAll("UE", "Ü");
+ str = str.replaceAll("Ae", "Ä");
+ str = str.replaceAll("Oe", "Ö");
+ str = str.replaceAll("Ue", "Ü");
+ return str;
+ }
- /**
+ /**
* Returns "+" for positive values and "-" for negative values.
*
* @param value
@@ -688,18 +687,6 @@
* @return
*/
public static String listString(String delim, String... strList) {
- // StringBuffer resultStr = new StringBuffer();
- // for (int i=0; i<strList.length; i++) {
- // // ignore item if it it null or empty
- // if ( strList[i] == null || "".equals(strList[i].trim()) )
- // continue;
- // // append delimiter (if necessary)
- // if ( resultStr.length() > 0 && delim != null )
- // resultStr.append(delim);
- // // append string
- // resultStr.append(strList[i].trim());
- // }
- // return resultStr.toString();
return listObjects(delim, true, (Object[]) strList);
}
@@ -715,7 +702,10 @@
* @return
*/
public static String listObjects(String delim, boolean ignoreNulls, Object... objList) {
- StringBuffer resultStr = new StringBuffer();
+ // Änderung ST: 1.2.2013: Wenn keine Thread-Safety benötigt wird, dann ist StringBuilder die schnellere
+ // Alternative zu StringBuffer. http://schabby.de/stringbuilder-stringbuffer/
+ final StringBuilder resultStr = new StringBuilder();
+
for (int i = 0; i < objList.length; i++) {
// ignore item if it is null or empty
if (ignoreNulls
@@ -808,7 +798,10 @@
* aneinanderzuhaengende Objekte
*/
public static String stringConcat(Object... str) {
- StringBuffer sb = new StringBuffer();
+
+ // Änderung ST: 1.2.2013: Wenn keine Thread-Safety benötigt wird, dann ist StringBuilder die schnellere
+ // Alternative zu StringBuffer. http://schabby.de/stringbuilder-stringbuffer/
+ StringBuilder sb = new StringBuilder();
for (int i = 0; str != null && i < str.length; i++)
if (str[i] != null)
sb.append(str[i]);
@@ -899,16 +892,6 @@
* aneinanderzuhaengende Stings
*/
public static String stringConcatWithSep(String sep, String maskChar, Object... str) {
- // StringBuffer sb = new StringBuffer();
- // for (int i = 0; str != null && i < str.length; i++) {
- // if (i > 0)
- // sb.append(sep);
- // if (maskChar != null)
- // sb.append(maskChar).append(str[i]).append(maskChar);
- // else
- // sb.append(str[i]);
- // }
- // return sb.toString();
return stringConcatWithSep(sep, maskChar, false, str);
}
@@ -928,8 +911,12 @@
* aneinanderzuhaengende Stings
*/
public static String stringConcatWithSep(String sep, String maskChar, boolean ignoreNulls, Object... str) {
- StringBuffer sb = new StringBuffer();
+ // Änderung ST: 1.2.2013: Wenn keine Thread-Safety benötigt wird, dann ist StringBuilder die schnellere
+ // Alternative zu StringBuffer. http://schabby.de/stringbuilder-stringbuffer/
+ // StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
+
// wenn statt einem Object-Array ein nativer Array angegeben wird,
// wird dieser nur als ein Parameter behandelt
// -> In Objekt-Liste umkopieren
@@ -2047,8 +2034,7 @@
* @param stackTrace
* stack trace to explore
* @param depth
- * maximum count of classes, which are explored in the stack trace (-1 for complete
- * stack trace)
+ * maximum count of classes, which are explored in the stack trace (-1 for complete stack trace)
*/
public static String[] getLastStackTraceClasses(StackTraceElement[] stackTrace, int depth) {
Vector<String> lastClasses = new Vector<String>();
@@ -2683,105 +2669,115 @@
return string;
}
- /**
- * Sorts a list according to a {@link Comparator}.
- * @param sourceList list to sort
- * @param comparator comparator used for sort
- * @param destList list to store the sorted data in (if {@code null} the
- * source list will be changed!)
- */
- public static <T> List<T> sortList(List<T> sourceList, Comparator<T> comparator, List<T> destList) {
- if ( destList == null )
- destList = sourceList;
- // Sort items in TreeSet
- TreeSet<T> resultSorted = new TreeSet<T>(comparator);
- resultSorted.addAll(sourceList);
- // Put items (now sorted) in destination list
- if (destList == sourceList)
- destList.clear();
- destList.addAll(resultSorted);
-
- return destList;
- }
+ /**
+ * Sorts a list according to a {@link Comparator}.
+ *
+ * @param sourceList
+ * list to sort
+ * @param comparator
+ * comparator used for sort
+ * @param destList
+ * list to store the sorted data in (if {@code null} the source list will be changed!)
+ */
+ public static <T> List<T> sortList(List<T> sourceList, Comparator<T> comparator, List<T> destList) {
+ if (destList == null)
+ destList = sourceList;
+ // Sort items in TreeSet
+ TreeSet<T> resultSorted = new TreeSet<T>(comparator);
+ resultSorted.addAll(sourceList);
+ // Put items (now sorted) in destination list
+ if (destList == sourceList)
+ destList.clear();
+ destList.addAll(resultSorted);
- /**
- * Sorts a list according to a {@link Comparator}.
- * @param sourceList list to sort
- * @param comparator comparator used for sort
- */
- public static <T> void sortList(List<T> sourceList, Comparator<T> comparator) {
- sortList(sourceList,comparator,null);
- }
-
- /**
- * Converts a string to camel-case, so that each word-starting letter
- * is in upper-case and any other latters in lower-case.
- * @param str string to convert
- * @param inverted indicates whether camel-case is reverted (word-starting letter in
- * upper-case, other letters in lower-case.
- */
- public static String toCamelCase(String str, boolean inverted) {
- Matcher m = Pattern.compile("(^|\\s+)(\\w)").matcher(str);
- StringBuilder sb = new StringBuilder();
- int last = 0;
- while (m.find()) {
- String rest = str.substring(last, m.start());
- rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
- sb.append(rest); // rest upto first/next occurence
- sb.append(m.group(1)); // white spaces
- String upperCase = m.group(2); // part to convert in camel
- upperCase = inverted ? upperCase.toLowerCase() : upperCase.toUpperCase();
- sb.append(upperCase);
- last = m.end();
- }
- // rest upto end of string
- String rest = str.substring(last);
- rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
- sb.append(rest);
-
- return sb.toString();
- }
+ return destList;
+ }
-
- /**
- * Checks the command line parameters for one of the following keys and
- * sets the default {@link Locale}. Possible parameter keys: {@code -l},
- * {@code -L}, {@code /l}, {@code /L}
- * @param args command line arguments
- * @return array index the language parameter was found in; -1 if none of the possible
- * parameter keys was found
- */
- public static int setDefaultLocaleFromCommandLineArguments(String[] args) {
- return setDefaultLocaleFromCommandLineArguments(args, false, "-l", "/l");
- }
-
- /**
- * Checks the command line parameters for a language parameter and
- * sets the default {@link Locale}.
- * @param args command line arguments
- * @param caseSensitive indicates whether the parameter must be specified exactly or
- * case-insensitive
- * @param paramKeys possible parameter keys for language code (e.g. "-l", "/l", "-language", ...)
- * followed by a valid ISO-639 code
- * @return array index the language parameter was found in; -1 if none of the possible
- * parameter keys was found
- */
- public static int setDefaultLocaleFromCommandLineArguments(String[] args, boolean caseSensitive, String... paramKeys) {
- for (int i=0; i<args.length; i++) {
- // Angabe der Sprache
- // -l <language>
- for (String paramKey : paramKeys) {
- if ( caseSensitive && args[i].equals(paramKey) ||
- !caseSensitive && args[i].equalsIgnoreCase(paramKey) ) {
- try {
- Locale.setDefault( new Locale(args[i+1]) );
- return i;
- } catch (Exception err) {
- System.err.println("Invalid Argument: After "+args[i]+" a valid ISO-639 code is expected");
- }
- }
- }
- }
- return -1;
- }
+ /**
+ * Sorts a list according to a {@link Comparator}.
+ *
+ * @param sourceList
+ * list to sort
+ * @param comparator
+ * comparator used for sort
+ */
+ public static <T> void sortList(List<T> sourceList, Comparator<T> comparator) {
+ sortList(sourceList, comparator, null);
+ }
+
+ /**
+ * Converts a string to camel-case, so that each word-starting letter is in upper-case and any other latters in
+ * lower-case.
+ *
+ * @param str
+ * string to convert
+ * @param inverted
+ * indicates whether camel-case is reverted (word-starting letter in upper-case, other letters in
+ * lower-case.
+ */
+ public static String toCamelCase(String str, boolean inverted) {
+ Matcher m = Pattern.compile("(^|\\s+)(\\w)").matcher(str);
+ StringBuilder sb = new StringBuilder();
+ int last = 0;
+ while (m.find()) {
+ String rest = str.substring(last, m.start());
+ rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
+ sb.append(rest); // rest upto first/next occurence
+ sb.append(m.group(1)); // white spaces
+ String upperCase = m.group(2); // part to convert in camel
+ upperCase = inverted ? upperCase.toLowerCase() : upperCase.toUpperCase();
+ sb.append(upperCase);
+ last = m.end();
+ }
+ // rest upto end of string
+ String rest = str.substring(last);
+ rest = inverted ? rest.toUpperCase() : rest.toLowerCase();
+ sb.append(rest);
+
+ return sb.toString();
+ }
+
+ /**
+ * Checks the command line parameters for one of the following keys and sets the default {@link Locale}. Possible
+ * parameter keys: {@code -l}, {@code -L}, {@code /l}, {@code /L}
+ *
+ * @param args
+ * command line arguments
+ * @return array index the language parameter was found in; -1 if none of the possible parameter keys was found
+ */
+ public static int setDefaultLocaleFromCommandLineArguments(String[] args) {
+ return setDefaultLocaleFromCommandLineArguments(args, false, "-l", "/l");
+ }
+
+ /**
+ * Checks the command line parameters for a language parameter and sets the default {@link Locale}.
+ *
+ * @param args
+ * command line arguments
+ * @param caseSensitive
+ * indicates whether the parameter must be specified exactly or case-insensitive
+ * @param paramKeys
+ * possible parameter keys for language code (e.g. "-l", "/l", "-language", ...) followed by a valid
+ * ISO-639 code
+ * @return array index the language parameter was found in; -1 if none of the possible parameter keys was found
+ */
+ public static int setDefaultLocaleFromCommandLineArguments(String[] args, boolean caseSensitive,
+ String... paramKeys) {
+ for (int i = 0; i < args.length; i++) {
+ // Angabe der Sprache
+ // -l <language>
+ for (String paramKey : paramKeys) {
+ if (caseSensitive && args[i].equals(paramKey) ||
+ !caseSensitive && args[i].equalsIgnoreCase(paramKey)) {
+ try {
+ Locale.setDefault(new Locale(args[i + 1]));
+ return i;
+ } catch (Exception err) {
+ System.err.println("Invalid Argument: After " + args[i] + " a valid ISO-639 code is expected");
+ }
+ }
+ }
+ }
+ return -1;
+ }
}
More information about the Schmitzm-commits
mailing list