View Javadoc
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    *      http://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  
22  /**
23   * Fills and returns arrays in the fluent style.
24   *
25   * @since 3.14.0
26   */
27  public final class ArrayFill {
28  
29      /**
30       * Fills and returns the given array.
31       *
32       * @param a   the array to be filled (may be null).
33       * @param val the value to be stored in all elements of the array.
34       * @return the given array.
35       * @see Arrays#fill(byte[],byte)
36       */
37      public static byte[] fill(final byte[] a, final byte val) {
38          if (a != null) {
39              Arrays.fill(a, val);
40          }
41          return a;
42      }
43  
44      /**
45       * Fills and returns the given array.
46       *
47       * @param a   the array to be filled (may be null).
48       * @param val the value to be stored in all elements of the array.
49       * @return the given array.
50       * @see Arrays#fill(char[],char)
51       */
52      public static char[] fill(final char[] a, final char val) {
53          if (a != null) {
54              Arrays.fill(a, val);
55          }
56          return a;
57      }
58  
59      /**
60       * Fills and returns the given array.
61       *
62       * @param a   the array to be filled (may be null).
63       * @param val the value to be stored in all elements of the array.
64       * @return the given array.
65       * @see Arrays#fill(double[],double)
66       */
67      public static double[] fill(final double[] a, final double val) {
68          if (a != null) {
69              Arrays.fill(a, val);
70          }
71          return a;
72      }
73  
74      /**
75       * Fills and returns the given array.
76       *
77       * @param a   the array to be filled (may be null).
78       * @param val the value to be stored in all elements of the array.
79       * @return the given array.
80       * @see Arrays#fill(float[],float)
81       */
82      public static float[] fill(final float[] a, final float val) {
83          if (a != null) {
84              Arrays.fill(a, val);
85          }
86          return a;
87      }
88  
89      /**
90       * Fills and returns the given array.
91       *
92       * @param a   the array to be filled (may be null).
93       * @param val the value to be stored in all elements of the array.
94       * @return the given array.
95       * @see Arrays#fill(int[],int)
96       */
97      public static int[] fill(final int[] a, final int val) {
98          if (a != null) {
99              Arrays.fill(a, val);
100         }
101         return a;
102     }
103 
104     /**
105      * Fills and returns the given array.
106      *
107      * @param a   the array to be filled (may be null).
108      * @param val the value to be stored in all elements of the array.
109      * @return the given array.
110      * @see Arrays#fill(long[],long)
111      */
112     public static long[] fill(final long[] a, final long val) {
113         if (a != null) {
114             Arrays.fill(a, val);
115         }
116         return a;
117     }
118 
119     /**
120      * Fills and returns the given array.
121      *
122      * @param a   the array to be filled (may be null).
123      * @param val the value to be stored in all elements of the array.
124      * @return the given array.
125      * @see Arrays#fill(short[],short)
126      */
127     public static short[] fill(final short[] a, final short val) {
128         if (a != null) {
129             Arrays.fill(a, val);
130         }
131         return a;
132     }
133 
134     /**
135      * Fills and returns the given array.
136      *
137      * @param <T> the array type.
138      * @param a   the array to be filled (may be null).
139      * @param val the value to be stored in all elements of the array.
140      * @return the given array.
141      * @see Arrays#fill(Object[],Object)
142      */
143     public static <T> T[] fill(final T[] a, final T val) {
144         if (a != null) {
145             Arrays.fill(a, val);
146         }
147         return a;
148     }
149 
150     private ArrayFill() {
151         // no instances
152     }
153 
154 }