1/* 2 * Copyright (c) 2016 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.matchers.text; 6 7import java.lang.reflect.Array; 8import java.util.Iterator; 9 10/** 11 * Inspired on hamcrest, internal package class, 12 * TODO add specific unit tests instead of relying on higher level unit tests 13 */ 14class ArrayIterator implements Iterator<Object> { 15 16 private final Object array; 17 private int currentIndex = 0; 18 19 public ArrayIterator(Object array) { 20 if (array == null) { 21 //TODO extract a small utility for null-checking 22 throw new IllegalArgumentException("Expected array instance but got null"); 23 } 24 if (!array.getClass().isArray()) { 25 throw new IllegalArgumentException("Expected array but got object of type: " 26 + array.getClass() + ", the object: " + array.toString()); 27 } 28 this.array = array; 29 } 30 31 public boolean hasNext() { 32 return currentIndex < Array.getLength(array); 33 } 34 35 public Object next() { 36 return Array.get(array, currentIndex++); 37 } 38 39 public void remove() { 40 throw new UnsupportedOperationException("cannot remove items from an array"); 41 } 42} 43