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.List;
  32. import java.util.Optional;
  33. import lombok.AccessLevel;
  34. import lombok.NoArgsConstructor;

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

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

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

  100.     /*******************************************************************************************************************
  101.      *
  102.      * Returns the (optional) first element of a list.
  103.      *
  104.      * @param     list      the list
  105.      * @return              the first element
  106.      *
  107.      * @it.tidalwave.javadoc.stable
  108.      *
  109.      ******************************************************************************************************************/
  110.     @Nonnull
  111.     public static <T> Optional<T> optionalHead (@Nonnull final List<? extends T> list)
  112.       {
  113.         return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
  114.       }

  115.     /*******************************************************************************************************************
  116.      *
  117.      * Returns the first element of a list.
  118.      *
  119.      * @param     list      the list (cannot be empty)
  120.      * @return              the first element
  121.      * @throws    IllegalArgumentException  if the list is empty
  122.      *
  123.      * @it.tidalwave.javadoc.stable
  124.      *
  125.      ******************************************************************************************************************/
  126.     @Nonnull
  127.     public static <T> T head (@Nonnull final List<? extends T> list)
  128.       {
  129.         if (list.isEmpty())
  130.           {
  131.             throw new IllegalArgumentException("List is empty");
  132.           }

  133.         return list.get(0);
  134.       }

  135.     /*******************************************************************************************************************
  136.      *
  137.      * Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an
  138.      * empty list. The resulting list is mutable.
  139.      *
  140.      * @param     list      the list
  141.      * @return              the tail of the list
  142.      *
  143.      * @it.tidalwave.javadoc.stable
  144.      *
  145.      ******************************************************************************************************************/
  146.     @Nonnull
  147.     public static <T> List<T> tail (@Nonnull final List<? extends T> list)
  148.       {
  149.         return new ArrayList<>(list.subList(1, list.size()));
  150.       }
  151.   }