Coverage Report - org.mule.util.MapCombiner
 
Classes in this File Line Coverage Branch Coverage Complexity
MapCombiner
71%
39/55
68%
19/28
1.524
 
 1  
 /*
 2  
  * $Id: MapCombiner.java 10489 2008-01-23 17:53:38Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util;
 12  
 
 13  
 import java.util.Collection;
 14  
 import java.util.HashMap;
 15  
 import java.util.Iterator;
 16  
 import java.util.List;
 17  
 import java.util.Map;
 18  
 import java.util.Set;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 
 23  
 /**
 24  
  * This allows a collection (list) of maps to be defined in Spring, via the "list" property, and
 25  
  * then presents all the maps as a single combine map at run time.  For efficiency the combination
 26  
  * of maps is done once and then cached.
 27  
  */
 28  982
 public class MapCombiner implements Map
 29  
 {
 30  
 
 31  
     public static final String LIST = "list"; // the setter/getter
 32  
     public static final int UNLIMITED_DEPTH = -1;
 33  
 
 34  982
     private Log logger = LogFactory.getLog(getClass());
 35  982
     private int maxDepth = UNLIMITED_DEPTH;
 36  
     private List list;
 37  982
     private Map cachedMerge = new HashMap();
 38  982
     private boolean isMerged = false;
 39  
 
 40  
     private synchronized Map getCachedMerge()
 41  
     {
 42  1966
         if (!isMerged)
 43  
         {
 44  982
             for (Iterator maps = list.iterator(); maps.hasNext();)
 45  
             {
 46  1964
                 mergeMaps(maxDepth, cachedMerge, (Map) maps.next());
 47  
             }
 48  982
             isMerged = true;
 49  
         }
 50  1966
         return cachedMerge;
 51  
     }
 52  
 
 53  
     public void setMaxDepth(int maxDepth)
 54  
     {
 55  4
         this.maxDepth = maxDepth;
 56  4
     }
 57  
 
 58  
     private void mergeMaps(int headroom, Map accumulator, Map extra)
 59  
     {
 60  1972
         for (Iterator keys = extra.keySet().iterator(); keys.hasNext();)
 61  
         {
 62  52
             Object key = keys.next();
 63  52
             Object valueExtra = extra.get(key);
 64  52
             if (accumulator.containsKey(key))
 65  
             {
 66  14
                 Object valueOriginal = accumulator.get(key);
 67  14
                 if (valueExtra instanceof Map && valueOriginal instanceof Map && headroom != 0)
 68  
                 {
 69  8
                     mergeMaps(headroom - 1, (Map) valueOriginal, (Map) valueExtra);
 70  
                 }
 71  6
                 else if (valueExtra instanceof Collection && valueOriginal instanceof Collection && headroom != 0)
 72  
                 {
 73  2
                     ((Collection) valueOriginal).addAll((Collection) valueExtra);
 74  
                 }
 75  
                 else
 76  
                 {
 77  4
                     if (logger.isDebugEnabled())
 78  
                     {
 79  0
                         logger.debug("Overwriting " + valueOriginal + " for " + key + " during map merge");
 80  
                     }
 81  4
                     accumulator.put(key, valueExtra);
 82  
                 }
 83  14
             }
 84  
             else
 85  
             {
 86  38
                 accumulator.put(key, valueExtra);
 87  
             }
 88  52
         }
 89  1972
     }
 90  
 
 91  
     public void setList(List list)
 92  
     {
 93  982
         assertNotMerged();
 94  982
         this.list = list;
 95  982
     }
 96  
 
 97  
     public List getList()
 98  
     {
 99  20
         assertNotMerged();
 100  20
         return list;
 101  
     }
 102  
 
 103  
     private synchronized void assertNotMerged()
 104  
     {
 105  1002
         if (isMerged)
 106  
         {
 107  0
             throw new IllegalStateException("Maps have already been merged");
 108  
         }
 109  1002
     }
 110  
 
 111  
     // hashcode, equals and toString don't trigger merge
 112  
 
 113  
     public int hashCode()
 114  
     {
 115  0
         return cachedMerge.hashCode();
 116  
     }
 117  
 
 118  
     public boolean equals(Object o)
 119  
     {
 120  10
         return cachedMerge.equals(o);
 121  
     }
 122  
 
 123  
     public String toString()
 124  
     {
 125  0
         if (isMerged)
 126  
         {
 127  0
             return "merged: " + cachedMerge.toString();
 128  
         }
 129  
         else
 130  
         {
 131  0
             return "unmerged: " + (null == list ? null : list.toString());
 132  
         }
 133  
     }
 134  
 
 135  
     public int size()
 136  
     {
 137  486
         return getCachedMerge().size();
 138  
     }
 139  
 
 140  
     public void clear()
 141  
     {
 142  0
         getCachedMerge().clear();
 143  0
     }
 144  
 
 145  
     public boolean isEmpty()
 146  
     {
 147  10
         return getCachedMerge().isEmpty();
 148  
     }
 149  
 
 150  
     public boolean containsKey(Object key)
 151  
     {
 152  1458
         return getCachedMerge().containsKey(key);
 153  
     }
 154  
 
 155  
     public boolean containsValue(Object value)
 156  
     {
 157  0
         return getCachedMerge().containsValue(value);
 158  
     }
 159  
 
 160  
     public Collection values()
 161  
     {
 162  0
         return getCachedMerge().values();
 163  
     }
 164  
 
 165  
     public void putAll(Map t)
 166  
     {
 167  0
         getCachedMerge().putAll(t);
 168  0
     }
 169  
 
 170  
     public Set entrySet()
 171  
     {
 172  12
         return getCachedMerge().entrySet();
 173  
     }
 174  
 
 175  
     public Set keySet()
 176  
     {
 177  0
         return getCachedMerge().keySet();
 178  
     }
 179  
 
 180  
     public Object get(Object key)
 181  
     {
 182  0
         return getCachedMerge().get(key);
 183  
     }
 184  
 
 185  
     public Object remove(Object key)
 186  
     {
 187  0
         return getCachedMerge().remove(key);
 188  
     }
 189  
 
 190  
     public Object put(Object key, Object value)
 191  
     {
 192  0
         return getCachedMerge().put(key, value);
 193  
     }
 194  
 
 195  
 }