Coverage Report - org.mule.util.MapUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MapUtils
0%
0/41
0%
0/12
4
 
 1  
 /*
 2  
  * $Id: MapUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.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 Map mapWithKeysAndValues(Class mapClass, Object[] keys, Object[] values)
 28  
     {
 29  0
         Collection keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST);
 30  0
         Collection valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST);
 31  0
         return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator());
 32  
     }
 33  
 
 34  
     /**
 35  
      * Convenience method for CollectionUtil#mapWithKeysAndValues(Class, Iterator,
 36  
      * Iterator); keys and values can be null or empty.
 37  
      */
 38  
     public static Map mapWithKeysAndValues(Class mapClass, Collection keys, Collection 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 Map mapWithKeysAndValues(Class mapClass, Iterator keys, Iterator values)
 55  
     {
 56  0
         Map m = null;
 57  
 
 58  0
         if (mapClass == null)
 59  
         {
 60  0
             throw new IllegalArgumentException("Map class must not be null!");
 61  
         }
 62  
 
 63  
         try
 64  
         {
 65  0
             m = (Map) mapClass.newInstance();
 66  
         }
 67  0
         catch (Exception ex)
 68  
         {
 69  0
             throw new RuntimeException(ex);
 70  0
         }
 71  
 
 72  0
         if (keys != null && values != null)
 73  
         {
 74  0
             while (keys.hasNext() && values.hasNext())
 75  
             {
 76  0
                 m.put(keys.next(), values.next());
 77  
             }
 78  
         }
 79  
 
 80  0
         return m;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Creates a String representation of the given Map, with optional newlines
 85  
      * between elements.
 86  
      * 
 87  
      * @param props the map to format
 88  
      * @param newline indicates whether elements are to be split across lines
 89  
      * @return the formatted String
 90  
      */
 91  
     public static String toString(Map props, boolean newline)
 92  
     {
 93  0
         if (props == null || props.isEmpty())
 94  
         {
 95  0
             return "{}";
 96  
         }
 97  
 
 98  0
         StringBuffer buf = new StringBuffer(props.size() * 32);
 99  0
         buf.append('{');
 100  
 
 101  0
         if (newline)
 102  
         {
 103  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 104  
         }
 105  
 
 106  0
         Object[] entries = props.entrySet().toArray();
 107  
         int i;
 108  
 
 109  0
         for (i = 0; i < entries.length - 1; i++)
 110  
         {
 111  0
             Map.Entry property = (Map.Entry) entries[i];
 112  0
             buf.append(property.getKey());
 113  0
             buf.append('=');
 114  0
             buf.append(PropertiesUtils.maskedPropertyValue(property));
 115  
 
 116  0
             if (newline)
 117  
             {
 118  0
                 buf.append(SystemUtils.LINE_SEPARATOR);
 119  
             }
 120  
             else
 121  
             {
 122  0
                 buf.append(',').append(' ');
 123  
             }
 124  
         }
 125  
 
 126  
         // don't forget the last one
 127  0
         Map.Entry lastProperty = (Map.Entry) entries[i];
 128  0
         buf.append(lastProperty.getKey().toString());
 129  0
         buf.append('=');
 130  0
         buf.append(PropertiesUtils.maskedPropertyValue(lastProperty));
 131  
 
 132  0
         if (newline)
 133  
         {
 134  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 135  
         }
 136  
 
 137  0
         buf.append('}');
 138  0
         return buf.toString();
 139  
     }
 140  
 
 141  
 }