You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jo...@apache.org on 2007/04/07 06:19:18 UTC

svn commit: r526354 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util: UtilDateTime.java UtilMisc.java

Author: jonesde
Date: Fri Apr  6 21:19:10 2007
New Revision: 526354

URL: http://svn.apache.org/viewvc?view=rev&rev=526354
Log:
A few new methods for convenience, especially working with Lists and Maps of Maps and doing certain calculations

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilMisc.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java?view=diff&rev=526354&r1=526353&r2=526354
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilDateTime.java Fri Apr  6 21:19:10 2007
@@ -617,6 +617,7 @@
         calendar.setTime(date);
         return dateFormat.format(date);
     }
+    
     /**
      * Makes a date String in the format MM/DD/YYYY from a Date
      *
@@ -736,5 +737,3 @@
         return calendar.get(Calendar.WEEK_OF_YEAR);
     }
 }
-
-

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilMisc.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilMisc.java?view=diff&rev=526354&r1=526353&r2=526354
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilMisc.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilMisc.java Fri Apr  6 21:19:10 2007
@@ -18,6 +18,7 @@
  *******************************************************************************/
 package org.ofbiz.base.util;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -40,6 +41,8 @@
 public class UtilMisc {
 
     public static final String module = UtilMisc.class.getName();
+    
+    public static final BigDecimal ZERO_BD = new BigDecimal(0.0);
 
     /**
      * Get an iterator from a collection, returning null if collection is null
@@ -191,6 +194,55 @@
             return null;
         }
         return toSort;
+    }
+    
+    /**
+     * Assuming outerMap not null; if null will throw a NullPointerException 
+     */
+    public static Map getMapFromMap(Map outerMap, Object key) {
+        Map innerMap = (Map) outerMap.get(key);
+        if (innerMap == null) {
+            innerMap = FastMap.newInstance();
+            outerMap.put(key, innerMap);
+        }
+        return innerMap;
+    }
+
+    /**
+     * Assuming outerMap not null; if null will throw a NullPointerException 
+     */
+    public static List getListFromMap(Map outerMap, Object key) {
+        List innerList = (List) outerMap.get(key);
+        if (innerList == null) {
+            innerList = FastList.newInstance();
+            outerMap.put(key, innerList);
+        }
+        return innerList;
+    }
+    
+    /**
+     * Assuming theMap not null; if null will throw a NullPointerException 
+     */
+    public static void addToBigDecimalInMap(Map theMap, Object mapKey, BigDecimal addNumber) {
+        if (addNumber == null || ZERO_BD.equals(addNumber)) {
+            return;
+        }
+        Object currentNumberObj = theMap.get(mapKey);
+        BigDecimal currentNumber = null;
+        if (currentNumberObj == null) {
+            currentNumber = ZERO_BD;
+        } else if (currentNumberObj instanceof BigDecimal) {
+            currentNumber = (BigDecimal) currentNumberObj;
+        } else if (currentNumberObj instanceof Double) {
+            currentNumber = new BigDecimal(((Double) currentNumberObj).doubleValue());
+        } else if (currentNumberObj instanceof Long) {
+            currentNumber = new BigDecimal(((Long) currentNumberObj).longValue());
+        } else {
+            throw new IllegalArgumentException("In addToBigDecimalInMap found a Map value of a type not supported: " + currentNumberObj.getClass().getName());
+        }
+        
+        currentNumber = currentNumber.add(addNumber);
+        theMap.put(mapKey, currentNumber);
     }
 
     public static Object removeFirst(List lst) {