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

org.jhotdraw8.icollection.facade.ReadOnlyListFacade Maven / Gradle / Ivy

The newest version!
/*
 * @(#)ReadOnlyListFacade.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.icollection.facade;

import org.jhotdraw8.icollection.readonly.AbstractReadOnlyList;
import org.jhotdraw8.icollection.readonly.ReadOnlyList;
import org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection;

import java.util.List;
import java.util.Objects;
import java.util.function.IntFunction;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
 * Provides a {@link ReadOnlyList} facade to a set of {@code ReadOnlyList} functions.
 *
 * @param  the element type
 * @author Werner Randelshofer
 */
public class ReadOnlyListFacade extends AbstractReadOnlyList {
    private final IntSupplier sizeFunction;
    private final IntFunction getFunction;
    private final Supplier> readOnlyReversedFunction;

    public ReadOnlyListFacade(List backingList) {
        this.sizeFunction = backingList::size;
        this.getFunction = backingList::get;
        this.readOnlyReversedFunction = () -> new ReadOnlyListFacade<>(
                sizeFunction,
                index -> getFunction.apply(sizeFunction.getAsInt() - index),
                () -> this);
    }

    public ReadOnlyListFacade(IntSupplier sizeFunction, IntFunction getFunction) {
        this.sizeFunction = sizeFunction;
        this.getFunction = getFunction;
        this.readOnlyReversedFunction = () -> new ReadOnlyListFacade<>(
                sizeFunction,
                index -> getFunction.apply(sizeFunction.getAsInt() - index),
                () -> this);
    }

    public ReadOnlyListFacade(IntSupplier sizeFunction, IntFunction getFunction, Supplier> readOnlyReversedFunction) {
        this.sizeFunction = sizeFunction;
        this.getFunction = getFunction;
        this.readOnlyReversedFunction = readOnlyReversedFunction;
    }

    @Override
    public E get(int index) {
        return getFunction.apply(index);
    }

    @Override
    public ReadOnlySequencedCollection readOnlyReversed() {
        return readOnlyReversedFunction.get();
    }

    @Override
    public ReadOnlyList readOnlySubList(int fromIndex, int toIndex) {
        int length = size();
        Objects.checkFromToIndex(fromIndex, toIndex, length);
        return new ReadOnlyListFacade<>(
                () -> toIndex - fromIndex,
                i -> getFunction.apply(i - fromIndex),
                readOnlyReversedFunction);
    }

    @Override
    public int size() {
        return sizeFunction.getAsInt();
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy