1 5 package com.opensymphony.webwork.util; 6 7 import java.lang.reflect.Array ; 8 import java.util.*; 9 10 11 19 public class MakeIterator { 20 22 29 public static boolean isIterable(Object object) { 30 if (object == null) { 31 return false; 32 } 33 34 if (object instanceof Map) { 35 return true; 36 } else if (object instanceof Collection) { 37 return true; 38 } else if (object.getClass().isArray()) { 39 return true; 40 } else if (object instanceof Enumeration) { 41 return true; 42 } else if (object instanceof Iterator) { 43 return true; 44 } else { 45 return false; 46 } 47 } 48 49 public static Iterator convert(Object value) { 50 Iterator iterator; 51 52 if (value instanceof Iterator) { 53 return (Iterator) value; 54 } 55 56 if (value instanceof Map) { 57 value = ((Map) value).entrySet(); 58 } 59 60 if (value == null) { 61 return null; 62 } 63 64 if (value instanceof Collection) { 65 iterator = ((Collection) value).iterator(); 66 } else if (value.getClass().isArray()) { 67 Object a = Array.newInstance(value.getClass().getComponentType(), (Array.getLength(value))); 70 ArrayList list = new ArrayList(Array.getLength(value)); 71 72 for (int j = 0; j < Array.getLength(value); j++) { 73 list.add(Array.get(value, j)); 74 } 75 76 iterator = list.iterator(); 77 } else if (value instanceof Enumeration) { 78 Enumeration enumeration = (Enumeration) value; 79 ArrayList list = new ArrayList(); 80 81 while (enumeration.hasMoreElements()) { 82 list.add(enumeration.nextElement()); 83 } 84 85 iterator = list.iterator(); 86 } else { 87 List list = new ArrayList(1); 88 list.add(value); 89 iterator = list.iterator(); 90 } 91 92 return iterator; 93 } 94 } 95 | Popular Tags |