SeqList.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * 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  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.util;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.io.Serializable;
025 import java.util.AbstractList;
026 import java.util.RandomAccess;
027 
028 /**
029  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
030  * @version 3.4
031  * @since 3.4
032  */
033 class SeqList<T>
034     extends AbstractList<T>
035     implements
036     RandomAccess,
037     Serializable
038 {
039     private static final long serialVersionUID = 1L;
040 
041     public final Seq<T> seq;
042 
043     public SeqList(final Seq<T> seq) {
044         this.seq = requireNonNull(seq, "Seq must not be null.");
045     }
046 
047     @Override
048     public T get(final int index) {
049         return seq.get(index);
050     }
051 
052     @Override
053     public int size() {
054         return seq.length();
055     }
056 
057     @Override
058     public int indexOf(final Object element) {
059         int index = -1;
060         if (element == null) {
061             for (int i = 0; i < seq.length() && index == -1; ++i) {
062                 if (seq.get(i) == null) {
063                     index = i;
064                 }
065             }
066         } else {
067             for (int i = 0; i < seq.length() && index == -1; ++i) {
068                 if (element.equals(seq.get(i))) {
069                     index = i;
070                 }
071             }
072         }
073 
074         return index;
075     }
076 
077     @Override
078     public int lastIndexOf(final Object element) {
079         int index = -1;
080         if (element == null) {
081             for (int i = seq.length(); --i >= 0 && index == -1;) {
082                 if (seq.get(i) == null) {
083                     index = i;
084                 }
085             }
086         } else {
087             for (int i = seq.length(); --i >= 0 && index == -1;) {
088                 if (element.equals(seq.get(i))) {
089                     index = i;
090                 }
091             }
092         }
093 
094         return index;
095     }
096 
097     @Override
098     public boolean contains(final Object element) {
099         return indexOf(element) != -1;
100     }
101 
102     @Override
103     public Object[] toArray() {
104         final Object[] array = new Object[size()];
105         for (int i = size(); --i >= 0;) {
106             array[i] = this.seq.get(i);
107         }
108         return array;
109     }
110 
111     @SuppressWarnings("unchecked")
112     @Override
113     public <E> E[] toArray(final E[] array) {
114         if (array.length < size()) {
115             final E[] copy = (E[])java.lang.reflect.Array.newInstance(
116                 array.getClass().getComponentType(), size()
117             );
118             for (int i = size(); --i >= 0;) {
119                 copy[i] = (E) this.seq.get(i);
120             }
121 
122             return copy;
123         }
124 
125         for (int i = size(); --i >= 0;) {
126             array[i] = (E) this.seq.get(i);
127         }
128         return array;
129     }
130 
131 }