BaseSeq.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-5.2.0).
03  * Copyright (c) 2007-2020 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@gmail.com)
19  */
20 package io.jenetics.util;
21 
22 import java.util.Iterator;
23 import java.util.ListIterator;
24 import java.util.RandomAccess;
25 import java.util.Spliterator;
26 import java.util.stream.Stream;
27 import java.util.stream.StreamSupport;
28 
29 import io.jenetics.internal.collection.BaseSeqIterator;
30 import io.jenetics.internal.collection.BaseSeqSpliterator;
31 
32 /**
33  * General base interface for a ordered, fixed sized, object sequence.
34  *
35  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
36  @since 5.2
37  @version 5.2
38  */
39 public interface BaseSeq<T> extends Iterable<T>, RandomAccess {
40 
41     /**
42      * Return the value at the given {@code index}.
43      *
44      @param index index of the element to return.
45      @return the value at the given {@code index}.
46      @throws IndexOutOfBoundsException if the index is out of range
47      *         (index &lt; 0 || index &gt;= size()).
48      */
49     T get(final int index);
50 
51     /**
52      * Return the length of this sequence. Once the sequence is created, the
53      * length can't be changed.
54      *
55      @return the length of this sequence.
56      */
57     int length();
58 
59     @Override
60     public default Iterator<T> iterator() {
61         return listIterator();
62     }
63 
64     /**
65      * Returns a list iterator over the elements in this sequence (in proper
66      * order).
67      *
68      @return a list iterator over the elements in this sequence (in proper
69      *         order)
70      */
71     default ListIterator<T> listIterator() {
72         return new BaseSeqIterator<>(this);
73     }
74 
75     /**
76      * Returns a sequential Stream with this sequence as its source.
77      *
78      @return a sequential Stream over the elements in this sequence
79      */
80     default Stream<T> stream() {
81         return StreamSupport.stream(spliterator()false);
82     }
83 
84     @Override
85     default Spliterator<T> spliterator() {
86         return new BaseSeqSpliterator<>(this);
87     }
88 
89 }