View Javadoc

1   /*
2    * $Id: MakeIterator.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.util;
23  
24  import java.lang.reflect.Array;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Enumeration;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  
33  /***
34   * MakeIterator.
35   *
36   */
37  public class MakeIterator {
38  
39      /***
40       * Determine whether a given object can be made into an <code>Iterator</code>
41       *
42       * @param object the object to check
43       * @return <code>true</code> if the object can be converted to an iterator and
44       *         <code>false</code> otherwise
45       */
46      public static boolean isIterable(Object object) {
47          if (object == null) {
48              return false;
49          }
50  
51          if (object instanceof Map) {
52              return true;
53          } else if (object instanceof Collection) {
54              return true;
55          } else if (object.getClass().isArray()) {
56              return true;
57          } else if (object instanceof Enumeration) {
58              return true;
59          } else if (object instanceof Iterator) {
60              return true;
61          } else {
62              return false;
63          }
64      }
65  
66      public static Iterator convert(Object value) {
67          Iterator iterator;
68  
69          if (value instanceof Iterator) {
70              return (Iterator) value;
71          }
72  
73          if (value instanceof Map) {
74              value = ((Map) value).entrySet();
75          }
76  
77          if (value == null) {
78              return null;
79          }
80  
81          if (value instanceof Collection) {
82              iterator = ((Collection) value).iterator();
83          } else if (value.getClass().isArray()) {
84              //need ability to support primitives; therefore, cannot
85              //use Object[] casting.
86              ArrayList list = new ArrayList(Array.getLength(value));
87  
88              for (int j = 0; j < Array.getLength(value); j++) {
89                  list.add(Array.get(value, j));
90              }
91  
92              iterator = list.iterator();
93          } else if (value instanceof Enumeration) {
94              Enumeration enumeration = (Enumeration) value;
95              ArrayList list = new ArrayList();
96  
97              while (enumeration.hasMoreElements()) {
98                  list.add(enumeration.nextElement());
99              }
100 
101             iterator = list.iterator();
102         } else {
103             List list = new ArrayList(1);
104             list.add(value);
105             iterator = list.iterator();
106         }
107 
108         return iterator;
109     }
110 }