Add ArrayFill

This commit is contained in:
Gary Gregory 2023-08-10 09:20:31 -04:00
parent 1a1bc77c2a
commit 2e9b000e6c
4 changed files with 256 additions and 0 deletions

View File

@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add FailableFunction#function(FailableFunction).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CalendarUtils.getInstance().</action>
<action type="add" dev="aherbert" due-to="Dan Watson">Add syntax for optional tokens to DurationFormatUtils #1062.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayFill.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 59.</action>
</release>

View File

@ -0,0 +1,137 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import java.util.Arrays;
/**
* Fills and returns arrays in the fluent style.
*
* @since 3.14.0
*/
public final class ArrayFill {
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(byte[],byte)
*/
public static byte[] fill(final byte[] a, final byte val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(char[],char)
*/
public static char[] fill(final char[] a, final char val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(double[],double)
*/
public static double[] fill(final double[] a, final double val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(float[],float)
*/
public static float[] fill(final float[] a, final float val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(int[],int)
*/
public static int[] fill(final int[] a, final int val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(long[],long)
*/
public static long[] fill(final long[] a, final long val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(short[],short)
*/
public static short[] fill(final short[] a, final short val) {
Arrays.fill(a, val);
return a;
}
/**
* Fills and returns the given array.
*
* @param a the array to be filled.
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(Object[],Object)
*/
public static <T> T[] fill(final T[] a, final T val) {
Arrays.fill(a, val);
return a;
}
private ArrayFill() {
// no instances
}
}

View File

@ -22,6 +22,7 @@ import java.util.Comparator;
/**
* Sorts and returns arrays in the fluent style.
*
* TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
* @since 3.12.0
*/
public class ArraySorter {

View File

@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
/**
* Tests {@link ArrayFill}.
*/
public class ArrayFillTest extends AbstractLangTest {
@Test
public void testFillByteArray() {
final byte[] array = new byte[3];
final byte val = (byte) 1;
final byte[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final byte v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillCharArray() {
final char[] array = new char[3];
final char val = 1;
final char[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final char v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillDoubleArray() {
final double[] array = new double[3];
final double val = 1;
final double[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final double v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillFloatArray() {
final float[] array = new float[3];
final float val = 1;
final float[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final float v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillIntArray() {
final int[] array = new int[3];
final int val = 1;
final int[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final int v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillLongArray() {
final long[] array = new long[3];
final long val = 1;
final long[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final long v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillObjectArray() {
final String[] array = new String[3];
final String val = "A";
final String[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final String v : actual) {
assertEquals(val, v);
}
}
@Test
public void testFillShortArray() {
final short[] array = new short[3];
final short val = (byte) 1;
final short[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final short v : actual) {
assertEquals(val, v);
}
}
}