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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 21 20:24:14 CET 2012


Author: mojays
Date: 2012-03-21 20:24:14 +0100 (Wed, 21 Mar 2012)
New Revision: 1905

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil: new method transformMapKeys(.) to convert map keys according to mapping

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-03-20 18:46:36 UTC (rev 1904)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-03-21 19:24:14 UTC (rev 1905)
@@ -51,8 +51,10 @@
 import java.util.Comparator;
 import java.util.Date;
 import java.util.GregorianCalendar;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.TimeZone;
 import java.util.TreeSet;
 import java.util.Vector;
@@ -101,6 +103,11 @@
 
 	final static Pattern onlyZerosRegEx = Pattern.compile("^([\\s0.,]*)$");
 
+	/** Dummy day representing the 01.01.0001. */
+	public static final Date DAY1 = new GregorianCalendar(1,Calendar.JANUARY,1).getTime();
+    /** Dummy day representing the 02.01.0001. */
+    public static final Date DAY2 = new GregorianCalendar(1,Calendar.JANUARY,2).getTime();
+	
 	/**
 	 * Date formatter for "dd.MM.yyyy [HH:mm:ss]".
 	 */
@@ -784,6 +791,28 @@
 	}
 
 	/**
+	 * Transforms map keys according to a mapping.
+	 * @param sourceMap source map
+	 * @param mapping   mapping from source key to destination key (if mapping contains
+	 *                  no value for a key of source map, the original key is used!)
+	 * @param destMap   destination map to put the values in (if {@code null} a new
+	 *                  {@link HashMap} is created)
+	 * @return {@code sourceMap} if mapping is {@code null}
+	 */
+	public static <K,V> Map<K,V> transformMapKeys(Map<K,V> sourceMap, Map<K,K> mapping, Map<K,V> destMap) {
+	  if ( mapping == null )
+	    return sourceMap;
+	  if ( destMap == null )
+	    destMap = new HashMap<K,V>();  
+	  for (K sourceKey : sourceMap.keySet()) {
+	    K destKey = mapping.get(sourceKey);
+	    V  value  = sourceMap.get(sourceKey);
+	    destMap.put(destKey != null ? destKey : sourceKey,value);
+	  }
+	  return destMap;
+	}
+
+	/**
 	 * Erzeugt einen vorbelegten Array.
 	 * 
 	 * @param count



More information about the Schmitzm-commits mailing list