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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Feb 25 06:52:57 CET 2013


Author: alfonx
Date: 2013-02-25 06:52:57 +0100 (Mon, 25 Feb 2013)
New Revision: 2257

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
Wie sch?\195?\182n, dass es JUnit-Tests gibt...

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-24 23:45:54 UTC (rev 2256)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-02-25 05:52:57 UTC (rev 2257)
@@ -88,13 +88,14 @@
 public class LangUtil {
 	private static NumberFormat DEFAULT_DELAY_FORMAT = new DecimalFormat("0.00");
 
-//	/**
-//	 * If {@code true} {@link #removeWhitespacesToEmpty(String)} (which uses {@link RegexCache}) is called in
-//	 * {@link #parseStringDefaultNull(String, boolean)}. To avoid this behavior for an application, this flag can be set
-//	 * to {@code false}.<br>
-//	 * Default: {@code true}
-//	 */
-//	public static boolean REMOVE_WHITESPACES_ON_PARSE_STRING = true;
+	// /**
+	// * If {@code true} {@link #removeWhitespacesToEmpty(String)} (which uses {@link RegexCache}) is called in
+	// * {@link #parseStringDefaultNull(String, boolean)}. To avoid this behavior for an application, this flag can be
+	// set
+	// * to {@code false}.<br>
+	// * Default: {@code true}
+	// */
+	// public static boolean REMOVE_WHITESPACES_ON_PARSE_STRING = true;
 
 	/** Eine Sekunde in Millisekunden. */
 	public static final long SEC_MILLIS = 1000;
@@ -169,23 +170,24 @@
 		return string.trim();
 	}
 
-//	/**
-//	 * Checks whether {@link RegexCache} is available. In some cases the "Apache Lucene" library is excluded because of
-//	 * application size issues ("FatJar"). This method checks this case and sets the
-//	 * {@link #REMOVE_WHITESPACES_ON_PARSE_STRING} flag. This method can be called at the beginning of an application.
-//	 */
-//	public static boolean checkRegexCacheAvailable() {
-////		try {
-////			RegexCache.getInstance();
-////			REMOVE_WHITESPACES_ON_PARSE_STRING = true;
-////			return true;
-////		} catch (Throwable e) {
-////			// RegexCache can not be used
-//			REMOVE_WHITESPACES_ON_PARSE_STRING = false;
-//			return false;
-////		}
-//
-//	}
+	// /**
+	// * Checks whether {@link RegexCache} is available. In some cases the "Apache Lucene" library is excluded because
+	// of
+	// * application size issues ("FatJar"). This method checks this case and sets the
+	// * {@link #REMOVE_WHITESPACES_ON_PARSE_STRING} flag. This method can be called at the beginning of an application.
+	// */
+	// public static boolean checkRegexCacheAvailable() {
+	// // try {
+	// // RegexCache.getInstance();
+	// // REMOVE_WHITESPACES_ON_PARSE_STRING = true;
+	// // return true;
+	// // } catch (Throwable e) {
+	// // // RegexCache can not be used
+	// REMOVE_WHITESPACES_ON_PARSE_STRING = false;
+	// return false;
+	// // }
+	//
+	// }
 
 	/**
 	 * Returns "+" for positive values and "-" for negative values.
@@ -704,7 +706,7 @@
 		// Ä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
@@ -797,7 +799,7 @@
 	 *            aneinanderzuhaengende Objekte
 	 */
 	public static String stringConcat(Object... str) {
-		
+
 		// Ä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();
@@ -1571,33 +1573,33 @@
 		}
 	}
 
-    /**
-     * Instantiates an object by {@link Class#forName(String)}. The class must be present in runtime classpath.
-     * 
-     * @param className
-     *            full name of class (incl. package)
-     * @param constrArg
-     *            arguments for the constructor
-     * @exception UnsupportedOperationException
-     *                if class could be loaded, but no matching constructor can be found
-     */
-    public static <E> E instantiateObject(Class<E> clazz, Object... constrArg) {
-        try {
-            for (Constructor constructor : clazz.getConstructors())
-                try {
-                    return (E)constructor.newInstance(constrArg);
-                } catch (IllegalArgumentException err) {
-                }
-            throw new UnsupportedOperationException("No matching constructor found...");
-        } catch (Exception err) {
-            // LOGGER.error("Unexpected error when trying to instantiate "+className,err);
-            // do not fall back, because in general the
-            // the class is supported (class present)!
-            throw new RuntimeException(err);
-        }
-    }
+	/**
+	 * Instantiates an object by {@link Class#forName(String)}. The class must be present in runtime classpath.
+	 * 
+	 * @param className
+	 *            full name of class (incl. package)
+	 * @param constrArg
+	 *            arguments for the constructor
+	 * @exception UnsupportedOperationException
+	 *                if class could be loaded, but no matching constructor can be found
+	 */
+	public static <E> E instantiateObject(Class<E> clazz, Object... constrArg) {
+		try {
+			for (Constructor constructor : clazz.getConstructors())
+				try {
+					return (E) constructor.newInstance(constrArg);
+				} catch (IllegalArgumentException err) {
+				}
+			throw new UnsupportedOperationException("No matching constructor found...");
+		} catch (Exception err) {
+			// LOGGER.error("Unexpected error when trying to instantiate "+className,err);
+			// do not fall back, because in general the
+			// the class is supported (class present)!
+			throw new RuntimeException(err);
+		}
+	}
 
-    /**
+	/**
 	 * Ruft die Java Garbage Collection auf.
 	 * 
 	 * @return Anzahl an Bytes, die durch den Aufruf der Garbage Collection freigegeben wurden
@@ -1915,19 +1917,19 @@
 		return max(0, collections);
 	}
 
-    /**
-     * Returns the maximum value of some double values
-     */
-    public static double max(double... values) {
-      if ( values.length == 0 )
-        return 0;
-      double maxValue = values[0];
-      for (double v : values)
-        maxValue = Math.max(v, maxValue);
-      return maxValue;
-    }
+	/**
+	 * Returns the maximum value of some double values
+	 */
+	public static double max(double... values) {
+		if (values.length == 0)
+			return 0;
+		double maxValue = values[0];
+		for (double v : values)
+			maxValue = Math.max(v, maxValue);
+		return maxValue;
+	}
 
-    /**
+	/**
 	 * Returns the minimum value of some integer collections.
 	 * 
 	 * @param defMinValue
@@ -1956,43 +1958,49 @@
 		return min(0, collections);
 	}
 
-    /**
-     * Returns the minimum value of some double values
-     */
-    public static double min(double... values) {
-      if ( values.length == 0 )
-        return 0;
-      double minValue = values[0];
-      for (double v : values)
-        minValue = Math.min(v, minValue);
-      return minValue;
-    }
-	
 	/**
+	 * Returns the minimum value of some double values
+	 */
+	public static double min(double... values) {
+		if (values.length == 0)
+			return 0;
+		double minValue = values[0];
+		for (double v : values)
+			minValue = Math.min(v, minValue);
+		return minValue;
+	}
+
+	/**
 	 * Determines the number of digits of a value.
-	 * @param value a decimal value
-	 * @param maxDigits maximum number of digits to test (to avoid infinite test!)
-	 * @return {@code maxDigits + 1} if value has more digits 
+	 * 
+	 * @param value
+	 *            a decimal value
+	 * @param maxDigits
+	 *            maximum number of digits to test (to avoid infinite test!)
+	 * @return {@code maxDigits + 1} if value has more digits
 	 */
 	public static int getDigits(double value, int maxDigits) {
-	  for (int dig=0; dig<=maxDigits; dig++) {
-	    if ( value == round(value, dig) )
-	      return dig;
-	  }
-	  return maxDigits + 1;
+		for (int dig = 0; dig <= maxDigits; dig++) {
+			if (value == round(value, dig))
+				return dig;
+		}
+		return maxDigits + 1;
 	}
 
 	/**
-     * Determines the number of digits of a value and returns a corrisponding initialized
-     * double value (0.1, 0.01, 0.001, etc.).
-     * @param value a decimal value
-     * @param maxDigits maximum number of digits to test (to avoid infinite test!)
-     * @see #getDigits(double, int) 
-     */
-    public static double getDigitValue(double value, int maxDigits) {
-      int digits = getDigits(value, maxDigits);
-      return Math.pow(10, -digits);
-    }
+	 * Determines the number of digits of a value and returns a corrisponding initialized double value (0.1, 0.01,
+	 * 0.001, etc.).
+	 * 
+	 * @param value
+	 *            a decimal value
+	 * @param maxDigits
+	 *            maximum number of digits to test (to avoid infinite test!)
+	 * @see #getDigits(double, int)
+	 */
+	public static double getDigitValue(double value, int maxDigits) {
+		int digits = getDigits(value, maxDigits);
+		return Math.pow(10, -digits);
+	}
 
 	/**
 	 * Rundet einen Wert auf Nach- oder Vorkommastellen.
@@ -2360,12 +2368,8 @@
 	public static Boolean parseStringDefaultNull(String str, boolean lax) {
 		if (str == null)
 			return null;
-		// String trimmedLowcase = removeWhitespacesToEmpty(str.toLowerCase())
-		// .trim();
 		String trimmedLowcase = str.toLowerCase();
-//		if (REMOVE_WHITESPACES_ON_PARSE_STRING)
-			trimmedLowcase = removeWhitespacesToEmpty(trimmedLowcase);
-//		trimmedLowcase = trimmedLowcase.trim();
+		trimmedLowcase = removeWhitespacesToEmpty(trimmedLowcase);
 
 		if (trimmedLowcase.startsWith("no"))
 			return false;
@@ -2490,20 +2494,25 @@
 	}
 
 	/**
-	 * Alle Zeilenumbrüche raus // Geschützte Leerzeichen weg // Tabulator -> Leer
+	 * Alle Zeilenumbrüche, geschützte Leerzeichen, Tabulatoren
 	 */
 	final static String REMOVE_WHITESPACES_PATTERN_R = "[\\u0000|\\n|\\t|\\r|\\u0085|\\u2028|\\u2029|\\u00A0]";
-	// final static Pattern REMOVE_WHITESPACES_PATTERN =
-	// RegexCache.getInstance().getPattern(REMOVE_WHITESPACES_PATTERN_R);
 	/**
+	 * Alle Zeilenumbrüche, geschützte Leerzeichen, Tabulatoren. (Instances of Pattern class are immutable and are safe
+	 * for use by multiple concurrent threads. Instances of the {@link Matcher} class are not safe for such use.)
+	 */
+	final static Pattern REMOVE_WHITESPACES_PATTERN = Pattern.compile(REMOVE_WHITESPACES_PATTERN_R);
+
+	/**
 	 * Leerzeichen konsolidieren
 	 */
 	final static String REMOVE_WHITESPACES_PATTERN2_R = "\\s{2,}";
+	/**
+	 * Leerzeichen konsolidieren. (Instances of Pattern class are immutable and are safe for use by multiple concurrent
+	 * threads. Instances of the {@link Matcher} class are not safe for such use.)
+	 */
+	final static Pattern REMOVE_WHITESPACES_PATTERN2 = Pattern.compile(REMOVE_WHITESPACES_PATTERN2_R);
 
-	// final static Pattern REMOVE_WHITESPACES_PATTERN2 =
-	// RegexCache.getInstance().getPattern(
-	// REMOVE_WHITESPACES_PATTERN2_R);
-
 	/**
 	 * Fasst Zeilenumbrüche und sonstige Leerräume zu einem einizgen Leerzeichen zusammen.
 	 * 
@@ -2513,14 +2522,14 @@
 		if (string == null)
 			return "";
 
-//		final Matcher matcher = RegexCache.getInstance().getMatcher(REMOVE_WHITESPACES_PATTERN_R, string);
-		final Matcher matcher = Pattern.compile(REMOVE_WHITESPACES_PATTERN_R).matcher(string);
+		// final Matcher matcher = RegexCache.getInstance().getMatcher(REMOVE_WHITESPACES_PATTERN_R, string);
+		final Matcher matcher = REMOVE_WHITESPACES_PATTERN.matcher(string);
 
 		string = matcher.replaceAll(" ");
 
 		// Leerraum konsolidieren
 		final String result = Pattern.compile(REMOVE_WHITESPACES_PATTERN2_R).matcher(string)
-				.replaceAll(" ");
+				.replaceAll(" ").trim();
 
 		return result;
 	}



More information about the Schmitzm-commits mailing list