ArrayFill.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.lang3;

  18. import java.util.Arrays;
  19. import java.util.function.IntFunction;

  20. import org.apache.commons.lang3.function.FailableIntFunction;

  21. /**
  22.  * Fills and returns arrays in the fluent style.
  23.  *
  24.  * @since 3.14.0
  25.  */
  26. public final class ArrayFill {

  27.     /**
  28.      * Fills and returns the given array, assigning the given {@code boolean} value to each element of the array.
  29.      *
  30.      * @param a   the array to be filled (may be null).
  31.      * @param val the value to be stored in all elements of the array.
  32.      * @return the given array.
  33.      * @see Arrays#fill(boolean[],boolean)
  34.      * @since 3.18.0
  35.      */
  36.     public static boolean[] fill(final boolean[] a, final boolean val) {
  37.         if (a != null) {
  38.             Arrays.fill(a, val);
  39.         }
  40.         return a;
  41.     }

  42.     /**
  43.      * Fills and returns the given array, assigning the given {@code byte} value to each element of the array.
  44.      *
  45.      * @param a   the array to be filled (may be null).
  46.      * @param val the value to be stored in all elements of the array.
  47.      * @return the given array.
  48.      * @see Arrays#fill(byte[],byte)
  49.      */
  50.     public static byte[] fill(final byte[] a, final byte val) {
  51.         if (a != null) {
  52.             Arrays.fill(a, val);
  53.         }
  54.         return a;
  55.     }

  56.     /**
  57.      * Fills and returns the given array, assigning the given {@code char} value to each element of the array.
  58.      *
  59.      * @param a   the array to be filled (may be null).
  60.      * @param val the value to be stored in all elements of the array.
  61.      * @return the given array.
  62.      * @see Arrays#fill(char[],char)
  63.      */
  64.     public static char[] fill(final char[] a, final char val) {
  65.         if (a != null) {
  66.             Arrays.fill(a, val);
  67.         }
  68.         return a;
  69.     }

  70.     /**
  71.      * Fills and returns the given array, assigning the given {@code double} value to each element of the array.
  72.      *
  73.      * @param a   the array to be filled (may be null).
  74.      * @param val the value to be stored in all elements of the array.
  75.      * @return the given array.
  76.      * @see Arrays#fill(double[],double)
  77.      */
  78.     public static double[] fill(final double[] a, final double val) {
  79.         if (a != null) {
  80.             Arrays.fill(a, val);
  81.         }
  82.         return a;
  83.     }

  84.     /**
  85.      * Fills and returns the given array, assigning the given {@code float} value to each element of the array.
  86.      *
  87.      * @param a   the array to be filled (may be null).
  88.      * @param val the value to be stored in all elements of the array.
  89.      * @return the given array.
  90.      * @see Arrays#fill(float[],float)
  91.      */
  92.     public static float[] fill(final float[] a, final float val) {
  93.         if (a != null) {
  94.             Arrays.fill(a, val);
  95.         }
  96.         return a;
  97.     }

  98.     /**
  99.      * Fills and returns the given array, assigning the given {@code int} value to each element of the array.
  100.      *
  101.      * @param a   the array to be filled (may be null).
  102.      * @param val the value to be stored in all elements of the array.
  103.      * @return the given array.
  104.      * @see Arrays#fill(int[],int)
  105.      */
  106.     public static int[] fill(final int[] a, final int val) {
  107.         if (a != null) {
  108.             Arrays.fill(a, val);
  109.         }
  110.         return a;
  111.     }

  112.     /**
  113.      * Fills and returns the given array, assigning the given {@code long} value to each element of the array.
  114.      *
  115.      * @param a   the array to be filled (may be null).
  116.      * @param val the value to be stored in all elements of the array.
  117.      * @return the given array.
  118.      * @see Arrays#fill(long[],long)
  119.      */
  120.     public static long[] fill(final long[] a, final long val) {
  121.         if (a != null) {
  122.             Arrays.fill(a, val);
  123.         }
  124.         return a;
  125.     }

  126.     /**
  127.      * Fills and returns the given array, assigning the given {@code short} value to each element of the array.
  128.      *
  129.      * @param a   the array to be filled (may be null).
  130.      * @param val the value to be stored in all elements of the array.
  131.      * @return the given array.
  132.      * @see Arrays#fill(short[],short)
  133.      */
  134.     public static short[] fill(final short[] a, final short val) {
  135.         if (a != null) {
  136.             Arrays.fill(a, val);
  137.         }
  138.         return a;
  139.     }

  140.     /**
  141.      * Fills and returns the given array, using the provided generator supplier to compute each element. Like
  142.      * {@link Arrays#setAll(Object[], IntFunction)} with exception support.
  143.      * <p>
  144.      * If the generator supplier throws an exception, it is relayed to the caller and the array is left in an indeterminate
  145.      * state.
  146.      * </p>
  147.      *
  148.      * @param <T> type of elements of the array.
  149.      * @param array array to be initialized.
  150.      * @param generator a function accepting an index and producing the desired value for that position.
  151.      * @return the input array
  152.      * @param <E> The kind of thrown exception or error.
  153.      * @throws E Thrown by the given {@code generator}.
  154.      * @see Arrays#setAll(Object[], IntFunction)
  155.      * @since 3.18.0
  156.      */
  157.     public static <T, E extends Throwable> T[] fill(final T[] array, final FailableIntFunction<? extends T, E> generator) throws E {
  158.         if (array != null && generator != null) {
  159.             for (int i = 0; i < array.length; i++) {
  160.                 array[i] = generator.apply(i);
  161.             }
  162.         }
  163.         return array;
  164.     }

  165.     /**
  166.      * Fills and returns the given array, assigning the given {@code T} value to each element of the array.
  167.      *
  168.      * @param <T> the array type.
  169.      * @param a   the array to be filled (may be null).
  170.      * @param val the value to be stored in all elements of the array.
  171.      * @return the given array.
  172.      * @see Arrays#fill(Object[],Object)
  173.      */
  174.     public static <T> T[] fill(final T[] a, final T val) {
  175.         if (a != null) {
  176.             Arrays.fill(a, val);
  177.         }
  178.         return a;
  179.     }

  180.     private ArrayFill() {
  181.         // no instances
  182.     }

  183. }