### Question:
ReflectionHelper { public static Object getIndexedValue(Object value, String index) { if ( value == null ) { return null; } int numIndex = -1; try { numIndex = Integer.valueOf( index ); } catch ( NumberFormatException nfe ) { } if ( numIndex == -1 ) { Map<?, ?> map = ( Map<?, ?> ) value; return map.get( index ); } Iterator<?> iter = null; Type type = value.getClass(); if ( isIterable( type ) ) { iter = ( ( Iterable<?> ) value ).iterator(); } else if ( isMap( type ) ) { Map<?, ?> map = ( Map<?, ?> ) value; iter = map.values().iterator(); } else if ( TypeUtils.isArray( type ) ) { List<?> arrayList = Arrays.asList( value ); iter = arrayList.iterator(); } int i = 0; Object o; while ( iter.hasNext() ) { o = iter.next(); if ( i == numIndex ) { return o; } i++; } return null; } private  ReflectionHelper(); @SuppressWarnings("unchecked") static T getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type); static String getPropertyName(Member member); static Class<?> getType(Member member); static Class<?> getAnnotations(Member member); static Type typeOf(Member member); static Object getValue(Member member, Object object); static void setAccessibility(Member member); static Class<?> loadClass(String name, Class<?> caller); static Type getIndexedType(Type type); static boolean isIterable(Type type); static boolean isMap(Type type); static Object getIndexedValue(Object value, String index); static boolean containsMember(Class<?> clazz, String property); static boolean containsField(Class<?> clazz, String fieldName); static Field getField(Class<?> clazz, String fieldName); static boolean containsMethod(Class<?> clazz, String methodName); static Method getMethod(Class<?> clazz, String methodName); static Class<?> classForName(String name, Class<?> caller);  }### Answer:
@Test public void testGetIndexedValueForMap() { Map<String, Object> map = new HashMap<String, Object>(); Object testObject = new Object(); String key = "key"; map.put( key, testObject ); Object value = ReflectionHelper.getIndexedValue( map, key ); assertEquals( value, testObject, "We should be able to retrieve the indexed object" ); value = ReflectionHelper.getIndexedValue( map, "0" ); assertEquals( value, testObject, "We should be able to retrieve the indexed object" ); value = ReflectionHelper.getIndexedValue( map, "foo" ); assertNull( value, "A non existent index should return the null value" ); value = ReflectionHelper.getIndexedValue( map, "2" ); assertNull( value, "A non existent index should return the null value" ); }
@Test public void testGetIndexedValueForList() { List<Object> list = new ArrayList<Object>(); Object testObject = new Object(); list.add( testObject ); Object value = ReflectionHelper.getIndexedValue( list, "0" ); assertEquals( value, testObject, "We should be able to retrieve the indexed object" ); value = ReflectionHelper.getIndexedValue( list, "2" ); assertNull( value, "A non existent index should return the null value" ); }
@Test public void testGetIndexedValueForNull() { Object value = ReflectionHelper.getIndexedValue( null, "0" ); assertNull( value ); }