[Schmitzm-commits] r1667 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Aug 8 16:21:36 CEST 2011


Author: alfonx
Date: 2011-08-08 16:21:35 +0200 (Mon, 08 Aug 2011)
New Revision: 1667

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
Parse String to Boolean mehtoden verbessert

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-08-04 22:56:17 UTC (rev 1666)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-08-08 14:21:35 UTC (rev 1667)
@@ -257,20 +257,20 @@
 	 *            date formats to try
 	 */
 	public static Date parseDate(String dateStr, DateFormat... formats) {
-	  try {
-		if (dateStr == null || dateStr.trim().equals(""))
-			return null;
-		dateStr = dateStr.trim();
-		for (DateFormat format : formats)
-			try {
-				Date date = format.parse(dateStr);
-				return date;
-			} catch (Exception err) {
-			}
-		throw new ParseException("Could not parse date: " + dateStr, 0);
-	  } catch ( ParseException err) {
-	    throw new UnsupportedOperationException(err);
-	  }
+		try {
+			if (dateStr == null || dateStr.trim().equals(""))
+				return null;
+			dateStr = dateStr.trim();
+			for (DateFormat format : formats)
+				try {
+					Date date = format.parse(dateStr);
+					return date;
+				} catch (Exception err) {
+				}
+			throw new ParseException("Could not parse date: " + dateStr, 0);
+		} catch (ParseException err) {
+			throw new UnsupportedOperationException(err);
+		}
 	}
 
 	/**
@@ -1763,14 +1763,8 @@
 	 * Interpretiert einen {@link String} zu einem {@link boolean} mit default <code>true</code>.
 	 */
 	public static boolean parseStringDefaultTrue(String str) {
-		if (str == null)
-			return true;
-		String trimmedLowcase = str.toLowerCase().trim();
-		if (trimmedLowcase.startsWith("n"))
-			return false;
-		if (trimmedLowcase.startsWith("0"))
-			return false;
-		return true;
+		Boolean b = parseStringDefaultNull(str);
+		return b != null ? b : true;
 	}
 
 	/**
@@ -1779,7 +1773,7 @@
 	public static Boolean parseStringDefaultNull(String str) {
 		if (str == null)
 			return null;
-		String trimmedLowcase = str.toLowerCase().trim();
+		String trimmedLowcase = removeWhitespacesToEmpty(str.toLowerCase());
 		if (trimmedLowcase.startsWith("n"))
 			return false;
 		if (trimmedLowcase.startsWith("0"))
@@ -1790,6 +1784,9 @@
 			return true;
 		if (trimmedLowcase.startsWith("1"))
 			return true;
+		if (trimmedLowcase.startsWith("true"))
+			return true;
+		// Default
 		return null;
 	}
 
@@ -1797,16 +1794,8 @@
 	 * Interpretiert einen {@link String} zu einem {@link boolean} mit default <code>false</code>.
 	 */
 	public static boolean parseStringDefaultFalse(String str) {
-		if (str == null)
-			return false;
-		String trimmedLowcase = str.toLowerCase().trim();
-		if (trimmedLowcase.startsWith("y"))
-			return true;
-		if (trimmedLowcase.startsWith("j"))
-			return true;
-		if (trimmedLowcase.startsWith("1"))
-			return true;
-		return false;
+		Boolean b = parseStringDefaultNull(str);
+		return b != null ? b : false;
 	}
 
 	/**
@@ -1920,25 +1909,25 @@
 	 * 
 	 * @author gregor
 	 */
-    public static String removeWhitespacesToEmpty(String string) {
-        if (string == null)
-                return "";
+	public static String removeWhitespacesToEmpty(String string) {
+		if (string == null)
+			return "";
 
-        // Alle Zeilenumbrüche raus
-        Matcher matcher = RegexCache.getInstance().getPattern("[\\n|\\r|\\u0085|\\u2028|\\u2029]").matcher(string);
-        string = matcher.replaceAll(" ");
-        
-        // Geschützte Leerzeichen weg
-        matcher = RegexCache.getInstance().getPattern("\\u00A0").matcher(string);
-        string = matcher.replaceAll(" ");
+		// Alle Zeilenumbrüche raus
+		Matcher matcher = RegexCache.getInstance().getPattern("[\\n|\\r|\\u0085|\\u2028|\\u2029]").matcher(string);
+		string = matcher.replaceAll(" ");
 
-        // Tabulator -> Leer
-        matcher = RegexCache.getInstance().getPattern("\\t").matcher(string);
-        string = matcher.replaceAll(" ");
-        
-        // Leerraum konsolidieren
-        matcher = RegexCache.getInstance().getPattern("\\s{2,}").matcher(string);
-        return matcher.replaceAll(" ");
-}
+		// Geschützte Leerzeichen weg
+		matcher = RegexCache.getInstance().getPattern("\\u00A0").matcher(string);
+		string = matcher.replaceAll(" ");
 
+		// Tabulator -> Leer
+		matcher = RegexCache.getInstance().getPattern("\\t").matcher(string);
+		string = matcher.replaceAll(" ");
+
+		// Leerraum konsolidieren
+		matcher = RegexCache.getInstance().getPattern("\\s{2,}").matcher(string);
+		return matcher.replaceAll(" ");
+	}
+
 }



More information about the Schmitzm-commits mailing list