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

it.tidalwave.util.CollectionUtils Maven / Gradle / Ivy

/*
 * *********************************************************************************************************************
 *
 * TheseFoolishThings: Miscellaneous utilities
 * http://tidalwave.it/projects/thesefoolishthings
 *
 * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.util;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import static java.util.Collections.emptyList;

/***********************************************************************************************************************
 *
 * This class contains a bunch of utility methods for manipulating lists.
 *
 * @author  Fabrizio Giudici
 * @since   3.2-ALPHA-13
 * @it.tidalwave.javadoc.stable
 *
 **********************************************************************************************************************/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CollectionUtils
  {
    /*******************************************************************************************************************
     *
     * Appends a list to an object. The resulting list is mutable.
     *
     * @param            the type of list items
     * @param     list      the list
     * @param     object    the list to append
     * @return              the list with the appended object
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List concat (@Nonnull final List list, @Nonnull final T object)
      {
        final List result = new ArrayList<>(list);
        result.add(object);
        return result;
      }

    /*******************************************************************************************************************
     *
     * Returns a concatenation of the given {@link Collection}s.
     *
     * @param              the static type
     * @param     collections the input collections
     * @return                the concatenation
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List concatAll (@Nonnull final Collection... collections)
      {
        final List result = new ArrayList<>();

        for (final var collection : collections)
          {
            result.addAll(collection);
          }

        return result;
      }

    /*******************************************************************************************************************
     *
     * Reverses a list. The resulting list is mutable.
     *
     * @param            the type of list items
     * @param     list      the list
     * @return              the reversed list
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List reversed (@Nonnull final List list)
      {
        final List result = new ArrayList<>(list);
        Collections.reverse(result);
        return result;
      }

    /*******************************************************************************************************************
     *
     * Sorts a list. The resulting list is mutable.
     *
     * @param            the type of list items
     * @param     list      the list
     * @return              the sorted list
     * @since     3.2-ALPHA-13
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static > List sorted (@Nonnull final List list)
      {
        final var result = new ArrayList(list);
        Collections.sort(result);
        return result;
      }

    /*******************************************************************************************************************
     *
     * Sorts a list with a given {@link Comparator}. The resulting list is mutable.
     *
     * @param              the type of list items
     * @param     list        the list
     * @param     comparator  the comparator
     * @return                the sorted list
     * @since     3.2-ALPHA-13
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List sorted (@Nonnull final List list,
                                      @Nonnull final Comparator comparator)
      {
        final var result = new ArrayList(list);
        result.sort(comparator);
        return result;
      }

    /*******************************************************************************************************************
     *
     * Returns the (optional) first element of a list.
     *
     * @param            the type of list items
     * @param     list      the list
     * @return              the first element
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  Optional optionalHead (@Nonnull final List list)
      {
        return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
      }

    /*******************************************************************************************************************
     *
     * Returns the first element of a list.
     *
     * @param            the type of list items
     * @param     list      the list (cannot be empty)
     * @return              the first element
     * @throws    IllegalArgumentException  if the list is empty
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  T head (@Nonnull final List list)
      {
        if (list.isEmpty())
          {
            throw new IllegalArgumentException("List is empty");
          }

        return list.get(0);
      }

    /*******************************************************************************************************************
     *
     * Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an
     * empty list. The resulting list is mutable.
     *
     * @param            the type of list items
     * @param     list      the list
     * @return              the tail of the list
     *
     * @it.tidalwave.javadoc.stable
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List tail (@Nonnull final List list)
      {
        return new ArrayList<>(list.subList(1, list.size()));
      }

    /*******************************************************************************************************************
     *
     * Return a sublist of the original {@link List}, from the given {@code from} and {@code to} index (not included).
     * If the {@code from} index is negative and/or the {@code to} index is lower than the {@code from} index or if an
     * attempt is made to read before the start or past the end of the list, truncation silently occurs.
     *
     * @param                  the static type
     * @param     list            the original list
     * @param     from            the first index (included)
     * @param     to              the last index (excluded)
     * @return                    the sublist
     * @since     3.2-ALPHA-17
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List safeSubList (@Nonnull final List list, final int from, final int to)
      {
        final var safeFrom = Math.max(from, 0);
        final var safeTo = Math.min(list.size(), to);
        return (safeFrom >= safeTo) ? emptyList() : new ArrayList<>(list.subList(safeFrom, safeTo));
      }

    /*******************************************************************************************************************
     *
     * Splits a given {@link List} at a set of boundaries. Each boundary is the starting point of a sublist to be
     * returned.
     *
     * @param                  the static type
     * @param     list            the original list
     * @param     boundaries      the boundaries
     * @return                    a list of sublists
     * @since     3.2-ALPHA-17
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  List> split (@Nonnull final List list, final int ... boundaries)
      {
        final var result = new ArrayList>();

        for (var i = 0; i < boundaries.length - 1; i++)
          {
            result.add(safeSubList(list, boundaries[i], boundaries[i + 1]));
          }

        return result;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy