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