ArraySeqList.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-1.6.0).
03  * Copyright (c) 2007-2014 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      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  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.util;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import java.util.AbstractList;
25 import java.util.RandomAccess;
26 
27 /**
28  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
29  @since 1.0
30  @version 1.0 &mdash; <em>$Date: 2014-02-15 $</em>
31  */
32 class ArraySeqList<T> extends AbstractList<T>
33     implements RandomAccess
34 {
35     final ArraySeq<T> _array;
36 
37     public ArraySeqList(final ArraySeq<T> array) {
38         _array = requireNonNull(array, "ArrayBase");
39     }
40 
41     @Override
42     public T get(final int index) {
43         return _array.get(index);
44     }
45 
46     @Override
47     public int size() {
48         return _array.length();
49     }
50 
51     @Override
52     public int indexOf(final Object element) {
53         return _array.indexOf(element);
54     }
55 
56     @Override
57     public boolean contains(final Object element) {
58         return indexOf(element!= -1;
59     }
60 
61     @Override
62     public Object[] toArray() {
63         return _array.toArray();
64     }
65 
66     @SuppressWarnings("unchecked")
67     @Override
68     public <E> E[] toArray(final E[] array) {
69         if (array.length < _array.length()) {
70             final E[] copy = (E[])java.lang.reflect.Array.newInstance(
71                     array.getClass().getComponentType(), _array.length()
72                 );
73             for (int i = 0; i < _array.length(); ++i) {
74                 copy[i(E)_array.get(i);
75             }
76 
77             return copy;
78         }
79 
80         System.arraycopy(_array._array, _array._start, array, 0, array.length);
81         return array;
82     }
83 
84 }