The Precondition.checkElementIndex (...) method does not care about the "target". You simply pass size and index as follows:
public String getElement(List<String> list, int index) { Preconditions.checkElementIndex(index, list.size(), "The given index is not valid"); return list.get(index); }
According to the Guava link, the checkElementIndex method can be implemented as follows:
public class Preconditions { public static int checkElementIndex(int index, int size) { if (size < 0) throw new IllegalArgumentException(); if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return index; } }
As you can see, he does not need to know a list, an array, or anything else.
source share