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

com.jongsoft.lang.collection.List Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2016 Jong Soft.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jongsoft.lang.collection;

import com.jongsoft.lang.common.Streamable;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public interface List extends Iterable, Streamable {

    /**
     * Add an element to the end of the contents of the current list and return the result in a new {@link List} instance
     *
     * @param value     the value to append to the list
     * @return          the new list with the value appended
     */
    default List add(T value) {
        return insert(size(), value);
    }

    /**
     * Add an element to the list at the provided index, shifting all elements after the index one.
     *
     * @param index the index at which to insert the element
     * @param value the element to insert
     * @return      the updated list with the inserted element
     */
    List insert(int index, T value);

    /**
     * Find the index for the provided element, will return -1 if the element
     * is not present in the list.
     *
     * @param lookFor   the element to look for
     * @return  the index of the element, or -1 if none found
     */
    int indexOf(Object lookFor);

    /**
     * Removes an element from the list and returns a new instance of the list.
     *
     * @param index     the index of the element to be removed
     * @return          the new instance of the list without the element at the provided index
     * @throws IndexOutOfBoundsException    in case the index is not between the 0 and list size
     */
    List remove(int index);

    /**
     * Removes the first element found matching the provided value. The match is done based upon the
     * {@link java.util.Objects#equals(Object, Object)} call.
     *
     * @param value the element to be removed
     * @return the current list if the element is not present, otherwise a new list instance without the element in it.
     */
    default List remove(T value) {
        int idx = indexOf(value);
        if (idx > -1) {
            return remove(idx);
        }

        return this;
    }

    /**
     * Attempts to look in the list if a value is present or not. This method will use the {@link Object#equals(Object)}
     * to determine equality.
     *
     * @param lookFor   the object to look for in the list
     * @return          true if the object exists in the list
     */
    default boolean contains(Object lookFor) {
        Iterator iterator = iterator();
        while (iterator.hasNext()) {
            if ((lookFor == null && iterator.next() == null) || (lookFor != null &&lookFor.equals(iterator.next()))) {
                return true;
            }
        }
        return false;
    }

    /**
     * Provides the amount of entries present in the list
     *
     * @return 0 if empty, otherwise the amount of entries
     */
    int size();

    /**
     * Convenience method to see if the current list is empty or not.
     *
     * @return true if the list contains elements, otherwise false
     */
    default boolean isEmpty() {
        return size() > 0;
    }

    /**
     * Get the element at the location of index
     *
     * @param index     the index of the element in the list to get
     * @return          the element at the provided index
     * @throws IndexOutOfBoundsException    in case the index provided is greater then the {@link #size()} - 1.
     */
    T get(int index) throws IndexOutOfBoundsException;

    @Override
    default Stream stream() {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.CONCURRENT), false);
    }

    /**
     * Filter the list contents with the provided predicate. Only returning those elements that match the predicate.
     *
     * @param predicate the predicate to use in the filter operation
     * @return          the filtered list
     */
    List filter(Predicate predicate);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy