001: /*
002: * $Id: MakeIterator.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.lang.reflect.Array;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * MakeIterator.
033: *
034: */
035: public class MakeIterator {
036:
037: /**
038: * Determine whether a given object can be made into an <code>Iterator</code>
039: *
040: * @param object the object to check
041: * @return <code>true</code> if the object can be converted to an iterator and
042: * <code>false</code> otherwise
043: */
044: public static boolean isIterable(Object object) {
045: if (object == null) {
046: return false;
047: }
048:
049: if (object instanceof Map) {
050: return true;
051: } else if (object instanceof Collection) {
052: return true;
053: } else if (object.getClass().isArray()) {
054: return true;
055: } else if (object instanceof Enumeration) {
056: return true;
057: } else if (object instanceof Iterator) {
058: return true;
059: } else {
060: return false;
061: }
062: }
063:
064: public static Iterator convert(Object value) {
065: Iterator iterator;
066:
067: if (value instanceof Iterator) {
068: return (Iterator) value;
069: }
070:
071: if (value instanceof Map) {
072: value = ((Map) value).entrySet();
073: }
074:
075: if (value == null) {
076: return null;
077: }
078:
079: if (value instanceof Collection) {
080: iterator = ((Collection) value).iterator();
081: } else if (value.getClass().isArray()) {
082: //need ability to support primitives; therefore, cannot
083: //use Object[] casting.
084: ArrayList list = new ArrayList(Array.getLength(value));
085:
086: for (int j = 0; j < Array.getLength(value); j++) {
087: list.add(Array.get(value, j));
088: }
089:
090: iterator = list.iterator();
091: } else if (value instanceof Enumeration) {
092: Enumeration enumeration = (Enumeration) value;
093: ArrayList list = new ArrayList();
094:
095: while (enumeration.hasMoreElements()) {
096: list.add(enumeration.nextElement());
097: }
098:
099: iterator = list.iterator();
100: } else {
101: List list = new ArrayList(1);
102: list.add(value);
103: iterator = list.iterator();
104: }
105:
106: return iterator;
107: }
108: }
|