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.lang.String.format; 023 024import java.io.Serial; 025import java.util.function.Function; 026 027import io.jenetics.util.ISeq; 028import io.jenetics.util.MSeq; 029 030/** 031 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 032 * @since 1.4 033 * @version 3.4 034 */ 035public class ArrayISeq<T> extends ArraySeq<T> implements ISeq<T> { 036 037 @Serial 038 private static final long serialVersionUID = 1L; 039 040 public ArrayISeq(final Array<T> array) { 041 super(array); 042 assert array.isSealed(); 043 } 044 045 @Override 046 public <B> ISeq<B> map(final Function<? super T, ? extends B> mapper) { 047 final Array<B> mapped = Array.ofLength(length()); 048 for (int i = 0; i < length(); ++i) { 049 mapped.set(i, mapper.apply(array.get(i))); 050 } 051 return new ArrayISeq<>(mapped.seal()); 052 } 053 054 @Override 055 public ISeq<T> append(final Iterable<? extends T> values) { 056 return new ArrayISeq<>(__append(values).seal()); 057 } 058 059 @Override 060 public ISeq<T> prepend(final Iterable<? extends T> values) { 061 return new ArrayISeq<>(__prepend(values).seal()); 062 } 063 064 @Override 065 public ISeq<T> subSeq(final int start) { 066 if (start < 0 || start > length()) { 067 throw new ArrayIndexOutOfBoundsException(format( 068 "Index %d range: [%d..%d)", start, 0, length() 069 )); 070 } 071 072 return start == length() 073 ? Empty.iseq() 074 : new ArrayISeq<>(array.slice(start, length())); 075 } 076 077 @Override 078 public ISeq<T> subSeq(int start, int end) { 079 if (start > end) { 080 throw new ArrayIndexOutOfBoundsException(format( 081 "start[%d] > end[%d]", start, end 082 )); 083 } 084 if (start < 0 || end > length()) { 085 throw new ArrayIndexOutOfBoundsException(format( 086 "Indexes (%d, %d) range: [%d..%d)", start, end, 0, length() 087 )); 088 } 089 090 return start == end 091 ? Empty.iseq() 092 : new ArrayISeq<>(array.slice(start, end)); 093 } 094 095 @Override 096 public MSeq<T> copy() { 097 return isEmpty() 098 ? Empty.mseq() 099 : new ArrayMSeq<>(array.copy()); 100 } 101 102}