Java tutorial
//package com.java2s; import java.lang.reflect.Array; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; public class Main { /** * Given an Object, and an index, returns the nth value in the * object. * <ul> * <li>If obj is a Map, returns the nth value from the <b>keySet</b> iterator, unless * the Map contains an Integer key with integer value = idx, in which case the * corresponding map entry value is returned. If idx exceeds the number of entries in * the map, an empty Iterator is returned. * <li>If obj is a List or an array, returns the nth value, throwing IndexOutOfBoundsException, * ArrayIndexOutOfBoundsException, resp. if the nth value does not exist. * <li>If obj is an iterator, enumeration or Collection, returns the nth value from the iterator, * returning an empty Iterator (resp. Enumeration) if the nth value does not exist. * <li>Returns the original obj if it is null or not a Collection or Iterator. * </ul> * * @param obj the object to get an index of, may be null * @param idx the index to get * @throws IndexOutOfBoundsException * @throws ArrayIndexOutOfBoundsException * * @deprecated use {@link #get(Object, int)} instead. Will be removed in v4.0 */ public static Object index(Object obj, int idx) { return index(obj, new Integer(idx)); } /** * Given an Object, and a key (index), returns the value associated with * that key in the Object. The following checks are made: * <ul> * <li>If obj is a Map, use the index as a key to get a value. If no match continue. * <li>Check key is an Integer. If not, return the object passed in. * <li>If obj is a Map, get the nth value from the <b>keySet</b> iterator. * If the Map has fewer than n entries, return an empty Iterator. * <li>If obj is a List or an array, get the nth value, throwing IndexOutOfBoundsException, * ArrayIndexOutOfBoundsException, resp. if the nth value does not exist. * <li>If obj is an iterator, enumeration or Collection, get the nth value from the iterator, * returning an empty Iterator (resp. Enumeration) if the nth value does not exist. * <li>Return the original obj. * </ul> * * @param obj the object to get an index of * @param index the index to get * @return the object at the specified index * @throws IndexOutOfBoundsException * @throws ArrayIndexOutOfBoundsException * * @deprecated use {@link #get(Object, int)} instead. Will be removed in v4.0 */ public static Object index(Object obj, Object index) { if (obj instanceof Map) { Map map = (Map) obj; if (map.containsKey(index)) { return map.get(index); } } int idx = -1; if (index instanceof Integer) { idx = ((Integer) index).intValue(); } if (idx < 0) { return obj; } else if (obj instanceof Map) { Map map = (Map) obj; Iterator iterator = map.keySet().iterator(); return index(iterator, idx); } else if (obj instanceof List) { return ((List) obj).get(idx); } else if (obj instanceof Object[]) { return ((Object[]) obj)[idx]; } else if (obj instanceof Enumeration) { Enumeration it = (Enumeration) obj; while (it.hasMoreElements()) { idx--; if (idx == -1) { return it.nextElement(); } else { it.nextElement(); } } } else if (obj instanceof Iterator) { return index((Iterator) obj, idx); } else if (obj instanceof Collection) { Iterator iterator = ((Collection) obj).iterator(); return index(iterator, idx); } return obj; } private static Object index(Iterator iterator, int idx) { while (iterator.hasNext()) { idx--; if (idx == -1) { return iterator.next(); } else { iterator.next(); } } return iterator; } /** * Returns the <code>index</code>-th value in <code>object</code>, throwing * <code>IndexOutOfBoundsException</code> if there is no such element or * <code>IllegalArgumentException</code> if <code>object</code> is not an * instance of one of the supported types. * <p> * The supported types, and associated semantics are: * <ul> * <li> Map -- the value returned is the <code>Map.Entry</code> in position * <code>index</code> in the map's <code>entrySet</code> iterator, * if there is such an entry.</li> * <li> List -- this method is equivalent to the list's get method.</li> * <li> Array -- the <code>index</code>-th array entry is returned, * if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code> * is thrown.</li> * <li> Collection -- the value returned is the <code>index</code>-th object * returned by the collection's default iterator, if there is such an element.</li> * <li> Iterator or Enumeration -- the value returned is the * <code>index</code>-th object in the Iterator/Enumeration, if there * is such an element. The Iterator/Enumeration is advanced to * <code>index</code> (or to the end, if <code>index</code> exceeds the * number of entries) as a side effect of this method.</li> * </ul> * * @param object the object to get a value from * @param index the index to get * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid * @throws IllegalArgumentException if the object type is invalid */ public static Object get(Object object, int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index cannot be negative: " + index); } if (object instanceof Map) { Map map = (Map) object; Iterator iterator = map.entrySet().iterator(); return get(iterator, index); } else if (object instanceof List) { return ((List) object).get(index); } else if (object instanceof Object[]) { return ((Object[]) object)[index]; } else if (object instanceof Iterator) { Iterator it = (Iterator) object; while (it.hasNext()) { index--; if (index == -1) { return it.next(); } else { it.next(); } } throw new IndexOutOfBoundsException("Entry does not exist: " + index); } else if (object instanceof Collection) { Iterator iterator = ((Collection) object).iterator(); return get(iterator, index); } else if (object instanceof Enumeration) { Enumeration it = (Enumeration) object; while (it.hasMoreElements()) { index--; if (index == -1) { return it.nextElement(); } else { it.nextElement(); } } throw new IndexOutOfBoundsException("Entry does not exist: " + index); } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.get(object, index); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } } }