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

io.github.selcukes.collections.Lists Maven / Gradle / Ivy

There is a newer version: 2.3.12
Show newest version
/*
 *  Copyright (c) Ramesh Babu Prudhvi.
 *
 *  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.
 */

package io.github.selcukes.collections;

import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@UtilityClass
public class Lists {

    /**
     * Returns a new {@code LinkedList} instance containing the specified
     * elements in the same order as they appear in the input array. This method
     * is annotated with {@code @SafeVarargs} to suppress unchecked warnings
     * that would otherwise occur due to the use of a varargs parameter.
     *
     * @param        the type of elements in the list
     * @param  elements the elements to be added to the list
     * @return          a new LinkedList containing the specified elements
     */
    @SafeVarargs
    public  List of(E... elements) {
        return new LinkedList<>(Arrays.asList(elements));
    }

    /**
     * Returns a list of non-empty values from a stream by applying a mapper
     * function to each element.
     *
     * @param  stream the stream of elements to be mapped and filtered
     * @param  mapper the function to apply to each element in the stream
     * @param      the type of the input elements in the stream
     * @param      the type of the output elements in the resulting list
     * @return        a list of non-empty values produced by applying the mapper
     *                function to the elements of the input stream
     */
    public  List of(Stream stream, Function mapper) {
        return stream.parallel()
                .map(mapper)
                .filter(Lists::nonEmpty)
                .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Checks if a given value is non-empty.
     *
     * @param  value the value to be checked for emptiness
     * @param     the type of the input value
     * @return       true if the value is not empty, false otherwise
     */
    public  boolean nonEmpty(T value) {
        if (value instanceof Collection collection) {
            return !collection.isEmpty();
        }
        if (value instanceof Object[] objects) {
            return objects.length > 0;
        }
        return StringHelper.isNonEmpty(value.toString());
    }

    /**
     * Returns a new case-insensitive list containing the specified elements.
     *
     * @param  elements the elements to include in the list
     * @return          a new case-insensitive list containing the specified
     *                  elements
     */
    public List ofIgnoreCase(String... elements) {
        var stringList = of(elements);
        return new CaseInsensitiveList(stringList);
    }

    /**
     * Returns a new list containing only the elements of the given list that
     * match the specified predicate.
     *
     * @param                    the type of elements in the list
     * @param  list                 the list to be filtered
     * @param  predicate            the predicate used to test elements for
     *                              inclusion
     * @return                      a new list containing only the matching
     *                              elements
     * @throws NullPointerException if the given list is {@code null}.
     */
    public  List retainIf(@NonNull List list, Predicate predicate) {
        return list.stream()
                .filter(predicate)
                .toList();
    }

    /**
     * Sorts a given list of elements with nulls appearing last.
     *
     * @param  list the list of elements to be sorted
     * @param    the type of elements in the list
     * @return      a new sorted list with nulls placed at the end
     */
    public  List sortWithNulls(List list) {
        return list.stream()
                .sorted(Comparator.nullsLast(Comparator.comparing(Object::toString, Comparator.naturalOrder())))
                .toList();
    }

    /**
     * Returns a list of string objects in the given list.
     *
     * @param  list the list of objects to convert to strings
     * @param    the type of object in the list
     * @return      a list of string objects in the given list.
     */
    public  List toString(@NonNull List list) {
        return list.stream()
                .map(Objects::toString)
                .toList();
    }

    @NoArgsConstructor
    private static class CaseInsensitiveList extends ArrayList {

        public CaseInsensitiveList(final Collection collection) {
            super(collection);
        }

        @Override
        public boolean contains(final Object o) {
            final String s = (String) o;
            return this.stream().anyMatch(str -> str.equalsIgnoreCase(s));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy