001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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 */ 020package io.jenetics.internal.collection; 021 022import static java.util.Objects.requireNonNull; 023 024import java.io.Serial; 025import java.io.Serializable; 026import java.util.RandomAccess; 027import java.util.function.Consumer; 028import java.util.function.Predicate; 029 030import io.jenetics.util.Seq; 031 032/** 033 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 034 * @since 1.4 035 * @version 5.2 036 */ 037public abstract class ArraySeq<T> 038 implements 039 Seq<T>, 040 RandomAccess, 041 Serializable 042{ 043 @Serial 044 private static final long serialVersionUID = 1L; 045 046 public final Array<T> array; 047 048 public ArraySeq(final Array<T> array) { 049 this.array = requireNonNull(array, "Array must not be null."); 050 } 051 052 @Override 053 public final T get(final int index) { 054 array.checkIndex(index); 055 return array.get(index); 056 } 057 058 @SuppressWarnings("unchecked") 059 final Array<T> __append(final Iterable<? extends T> values) { 060 requireNonNull(values); 061 return values instanceof ArraySeq 062 ? array.append(((ArraySeq<T>)values).array) 063 : array.append(values); 064 } 065 066 @SuppressWarnings("unchecked") 067 final Array<T> __prepend(final Iterable<? extends T> values) { 068 requireNonNull(values); 069 return values instanceof ArraySeq 070 ? ((ArraySeq<T>)values).array.append(array) 071 : array.prepend(values); 072 } 073 074 @Override 075 public void forEach(final Consumer<? super T> consumer) { 076 requireNonNull(consumer, "The consumer must not be null."); 077 078 for (int i = 0; i < array.length(); ++i) { 079 consumer.accept(array.get(i)); 080 } 081 } 082 083 @Override 084 public boolean forAll(final Predicate<? super T> predicate) { 085 requireNonNull(predicate, "Predicate"); 086 for (T element : this) { 087 if (!predicate.test(element)) return false; 088 } 089 return true; 090 } 091 092 @Override 093 public int indexWhere( 094 final Predicate<? super T> predicate, 095 final int start, 096 final int end 097 ) { 098 array.checkIndex(start, end); 099 requireNonNull(predicate, "Predicate"); 100 101 int index = -1; 102 103 for (int i = start; i < end && index == -1; ++i) { 104 if (predicate.test(array.get(i))) { 105 index = i; 106 } 107 } 108 109 return index; 110 } 111 112 @Override 113 public int lastIndexWhere( 114 final Predicate<? super T> predicate, 115 final int start, 116 final int end 117 ) { 118 array.checkIndex(start, end); 119 requireNonNull(predicate, "Predicate must not be null."); 120 121 int index = -1; 122 123 for (int i = end; --i >= start && index == -1;) { 124 if (predicate.test(array.get(i))) { 125 index = i; 126 } 127 } 128 129 return index; 130 } 131 132 @Override 133 public int length() { 134 return array.length(); 135 } 136 137 @Override 138 public String toString() { 139 return toString("[", ",", "]"); 140 } 141 142 @Override 143 public int hashCode() { 144 return Seq.hashCode(this); 145 } 146 147 @Override 148 public boolean equals(final Object obj) { 149 return obj == this || 150 obj instanceof Seq<?> other && 151 Seq.equals(this, other); 152 } 153 154}