org.elasticsearch.common.collect.BoundedArrayList Maven / Gradle / Ivy
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.collect;
import java.util.AbstractList;
import java.util.RandomAccess;
/**
* An array list that is based on an array and a size, only using the first size elements in the array.
*/
public class BoundedArrayList extends AbstractList implements RandomAccess {
private final E[] a;
private final int size;
public BoundedArrayList(E[] array, int size) {
if (array == null)
throw new NullPointerException();
a = array;
this.size = size;
}
public int size() {
return size;
}
public Object[] toArray() {
throw new UnsupportedOperationException();
}
public T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
public E get(int index) {
return a[index];
}
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < a.length; i++)
if (a[i] == null)
return i;
} else {
for (int i = 0; i < a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
public boolean contains(Object o) {
return indexOf(o) != -1;
}
}