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

net.lenni0451.commons.collections.Lists Maven / Gradle / Ivy

The newest version!
package net.lenni0451.commons.collections;

import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Supplier;

@UtilityClass
public class Lists {

    /**
     * Move the given item to the bottom of the list.
     *
     * @param list  The list to move the item in
     * @param input The item to move
     * @param    The item type
     * @return The list
     */
    public static  List moveToBottom(final List list, final T input) {
        if (list.size() > 1 && list.indexOf(input) < list.size() - 1) {
            list.remove(input);
            list.add(list.size(), input);
        }
        return list;
    }

    /**
     * Merge multiple lists into one.
* The lists are not modified. * * @param list The first list * @param others The other lists * @param The list type * @return The merged list */ @SafeVarargs public static List merge(final List list, final List... others) { List newList = new ArrayList<>(list); for (List other : others) newList.addAll(other); return newList; } /** * Create a new list with the given objects. * * @param listSupplier The supplier for the list * @param objects The objects to add to the list * @param The list type * @param The object type * @return The created list */ @SafeVarargs public static , O> T any(final Supplier listSupplier, final O... objects) { T list = listSupplier.get(); Collections.addAll(list, objects); return list; } /** * Create a new list which is passed to the given consumer. * * @param listSupplier The supplier to create the list * @param listConsumer The consumer to pass the list to * @param The list type * @param The object type * @return The created list */ public static , O> T any(final Supplier listSupplier, final Consumer listConsumer) { T list = listSupplier.get(); listConsumer.accept(list); return list; } /** * Create a new {@link ArrayList} with the given objects. * * @param objects The objects to add to the list * @param The object type * @return The created list */ @SafeVarargs public static ArrayList arrayList(final O... objects) { return any(ArrayList::new, objects); } /** * Create a new {@link ArrayList} which is passed to the given consumer. * * @param listConsumer The consumer to pass the list to * @param The object type * @return The created list */ public static ArrayList arrayList(final Consumer> listConsumer) { return any(ArrayList::new, listConsumer); } /** * Create a new {@link CopyOnWriteArrayList} with the given objects. * * @param objects The objects to add to the list * @param The object type * @return The created list */ @SafeVarargs public static CopyOnWriteArrayList copyOnWriteArrayList(final O... objects) { return any(CopyOnWriteArrayList::new, objects); } /** * Create a new {@link CopyOnWriteArrayList} which is passed to the given consumer. * * @param listConsumer The consumer to pass the list to * @param The object type * @return The created list */ public static CopyOnWriteArrayList copyOnWriteArrayList(final Consumer> listConsumer) { return any(CopyOnWriteArrayList::new, listConsumer); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy