001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.lang3; 019 020import java.util.Arrays; 021 022/** 023 * Fills and returns arrays in the fluent style. 024 * 025 * @since 3.14.0 026 */ 027public final class ArrayFill { 028 029 /** 030 * Fills and returns the given array. 031 * 032 * @param a the array to be filled (may be null). 033 * @param val the value to be stored in all elements of the array. 034 * @return the given array. 035 * @see Arrays#fill(byte[],byte) 036 */ 037 public static byte[] fill(final byte[] a, final byte val) { 038 if (a != null) { 039 Arrays.fill(a, val); 040 } 041 return a; 042 } 043 044 /** 045 * Fills and returns the given array. 046 * 047 * @param a the array to be filled (may be null). 048 * @param val the value to be stored in all elements of the array. 049 * @return the given array. 050 * @see Arrays#fill(char[],char) 051 */ 052 public static char[] fill(final char[] a, final char val) { 053 if (a != null) { 054 Arrays.fill(a, val); 055 } 056 return a; 057 } 058 059 /** 060 * Fills and returns the given array. 061 * 062 * @param a the array to be filled (may be null). 063 * @param val the value to be stored in all elements of the array. 064 * @return the given array. 065 * @see Arrays#fill(double[],double) 066 */ 067 public static double[] fill(final double[] a, final double val) { 068 if (a != null) { 069 Arrays.fill(a, val); 070 } 071 return a; 072 } 073 074 /** 075 * Fills and returns the given array. 076 * 077 * @param a the array to be filled (may be null). 078 * @param val the value to be stored in all elements of the array. 079 * @return the given array. 080 * @see Arrays#fill(float[],float) 081 */ 082 public static float[] fill(final float[] a, final float val) { 083 if (a != null) { 084 Arrays.fill(a, val); 085 } 086 return a; 087 } 088 089 /** 090 * Fills and returns the given array. 091 * 092 * @param a the array to be filled (may be null). 093 * @param val the value to be stored in all elements of the array. 094 * @return the given array. 095 * @see Arrays#fill(int[],int) 096 */ 097 public static int[] fill(final int[] a, final int val) { 098 if (a != null) { 099 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}