001 /*
002 * Java Genetic Algorithm Library (jenetics-6.0.0).
003 * Copyright (c) 2007-2020 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@gmail.com)
019 */
020 package io.jenetics.util;
021
022 import java.util.Iterator;
023 import java.util.ListIterator;
024 import java.util.RandomAccess;
025 import java.util.Spliterator;
026 import java.util.stream.Stream;
027 import java.util.stream.StreamSupport;
028
029 import io.jenetics.internal.collection.BaseSeqIterator;
030 import io.jenetics.internal.collection.BaseSeqSpliterator;
031
032 /**
033 * General base interface for a ordered, fixed sized, object sequence.
034 *
035 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
036 * @since 5.2
037 * @version 5.2
038 */
039 public interface BaseSeq<T> extends Iterable<T>, RandomAccess {
040
041 /**
042 * Return the value at the given {@code index}.
043 *
044 * @param index index of the element to return.
045 * @return the value at the given {@code index}.
046 * @throws IndexOutOfBoundsException if the index is out of range
047 * (index < 0 || index >= size()).
048 */
049 T get(final int index);
050
051 /**
052 * Return the length of this sequence. Once the sequence is created, the
053 * length can't be changed.
054 *
055 * @return the length of this sequence.
056 */
057 int length();
058
059 /**
060 * Returns {@code true} if this sequence contains no elements.
061 *
062 * @since 6.0
063 *
064 * @return {@code true} if this sequence contains no elements
065 */
066 default boolean isEmpty() {
067 return length() == 0;
068 }
069
070 /**
071 * Returns {@code true} if this sequence contains at least one element.
072 *
073 * @since 6.0
074 *
075 * @return {@code true} if this sequence contains at least one element
076 */
077 default boolean nonEmpty() {
078 return !isEmpty();
079 }
080
081 @Override
082 default Iterator<T> iterator() {
083 return listIterator();
084 }
085
086 /**
087 * Returns a list iterator over the elements in this sequence (in proper
088 * order).
089 *
090 * @return a list iterator over the elements in this sequence (in proper
091 * order)
092 */
093 default ListIterator<T> listIterator() {
094 return new BaseSeqIterator<>(this);
095 }
096
097 /**
098 * Returns a sequential Stream with this sequence as its source.
099 *
100 * @return a sequential Stream over the elements in this sequence
101 */
102 default Stream<T> stream() {
103 return StreamSupport.stream(spliterator(), false);
104 }
105
106 @Override
107 default Spliterator<T> spliterator() {
108 return new BaseSeqSpliterator<>(this);
109 }
110
111 }
|