All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jenetics.util.SeqView Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-7.1.2).
 * Copyright (c) 2007-2023 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package io.jenetics.util;

import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.function.Function;

/**
 * Seq view of a given list. The content is not copied on creation.
 *
 * @author Franz Wilhelmstötter
 * @version 4.2
 * @since 4.2
 */
final class SeqView implements Seq {

	private final List _list;

	SeqView(final List list) {
		_list = requireNonNull(list);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List asList() {
		return (List)_list;
	}

	@Override
	public T get(final int index) {
		return _list.get(index);
	}

	@Override
	public int length() {
		return _list.size();
	}

	@Override
	public Seq subSeq(final int start, final int end) {
		return new SeqView<>(_list.subList(start, end));
	}

	@Override
	public Seq subSeq(final int start) {
		return new SeqView<>(_list.subList(start, _list.size()));
	}

	@Override
	public  Seq map(final Function mapper) {
		requireNonNull(mapper);

		final MSeq result = MSeq.ofLength(length());
		for (int i = 0; i < length(); ++i) {
			result.set(i, mapper.apply(get(i)));
		}

		return result.toISeq();
	}

	@Override
	public Seq append(final Iterable values) {
		requireNonNull(values);
		return ISeq.of(_list).append(values);
	}

	@Override
	public Seq prepend(final Iterable values) {
		requireNonNull(values);
		return ISeq.of(_list).prepend(values);
	}

	@Override
	public Object[] toArray() {
		return _list.toArray();
	}

	@Override
	public  B[] toArray(final B[] array) {
		return _list.toArray(array);
	}

}