CollectionUtils.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.Comparator;
  32. import java.util.List;
  33. import java.util.Optional;
  34. import lombok.AccessLevel;
  35. import lombok.NoArgsConstructor;

  36. /***********************************************************************************************************************
  37.  *
  38.  * This class contains a bunch of utility methods for manipulating lists.
  39.  *
  40.  * @author  Fabrizio Giudici
  41.  * @since   3.2-ALPHA-13
  42.  * @it.tidalwave.javadoc.stable
  43.  *
  44.  **********************************************************************************************************************/
  45. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  46. public final class CollectionUtils
  47.   {
  48.     /*******************************************************************************************************************
  49.      *
  50.      * Appends a list to an object. The resulting list is mutable.
  51.      *
  52.      * @param     <T>       the type of list items
  53.      * @param     list      the list
  54.      * @param     object    the list to append
  55.      * @return              the list with the appended object
  56.      *
  57.      * @it.tidalwave.javadoc.stable
  58.      *
  59.      ******************************************************************************************************************/
  60.     @Nonnull
  61.     public static <T> List<T> concat (@Nonnull final List<? extends T> list, @Nonnull final T object)
  62.       {
  63.         final List<T> result = new ArrayList<>(list);
  64.         result.add(object);
  65.         return result;
  66.       }

  67.     /*******************************************************************************************************************
  68.      *
  69.      * Appends a list to another. The resulting list is mutable.
  70.      *
  71.      * @param     <T>       the type of list items
  72.      * @param     list1     the former list
  73.      * @param     list2     the latter list
  74.      * @return              the list with the appended object
  75.      *
  76.      * @it.tidalwave.javadoc.stable
  77.      *
  78.      ******************************************************************************************************************/
  79.     @Nonnull
  80.     public static <T> List<T> concat (@Nonnull final List<? extends T> list1, @Nonnull final List<? extends T> list2)
  81.       {
  82.         final List<T> result = new ArrayList<>(list1);
  83.         result.addAll(list2);
  84.         return result;
  85.       }

  86.     /*******************************************************************************************************************
  87.      *
  88.      * Reverses a list. The resulting list is mutable.
  89.      *
  90.      * @param     <T>       the type of list items
  91.      * @param     list      the list
  92.      * @return              the reversed list
  93.      *
  94.      * @it.tidalwave.javadoc.stable
  95.      *
  96.      ******************************************************************************************************************/
  97.     @Nonnull
  98.     public static <T> List<T> reversed (@Nonnull final List<? extends T> list)
  99.       {
  100.         final List<T> result = new ArrayList<>(list);
  101.         Collections.reverse(result);
  102.         return result;
  103.       }

  104.     /*******************************************************************************************************************
  105.      *
  106.      * Sorts a list. The resulting list is mutable.
  107.      *
  108.      * @param     <T>       the type of list items
  109.      * @param     list      the list
  110.      * @return              the sorted list
  111.      * @since     3.2-ALPHA-13
  112.      *
  113.      * @it.tidalwave.javadoc.stable
  114.      *
  115.      ******************************************************************************************************************/
  116.     @Nonnull
  117.     public static <T extends Comparable<? super T>> List<T> sorted (@Nonnull final List<? extends T> list)
  118.       {
  119.         final var result = new ArrayList<T>(list);
  120.         Collections.sort(result);
  121.         return result;
  122.       }

  123.     /*******************************************************************************************************************
  124.      *
  125.      * Sorts a list with a given {@link Comparator}. The resulting list is mutable.
  126.      *
  127.      * @param     <T>         the type of list items
  128.      * @param     list        the list
  129.      * @param     comparator  the comparator
  130.      * @return                the sorted list
  131.      * @since     3.2-ALPHA-13
  132.      *
  133.      * @it.tidalwave.javadoc.stable
  134.      *
  135.      ******************************************************************************************************************/
  136.     @Nonnull
  137.     public static <T> List<T> sorted (@Nonnull final List<? extends T> list,
  138.                                       @Nonnull final Comparator<? super T> comparator)
  139.       {
  140.         final var result = new ArrayList<T>(list);
  141.         result.sort(comparator);
  142.         return result;
  143.       }

  144.     /*******************************************************************************************************************
  145.      *
  146.      * Returns the (optional) first element of a list.
  147.      *
  148.      * @param     <T>       the type of list items
  149.      * @param     list      the list
  150.      * @return              the first element
  151.      *
  152.      * @it.tidalwave.javadoc.stable
  153.      *
  154.      ******************************************************************************************************************/
  155.     @Nonnull
  156.     public static <T> Optional<T> optionalHead (@Nonnull final List<? extends T> list)
  157.       {
  158.         return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
  159.       }

  160.     /*******************************************************************************************************************
  161.      *
  162.      * Returns the first element of a list.
  163.      *
  164.      * @param     <T>       the type of list items
  165.      * @param     list      the list (cannot be empty)
  166.      * @return              the first element
  167.      * @throws    IllegalArgumentException  if the list is empty
  168.      *
  169.      * @it.tidalwave.javadoc.stable
  170.      *
  171.      ******************************************************************************************************************/
  172.     @Nonnull
  173.     public static <T> T head (@Nonnull final List<? extends T> list)
  174.       {
  175.         if (list.isEmpty())
  176.           {
  177.             throw new IllegalArgumentException("List is empty");
  178.           }

  179.         return list.get(0);
  180.       }

  181.     /*******************************************************************************************************************
  182.      *
  183.      * Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an
  184.      * empty list. The resulting list is mutable.
  185.      *
  186.      * @param     <T>       the type of list items
  187.      * @param     list      the list
  188.      * @return              the tail of the list
  189.      *
  190.      * @it.tidalwave.javadoc.stable
  191.      *
  192.      ******************************************************************************************************************/
  193.     @Nonnull
  194.     public static <T> List<T> tail (@Nonnull final List<? extends T> list)
  195.       {
  196.         return new ArrayList<>(list.subList(1, list.size()));
  197.       }
  198.   }