View Javadoc

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