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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 29 02:17:00 CET 2012


Author: alfonx
Date: 2012-02-29 02:17:00 +0100 (Wed, 29 Feb 2012)
New Revision: 1877

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-02-29 01:14:37 UTC (rev 1876)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-02-29 01:17:00 UTC (rev 1877)
@@ -90,10 +90,10 @@
 	public static final long DAY_MILLIS = 24 * HOUR_MILLIS;
 	/** Eine Woche in Millisekunden. */
 	public static final long WEEK_MILLIS = 7 * DAY_MILLIS;
-	
+
 	/** Ein Kilobyte (KB) in Bytes */
 	public static final long KB_BYTES = 1024;
-    /** Ein Megabyte (MB) in Bytes */
+	/** Ein Megabyte (MB) in Bytes */
 	public static final long MB_BYTES = 1024 * KB_BYTES;
 
 	final static Pattern onlyZerosRegEx = Pattern.compile("^([\\s0.,]*)$");
@@ -105,7 +105,7 @@
 
 	/** Format for 2-digit hour information with leading zero. */
 	public static final NumberFormat HOUR_FORMAT = new DecimalFormat("00");
-	
+
 	/**
 	 * Liefert <code>true</code> wenn der String nur Nullen, Leerzeichen, Kommata und/oder Punkte enthält.
 	 */
@@ -145,83 +145,92 @@
 			return "";
 		return string.trim();
 	}
-	
-	   /**
-     * Returns "+" for positive values and "-" for negative values. 
-     * @param value a value
-     * @return empty string if value is {@code null}, {@code NaN} or {@code 0.0}
-     */
-    public static String getSignumStr(Number value) {
-      return getSignumStr(value, "");
-    }
-	
+
 	/**
-	 * Returns "+" for positive values and "-" for negative values. 
-	 * @param value a value
-	 * @param zeroSign sign to show for 0.0
-	 * @return empty string if value is {@code null} or {@code NaN} 
+	 * Returns "+" for positive values and "-" for negative values.
+	 * 
+	 * @param value
+	 *            a value
+	 * @return empty string if value is {@code null}, {@code NaN} or {@code 0.0}
 	 */
+	public static String getSignumStr(Number value) {
+		return getSignumStr(value, "");
+	}
+
+	/**
+	 * Returns "+" for positive values and "-" for negative values.
+	 * 
+	 * @param value
+	 *            a value
+	 * @param zeroSign
+	 *            sign to show for 0.0
+	 * @return empty string if value is {@code null} or {@code NaN}
+	 */
 	public static String getSignumStr(Number value, String zeroSign) {
-	  if ( value == null || Double.isNaN(value.doubleValue()) )
-	    return "";
-	  double v = value.doubleValue();
-	  if ( v == 0.0 )
-	    return zeroSign;
-	  return v < 0 ? "-" : "+";
+		if (value == null || Double.isNaN(value.doubleValue()))
+			return "";
+		double v = value.doubleValue();
+		if (v == 0.0)
+			return zeroSign;
+		return v < 0 ? "-" : "+";
 	}
-	
-    /**
-     * Returns a timezone description including difference to GMT for the default time zone.
-     * @param style    style for time zone description ({@link TimeZone#LONG} or 
-     *                 {@link TimeZone#SHORT})
-     * @see TimeZone#getDisplayName(boolean, int, Locale)
-     */
-    public static String getTimezoneDescription(int style) {
-      return getTimezoneDescription(null, style);
-    }
 
-    /**
-     * Returns a timezone description including difference to GMT for the default time zone.
-     * @param style    style for time zone description ({@link TimeZone#LONG} or 
-     *                 {@link TimeZone#SHORT})
-     * @see TimeZone#getDisplayName(boolean, int, Locale)
-     */
-    public static String getTimezoneDescription(TimeZone timezone, int style) {
-      return getTimezoneDescription(null, null, style);
-    }
+	/**
+	 * Returns a timezone description including difference to GMT for the default time zone.
+	 * 
+	 * @param style
+	 *            style for time zone description ({@link TimeZone#LONG} or {@link TimeZone#SHORT})
+	 * @see TimeZone#getDisplayName(boolean, int, Locale)
+	 */
+	public static String getTimezoneDescription(int style) {
+		return getTimezoneDescription(null, style);
+	}
 
-    /**
+	/**
+	 * Returns a timezone description including difference to GMT for the default time zone.
+	 * 
+	 * @param style
+	 *            style for time zone description ({@link TimeZone#LONG} or {@link TimeZone#SHORT})
+	 * @see TimeZone#getDisplayName(boolean, int, Locale)
+	 */
+	public static String getTimezoneDescription(TimeZone timezone, int style) {
+		return getTimezoneDescription(null, null, style);
+	}
+
+	/**
 	 * Returns a timezone description, including difference to GMT.
-	 * @param timezone a time zone (if {@code null} the default timezone is used)
-	 * @param date     special date to return the timezone for (daylight saving in GMT offset)
-	 * @param style    style for time zone description ({@link TimeZone#LONG} or 
-	 *                 {@link TimeZone#SHORT})
+	 * 
+	 * @param timezone
+	 *            a time zone (if {@code null} the default timezone is used)
+	 * @param date
+	 *            special date to return the timezone for (daylight saving in GMT offset)
+	 * @param style
+	 *            style for time zone description ({@link TimeZone#LONG} or {@link TimeZone#SHORT})
 	 * @see TimeZone#getDisplayName(boolean, int, Locale)
 	 */
 	public static String getTimezoneDescription(TimeZone timezone, Date date, int style) {
-	  if ( timezone == null )
-	    timezone = TimeZone.getDefault();
-	  if ( date == null )
-	    date = new Date();
+		if (timezone == null)
+			timezone = TimeZone.getDefault();
+		if (date == null)
+			date = new Date();
 
-	  // check whether daylight saving time must taken into account
-	  boolean inclDST = timezone.inDaylightTime(date);
-	  
-	  // construct description
-	  String timezoneStr = timezone.getDisplayName(inclDST,style);
-	  // for long description, show short term too
-	  if ( style == TimeZone.LONG )
-	    timezoneStr += " / " + timezone.getDisplayName(inclDST, TimeZone.SHORT);
+		// check whether daylight saving time must taken into account
+		boolean inclDST = timezone.inDaylightTime(date);
 
-	  // calculate offset to GMT
-	  long   zoneOffsetHours = timezone.getOffset(date.getTime())/LangUtil.HOUR_MILLIS;
-      String zoneOffsetStr = "GMT"
-          + LangUtil.getSignumStr(zoneOffsetHours,"+")
-          + HOUR_FORMAT.format(Math.abs(zoneOffsetHours));
-      
-      timezoneStr += " (" + zoneOffsetStr + ")";
+		// construct description
+		String timezoneStr = timezone.getDisplayName(inclDST, style);
+		// for long description, show short term too
+		if (style == TimeZone.LONG)
+			timezoneStr += " / " + timezone.getDisplayName(inclDST, TimeZone.SHORT);
 
-      return  timezoneStr;
+		// calculate offset to GMT
+		long zoneOffsetHours = timezone.getOffset(date.getTime()) / LangUtil.HOUR_MILLIS;
+		String zoneOffsetStr = "GMT" + LangUtil.getSignumStr(zoneOffsetHours, "+")
+				+ HOUR_FORMAT.format(Math.abs(zoneOffsetHours));
+
+		timezoneStr += " (" + zoneOffsetStr + ")";
+
+		return timezoneStr;
 	}
 
 	/**
@@ -286,18 +295,18 @@
 		return dateStr;
 	}
 
-    /**
-     * Returns the current time as formatted string.
-     * 
-     * @param formatter
-     *            date format (if {@code null}, the default format is used)
-     * @see #DEFAULT_DATE_TIME_FORMATTER
-     */
-    public static String getCurrentTimeFormatted(DateFormat formatter) {
-        return formatter.format(new Date(System.currentTimeMillis()));
-    }
+	/**
+	 * Returns the current time as formatted string.
+	 * 
+	 * @param formatter
+	 *            date format (if {@code null}, the default format is used)
+	 * @see #DEFAULT_DATE_TIME_FORMATTER
+	 */
+	public static String getCurrentTimeFormatted(DateFormat formatter) {
+		return formatter.format(new Date(System.currentTimeMillis()));
+	}
 
-    /**
+	/**
 	 * Returns the current time as formatted string.
 	 * 
 	 * @param format
@@ -339,28 +348,28 @@
 
 		return combinedDateCal.getTime();
 	}
-	
+
 	/**
 	 * Returns a {@link Date} which represents the given day and time in GMT time zone.<br>
 	 * <b>For example:</b><br>
-	 * The given date is 01.01.2011, 00:00 (in CET). Then this method returns 01.01.2011, 01:00, because
-	 * the timezone offset (+1 hour + possible daylight saving time offset) is added. In other
-	 * words: This method returns the date (in current timezone), when GMT represents the moment of
-	 * the given date.<br>
+	 * The given date is 01.01.2011, 00:00 (in CET). Then this method returns 01.01.2011, 01:00, because the timezone
+	 * offset (+1 hour + possible daylight saving time offset) is added. In other words: This method returns the date
+	 * (in current timezone), when GMT represents the moment of the given date.<br>
 	 * <b>Field of application:</b><br>
-	 * Excel does not handle timezones, so it will always show an exported date in GMT (java internal
-	 * timestamp). For birthdates or many other "fixed" moments this is not the expected way!
-	 * This method converts e.g. the birthdate (in the current timezone) so that it represents the
-	 * exact birthdate in GMT.
-	 * @param date a date in any timezone 
+	 * Excel does not handle timezones, so it will always show an exported date in GMT (java internal timestamp). For
+	 * birthdates or many other "fixed" moments this is not the expected way! This method converts e.g. the birthdate
+	 * (in the current timezone) so that it represents the exact birthdate in GMT.
+	 * 
+	 * @param date
+	 *            a date in any timezone
 	 * @return the date (in current timezone), when GMT represents this day and time
 	 */
 	public static Date getGmtDate(Date date) {
-      GregorianCalendar c = new GregorianCalendar();
-      c.setTime(date);
-      c.add(Calendar.MILLISECOND, c.get(Calendar.ZONE_OFFSET));
-      c.add(Calendar.MILLISECOND, c.get(Calendar.DST_OFFSET));
-      return c.getTime();
+		GregorianCalendar c = new GregorianCalendar();
+		c.setTime(date);
+		c.add(Calendar.MILLISECOND, c.get(Calendar.ZONE_OFFSET));
+		c.add(Calendar.MILLISECOND, c.get(Calendar.DST_OFFSET));
+		return c.getTime();
 	}
 
 	/**
@@ -649,7 +658,8 @@
 	 *            aneinanderzuhaengende Stings als Liste
 	 */
 	public static String stringConcatWithSep(String sep, Collection<? extends Object> list) {
-		if (list == null) return "";
+		if (list == null)
+			return "";
 		return stringConcatWithSep(sep, null, list.toArray());
 	}
 
@@ -723,14 +733,14 @@
 	 */
 	public static String stringConcatWithSep(String sep, String maskChar, boolean ignoreNulls, Object... str) {
 		StringBuffer sb = new StringBuffer();
-		
+
 		// wenn statt einem Object-Array ein nativer Array angegeben wird,
 		// wird dieser nur als ein Parameter behandelt
 		// -> In Objekt-Liste umkopieren
-		if ( str.length == 1 && BaseTypeUtil.isNativeArray(str[0]) ) {
-		  str = BaseTypeUtil.copyToList(str[0], Object.class).toArray();
+		if (str.length == 1 && BaseTypeUtil.isNativeArray(str[0])) {
+			str = BaseTypeUtil.copyToList(str[0], Object.class).toArray();
 		}
-		
+
 		for (int i = 0; str != null && i < str.length; i++) {
 			if (ignoreNulls && (str[i] == null || str[i].toString().trim().equals("")))
 				continue;
@@ -2023,15 +2033,15 @@
 		if (trimmedLowcase.endsWith("no"))
 			return false;
 		if (trimmedLowcase.endsWith("nein"))
-			return false;		
+			return false;
 		if (trimmedLowcase.equals("n"))
 			return false;
 		if (trimmedLowcase.startsWith("false"))
 			return false;
-        if (trimmedLowcase.startsWith("falsch"))
-          return false;
-        if (trimmedLowcase.startsWith("wrong"))
-            return false;        
+		if (trimmedLowcase.startsWith("falsch"))
+			return false;
+		if (trimmedLowcase.startsWith("wrong"))
+			return false;
 		if (trimmedLowcase.startsWith("0"))
 			return false;
 		if (trimmedLowcase.startsWith("yes"))
@@ -2046,7 +2056,7 @@
 			return true;
 		if (trimmedLowcase.equals("j"))
 			return true;
-		if (trimmedLowcase.startsWith("1"))
+		if (trimmedLowcase.matches("^1[^\\s]?"))
 			return true;
 		if (trimmedLowcase.startsWith("true"))
 			return true;
@@ -2060,8 +2070,8 @@
 			return true;
 		if (trimmedLowcase.contains("wahr"))
 			return true;
-        if (trimmedLowcase.contains("existie"))
-          return true;
+		if (trimmedLowcase.contains("existie"))
+			return true;
 		// Default
 		return null;
 	}
@@ -2225,7 +2235,7 @@
 	}
 
 	/**
-	 * Formatiert eine Zeitspanne als Text 
+	 * Formatiert eine Zeitspanne als Text
 	 */
 	public static String formatInterval(final long l) {
 		final long days = TimeUnit.MILLISECONDS.toDays(l);
@@ -2233,15 +2243,16 @@
 		final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr));
 		final long sec = TimeUnit.MILLISECONDS.toSeconds(l - TimeUnit.HOURS.toMillis(hr)
 				- TimeUnit.MINUTES.toMillis(min));
-//		final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)
-//				- TimeUnit.SECONDS.toMillis(sec));
+		// final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) -
+		// TimeUnit.MINUTES.toMillis(min)
+		// - TimeUnit.SECONDS.toMillis(sec));
 		if (days > 0)
 			return String.format("%ddays %02d:%02d:%02d", days, hr, min, sec);
 		else
 			return String.format("%02d:%02d:%02d", hr, min, sec);
-//		if (days > 0)
-//			return String.format("%ddays %02d:%02d:%02d.%03d", days, hr, min, sec, ms);
-//		else
-//			return String.format("%02d:%02d:%02d.%03d", days, hr, min, sec, ms);
+		// if (days > 0)
+		// return String.format("%ddays %02d:%02d:%02d.%03d", days, hr, min, sec, ms);
+		// else
+		// return String.format("%02d:%02d:%02d.%03d", days, hr, min, sec, ms);
 	}
 }



More information about the Schmitzm-commits mailing list