View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl2.internal;
18  
19  import java.lang.reflect.Array;
20  import java.util.AbstractList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.ListIterator;
25  
26  /**
27   * A class that wraps an array within an AbstractList.
28   * <p>
29   * It overrides all methods because introspection uses this class a a marker for wrapped arrays; the declared class
30   * for any method is thus always ArrayListWrapper.
31   * </p>
32   */
33  public class ArrayListWrapper extends AbstractList<Object> {
34      /** the array to wrap. */
35      private final Object array;
36  
37      /**
38       * Create the wrapper.
39       * @param anArray {@link #array}
40       */
41      public ArrayListWrapper(Object anArray) {
42          if (!anArray.getClass().isArray()) {
43              throw new IllegalArgumentException(anArray.getClass() + " is not an array");
44          }
45          this.array = anArray;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public Object get(int index) {
51          return Array.get(array, index);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public Object set(int index, Object element) {
57          Object old = get(index);
58          Array.set(array, index, element);
59          return old;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public int size() {
65          return Array.getLength(array);
66      }
67  
68      @Override
69      public Object[] toArray() {
70          final int size = size();
71          Object[] a = new Object[size];
72          for(int i = 0; i < size; ++i) {
73              a[i] = get(i);
74          }
75          return a;
76      }
77  
78      @Override
79      @SuppressWarnings("unchecked")
80      public <T> T[] toArray(T[] a) {
81          int size = size();
82          if (a.length < size) {
83              T[] x = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
84              System.arraycopy(a, a.length, x, 0, a.length);
85          }
86          for(int i = 0; i < size; ++i) {
87              a[i] = (T) get(i);
88          }
89          if (a.length > size) {
90              a[size] = null;
91          }
92          return a;
93      }
94  
95      @Override
96      public int indexOf(Object o) {
97          final int size = size();
98          if (o == null) {
99              for (int i = 0; i < size; i++) {
100                 if (get(i) == null) {
101                     return i;
102                 }
103             }
104         } else {
105             for (int i = 0; i < size; i++) {
106                 if (o.equals(get(i))) {
107                     return i;
108                 }
109             }
110         }
111         return -1;
112     }
113 
114     @Override
115     public boolean contains(Object o) {
116         return indexOf(o) != -1;
117     }
118     
119     @Override
120     public boolean isEmpty() {
121         return super.isEmpty();
122     }
123 
124     @Override
125     public Iterator<Object> iterator() {
126         return super.iterator();
127     }
128     
129     @Override
130     public boolean containsAll(Collection<?> c) {
131         return super.containsAll(c);
132     }
133 
134     @Override
135     public int lastIndexOf(Object o) {
136         return super.lastIndexOf(o);
137     }
138 
139     @Override
140     public ListIterator<Object> listIterator() {
141         return super.listIterator();
142     }
143 
144     @Override
145     public ListIterator<Object> listIterator(int index) {
146         return super.listIterator(index);
147     }
148 
149     @Override
150     public List<Object> subList(int fromIndex, int toIndex) {
151         return super.subList(fromIndex, toIndex);
152     }
153     
154     @Override
155     public boolean add(Object o) {
156         throw new UnsupportedOperationException("Not supported.");
157     }
158 
159     @Override
160     public boolean remove(Object o) {
161         throw new UnsupportedOperationException("Not supported.");
162     }
163 
164     @Override
165     public boolean addAll(Collection<? extends Object> c) {
166         throw new UnsupportedOperationException("Not supported.");
167     }
168 
169     @Override
170     public boolean addAll(int index, Collection<? extends Object> c) {
171         throw new UnsupportedOperationException("Not supported.");
172     }
173 
174     @Override
175     public boolean removeAll(Collection<?> c) {
176         throw new UnsupportedOperationException("Not supported.");
177     }
178 
179     @Override
180     public boolean retainAll(Collection<?> c) {
181         throw new UnsupportedOperationException("Not supported.");
182     }
183 
184     @Override
185     public void clear() {
186         throw new UnsupportedOperationException("Not supported.");
187     }
188 
189     @Override
190     public void add(int index, Object element) {
191         throw new UnsupportedOperationException("Not supported.");
192     }
193 
194     @Override
195     public Object remove(int index) {
196         throw new UnsupportedOperationException("Not supported.");
197     }
198 
199 }