View Javadoc

1   /*
2    * $Id: MakeIterator.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.util;
22  
23  import java.lang.reflect.Array;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Enumeration;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  
32  /***
33   * MakeIterator.
34   *
35   */
36  public class MakeIterator {
37  
38      /***
39       * Determine whether a given object can be made into an <code>Iterator</code>
40       *
41       * @param object the object to check
42       * @return <code>true</code> if the object can be converted to an iterator and
43       *         <code>false</code> otherwise
44       */
45      public static boolean isIterable(Object object) {
46          if (object == null) {
47              return false;
48          }
49  
50          if (object instanceof Map) {
51              return true;
52          } else if (object instanceof Collection) {
53              return true;
54          } else if (object.getClass().isArray()) {
55              return true;
56          } else if (object instanceof Enumeration) {
57              return true;
58          } else if (object instanceof Iterator) {
59              return true;
60          } else {
61              return false;
62          }
63      }
64  
65      public static Iterator convert(Object value) {
66          Iterator iterator;
67  
68          if (value instanceof Iterator) {
69              return (Iterator) value;
70          }
71  
72          if (value instanceof Map) {
73              value = ((Map) value).entrySet();
74          }
75  
76          if (value == null) {
77              return null;
78          }
79  
80          if (value instanceof Collection) {
81              iterator = ((Collection) value).iterator();
82          } else if (value.getClass().isArray()) {
83              //need ability to support primitives; therefore, cannot
84              //use Object[] casting.
85              ArrayList list = new ArrayList(Array.getLength(value));
86  
87              for (int j = 0; j < Array.getLength(value); j++) {
88                  list.add(Array.get(value, j));
89              }
90  
91              iterator = list.iterator();
92          } else if (value instanceof Enumeration) {
93              Enumeration enumeration = (Enumeration) value;
94              ArrayList list = new ArrayList();
95  
96              while (enumeration.hasMoreElements()) {
97                  list.add(enumeration.nextElement());
98              }
99  
100             iterator = list.iterator();
101         } else {
102             List list = new ArrayList(1);
103             list.add(value);
104             iterator = list.iterator();
105         }
106 
107         return iterator;
108     }
109 }