KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > MakeIterator


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import java.lang.reflect.Array JavaDoc;
8 import java.util.*;
9
10
11 /**
12  * MakeIterator
13  * <p/>
14  * Taken from WebWork 1.x by:
15  *
16  * @author &lt;a HREF="hermanns@aixcept.de"&gt;Rainer Hermanns&lt;/a&gt;
17  * @version $Id: MakeIterator.java,v 1.8 2005/04/01 06:44:12 jcarreira Exp $
18  */

19 public class MakeIterator {
20     //~ Methods ////////////////////////////////////////////////////////////////
21

22     /**
23      * Determine whether a given object can be made into an <code>Iterator</code>
24      *
25      * @param object the object to check
26      * @return <code>true</code> if the object can be converted to an iterator and
27      * <code>false</code> otherwise
28      */

29     public static boolean isIterable(Object JavaDoc 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 JavaDoc 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             //need ability to support primitives; therefore, cannot
68
//use Object[] casting.
69
Object JavaDoc 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