All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.mockito.internal.matchers.text.ArrayIterator Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.mockito.internal.matchers.text;

import java.lang.reflect.Array;
import java.util.Iterator;

/**
 * Inspired on hamcrest, internal package class,
 * TODO add specific unit tests instead of relying on higher level unit tests
 */
class ArrayIterator implements Iterator {

    private final Object array;
    private int currentIndex = 0;

    public ArrayIterator(Object array) {
        if (array == null) {
            //TODO extract a small utility for null-checking
            throw new IllegalArgumentException("Expected array instance but got null");
        }
        if (!array.getClass().isArray()) {
            throw new IllegalArgumentException("Expected array but got object of type: "
                    + array.getClass() + ", the object: " + array.toString());
        }
        this.array = array;
    }

    public boolean hasNext() {
        return currentIndex < Array.getLength(array);
    }

    public Object next() {
        return Array.get(array, currentIndex++);
    }

    public void remove() {
        throw new UnsupportedOperationException("cannot remove items from an array");
    }
}