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
18 package org.apache.commons.lang3;
19
20 import java.util.Arrays;
21 import java.util.function.IntFunction;
22
23 import org.apache.commons.lang3.function.FailableIntFunction;
24
25 /**
26 * Fills and returns arrays in the fluent style.
27 *
28 * @since 3.14.0
29 */
30 public final class ArrayFill {
31
32 /**
33 * Fills and returns the given array, assigning the given {@code boolean} value to each element of the array.
34 *
35 * @param a the array to be filled (may be null).
36 * @param val the value to be stored in all elements of the array.
37 * @return the given array.
38 * @see Arrays#fill(boolean[],boolean)
39 * @since 3.18.0
40 */
41 public static boolean[] fill(final boolean[] a, final boolean val) {
42 if (a != null) {
43 Arrays.fill(a, val);
44 }
45 return a;
46 }
47
48 /**
49 * Fills and returns the given array, assigning the given {@code byte} value to each element of the array.
50 *
51 * @param a the array to be filled (may be null).
52 * @param val the value to be stored in all elements of the array.
53 * @return the given array.
54 * @see Arrays#fill(byte[],byte)
55 */
56 public static byte[] fill(final byte[] a, final byte val) {
57 if (a != null) {
58 Arrays.fill(a, val);
59 }
60 return a;
61 }
62
63 /**
64 * Fills and returns the given array, assigning the given {@code char} value to each element of the array.
65 *
66 * @param a the array to be filled (may be null).
67 * @param val the value to be stored in all elements of the array.
68 * @return the given array.
69 * @see Arrays#fill(char[],char)
70 */
71 public static char[] fill(final char[] a, final char val) {
72 if (a != null) {
73 Arrays.fill(a, val);
74 }
75 return a;
76 }
77
78 /**
79 * Fills and returns the given array, assigning the given {@code double} value to each element of the array.
80 *
81 * @param a the array to be filled (may be null).
82 * @param val the value to be stored in all elements of the array.
83 * @return the given array.
84 * @see Arrays#fill(double[],double)
85 */
86 public static double[] fill(final double[] a, final double val) {
87 if (a != null) {
88 Arrays.fill(a, val);
89 }
90 return a;
91 }
92
93 /**
94 * Fills and returns the given array, assigning the given {@code float} value to each element of the array.
95 *
96 * @param a the array to be filled (may be null).
97 * @param val the value to be stored in all elements of the array.
98 * @return the given array.
99 * @see Arrays#fill(float[],float)
100 */
101 public static float[] fill(final float[] a, final float val) {
102 if (a != null) {
103 Arrays.fill(a, val);
104 }
105 return a;
106 }
107
108 /**
109 * Fills and returns the given array, assigning the given {@code int} value to each element of the array.
110 *
111 * @param a the array to be filled (may be null).
112 * @param val the value to be stored in all elements of the array.
113 * @return the given array.
114 * @see Arrays#fill(int[],int)
115 */
116 public static int[] fill(final int[] a, final int val) {
117 if (a != null) {
118 Arrays.fill(a, val);
119 }
120 return a;
121 }
122
123 /**
124 * Fills and returns the given array, assigning the given {@code long} value to each element of the array.
125 *
126 * @param a the array to be filled (may be null).
127 * @param val the value to be stored in all elements of the array.
128 * @return the given array.
129 * @see Arrays#fill(long[],long)
130 */
131 public static long[] fill(final long[] a, final long val) {
132 if (a != null) {
133 Arrays.fill(a, val);
134 }
135 return a;
136 }
137
138 /**
139 * Fills and returns the given array, assigning the given {@code short} value to each element of the array.
140 *
141 * @param a the array to be filled (may be null).
142 * @param val the value to be stored in all elements of the array.
143 * @return the given array.
144 * @see Arrays#fill(short[],short)
145 */
146 public static short[] fill(final short[] a, final short val) {
147 if (a != null) {
148 Arrays.fill(a, val);
149 }
150 return a;
151 }
152
153 /**
154 * Fills and returns the given array, using the provided generator supplier to compute each element. Like
155 * {@link Arrays#setAll(Object[], IntFunction)} with exception support.
156 * <p>
157 * If the generator supplier throws an exception, it is relayed to the caller and the array is left in an indeterminate
158 * state.
159 * </p>
160 *
161 * @param <T> type of elements of the array.
162 * @param array array to be initialized.
163 * @param generator a function accepting an index and producing the desired value for that position.
164 * @return the input array
165 * @param <E> The kind of thrown exception or error.
166 * @throws E Thrown by the given {@code generator}.
167 * @see Arrays#setAll(Object[], IntFunction)
168 * @since 3.18.0
169 */
170 public static <T, E extends Throwable> T[] fill(final T[] array, final FailableIntFunction<? extends T, E> generator) throws E {
171 if (array != null && generator != null) {
172 for (int i = 0; i < array.length; i++) {
173 array[i] = generator.apply(i);
174 }
175 }
176 return array;
177 }
178
179 /**
180 * Fills and returns the given array, assigning the given {@code T} value to each element of the array.
181 *
182 * @param <T> the array type.
183 * @param a the array to be filled (may be null).
184 * @param val the value to be stored in all elements of the array.
185 * @return the given array.
186 * @see Arrays#fill(Object[],Object)
187 */
188 public static <T> T[] fill(final T[] a, final T val) {
189 if (a != null) {
190 Arrays.fill(a, val);
191 }
192 return a;
193 }
194
195 private ArrayFill() {
196 // no instances
197 }
198
199 }