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

org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection Maven / Gradle / Ivy

/*
 * @(#)ReadOnlySequencedCollection.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */

package org.jhotdraw8.icollection.readonly;

import org.jhotdraw8.icollection.facade.SequencedCollectionFacade;

import java.util.SequencedCollection;

/**
 * A read-only interface to a sequenced collection.
 * A sequenced collection has a well-defined encounter order,
 * that supports operations at both ends, and that is reversible.
 *
 * @param  the element type
 */
public interface ReadOnlySequencedCollection extends ReadOnlyCollection {
    /**
     * Gets the first element.
     *
     * @return an element
     * @throws java.util.NoSuchElementException if the collection is empty
     */
    default E getFirst() {
        return iterator().next();
    }

    /**
     * Gets the last element.
     *
     * @return an element
     * @throws java.util.NoSuchElementException if the collection is empty
     */
    default E getLast() {
        return readOnlyReversed().iterator().next();
    }


    /**
     * Returns a reversed-order view of this collection.
     * Changes to the underlying collection are visible in the reversed view.
     *
     * @return a reversed-order view of this collection
     */
    ReadOnlySequencedCollection readOnlyReversed();

    @Override
    default SequencedCollection asCollection() {
        return new SequencedCollectionFacade<>(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy