ArrayISeq.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.5.0).
03  * Copyright (c) 2007-2016 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.internal.collection;
21 
22 import static java.lang.String.format;
23 
24 import java.util.function.Function;
25 
26 import org.jenetics.util.ISeq;
27 import org.jenetics.util.MSeq;
28 
29 /**
30  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
31  * @since 1.4
32  * @version 3.4
33  */
34 public class ArrayISeq<T> extends ArraySeq<T> implements ISeq<T> {
35     private static final long serialVersionUID = 1L;
36 
37     public ArrayISeq(final Array<T> array) {
38         super(array);
39         assert array.isSealed();
40     }
41 
42     @Override
43     public <B> ISeq<B> map(final Function<? super T, ? extends B> mapper) {
44         final Array<B> mapped = Array.ofLength(length());
45         for (int i = 0; i < length(); ++i) {
46             mapped.set(i, mapper.apply(array.get(i)));
47         }
48         return new ArrayISeq<>(mapped.seal());
49     }
50 
51     @Override
52     public ISeq<T> append(final Iterable<? extends T> values) {
53         return new ArrayISeq<>(__append(values).seal());
54     }
55 
56     @Override
57     public ISeq<T> prepend(final Iterable<? extends T> values) {
58         return new ArrayISeq<>(__prepend(values).seal());
59     }
60 
61     @Override
62     public ISeq<T> subSeq(final int start) {
63         if (start < 0 || start > length()) {
64             throw new ArrayIndexOutOfBoundsException(format(
65                 "Index %d range: [%d..%d)", start, 0, length()
66             ));
67         }
68 
69         return start == length()
70             ? Empty.iseq()
71             : new ArrayISeq<>(array.slice(start, length()));
72     }
73 
74     @Override
75     public ISeq<T> subSeq(int start, int end) {
76         if (start > end) {
77             throw new ArrayIndexOutOfBoundsException(format(
78                 "start[%d] > end[%d]", start, end
79             ));
80         }
81         if (start < 0 || end > length()) {
82             throw new ArrayIndexOutOfBoundsException(format(
83                 "Indexes (%d, %d) range: [%d..%d)", start, end, 0, length()
84             ));
85         }
86 
87         return start == end
88             ? Empty.iseq()
89             : new ArrayISeq<>(array.slice(start, end));
90     }
91 
92     @Override
93     public MSeq<T> copy() {
94         return isEmpty()
95             ? Empty.mseq()
96             : new ArrayMSeq<>(array.copy());
97     }
98 
99 }