Coverage Report - org.mule.util.MapUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MapUtils
0%
0/41
0%
0/30
0
 
 1  
 /*
 2  
  * $Id: MapUtils.java 19494 2010-09-09 15:58:39Z epere4 $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.Arrays;
 14  
 import java.util.Collection;
 15  
 import java.util.Collections;
 16  
 import java.util.Iterator;
 17  
 import java.util.Map;
 18  
 
 19  
 // @ThreadSafe
 20  0
 public class MapUtils extends org.apache.commons.collections.MapUtils
 21  
 {
 22  
 
 23  
     /**
 24  
      * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
 25  
      * Iterator); keys and values can be null or empty.
 26  
      */
 27  
     public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
 28  
                                                         K[] keys,
 29  
                                                         V[] values)
 30  
     {
 31  0
         Collection<K> keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
 32  0
         Collection<V> valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST);
 33  0
         return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator());
 34  
     }
 35  
 
 36  
     /**
 37  
      * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
 38  
      * Iterator); keys and values can be null or empty.
 39  
      */
 40  
     public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
 41  
                                                         Collection<K> keys,
 42  
                                                         Collection<V> values)
 43  
     {
 44  0
         keys = (keys != null ? keys : Collections.EMPTY_LIST);
 45  0
         values = (values != null ? values : Collections.EMPTY_LIST);
 46  0
         return mapWithKeysAndValues(mapClass, keys.iterator(), values.iterator());
 47  
     }
 48  
 
 49  
     /**
 50  
      * Create & populate a Map of arbitrary class. Populating stops when either the
 51  
      * keys or values iterator is null or exhausted.
 52  
      * 
 53  
      * @param mapClass the Class of the Map to instantiate
 54  
      * @param keys iterator for Objects ued as keys
 55  
      * @param values iterator for Objects used as values
 56  
      * @return the instantiated Map
 57  
      */
 58  
     public static <K, V> Map<K, V> mapWithKeysAndValues(Class<? extends Map> mapClass,
 59  
                                                         Iterator<K> keys,
 60  
                                                         Iterator<V> values)
 61  
     {
 62  0
         Map<K, V> m = null;
 63  
 
 64  0
         if (mapClass == null)
 65  
         {
 66  0
             throw new IllegalArgumentException("Map class must not be null!");
 67  
         }
 68  
 
 69  
         try
 70  
         {
 71  0
             m = mapClass.newInstance();
 72  
         }
 73  0
         catch (Exception ex)
 74  
         {
 75  0
             throw new RuntimeException(ex);
 76  0
         }
 77  
 
 78  0
         if (keys != null && values != null)
 79  
         {
 80  0
             while (keys.hasNext() && values.hasNext())
 81  
             {
 82  0
                 m.put(keys.next(), values.next());
 83  
             }
 84  
         }
 85  
 
 86  0
         return m;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Creates a String representation of the given Map, with optional newlines
 91  
      * between elements.
 92  
      * 
 93  
      * @param props the map to format
 94  
      * @param newline indicates whether elements are to be split across lines
 95  
      * @return the formatted String
 96  
      */
 97  
     public static String toString(Map props, boolean newline)
 98  
     {
 99  0
         if (props == null || props.isEmpty())
 100  
         {
 101  0
             return "{}";
 102  
         }
 103  
 
 104  0
         StringBuffer buf = new StringBuffer(props.size() * 32);
 105  0
         buf.append('{');
 106  
 
 107  0
         if (newline)
 108  
         {
 109  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 110  
         }
 111  
 
 112  0
         Object[] entries = props.entrySet().toArray();
 113  
         int i;
 114  
 
 115  0
         for (i = 0; i < entries.length - 1; i++)
 116  
         {
 117  0
             Map.Entry property = (Map.Entry) entries[i];
 118  0
             buf.append(property.getKey());
 119  0
             buf.append('=');
 120  0
             buf.append(PropertiesUtils.maskedPropertyValue(property));
 121  
 
 122  0
             if (newline)
 123  
             {
 124  0
                 buf.append(SystemUtils.LINE_SEPARATOR);
 125  
             }
 126  
             else
 127  
             {
 128  0
                 buf.append(',').append(' ');
 129  
             }
 130  
         }
 131  
 
 132  
         // don't forget the last one
 133  0
         Map.Entry lastProperty = (Map.Entry) entries[i];
 134  0
         buf.append(lastProperty.getKey().toString());
 135  0
         buf.append('=');
 136  0
         buf.append(PropertiesUtils.maskedPropertyValue(lastProperty));
 137  
 
 138  0
         if (newline)
 139  
         {
 140  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 141  
         }
 142  
 
 143  0
         buf.append('}');
 144  0
         return buf.toString();
 145  
     }
 146  
 
 147  
 }