001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.jexl2.internal;
018
019 import java.lang.reflect.Array;
020 import java.util.AbstractList;
021
022 /**
023 * A class that wraps an array with a List interface.
024 *
025 * @author Chris Schultz <chris@christopherschultz.net$gt;
026 * @version $Revision$ $Date: 2006-04-14 19:40:41 $
027 */
028 public class ArrayListWrapper extends AbstractList<Object> {
029 /** the array to wrap. */
030 private final Object array;
031
032 /**
033 * Create the wrapper.
034 * @param anArray {@link #array}
035 */
036 public ArrayListWrapper(Object anArray) {
037 if (!anArray.getClass().isArray()) {
038 throw new IllegalArgumentException(anArray.getClass() + " is not an array");
039 }
040 this.array = anArray;
041 }
042
043 /** {@inheritDoc} */
044 @Override
045 public Object get(int index) {
046 return Array.get(array, index);
047 }
048
049 /** {@inheritDoc} */
050 @Override
051 public Object set(int index, Object element) {
052 Object old = get(index);
053 Array.set(array, index, element);
054 return old;
055 }
056
057 /** {@inheritDoc} */
058 @Override
059 public int size() {
060 return Array.getLength(array);
061 }
062 }