Skip to content

Commit 5e25f1c

Browse files
committed
Changes in the way Byte instances are created
1 parent 69f4be1 commit 5e25f1c

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

commons-core/src/main/java/fr/inria/atlanmod/commons/collect/MoreArrays.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ public static <T> T[] addAll(T[] array, int index, Collection<? extends T> eleme
189189
return newArray;
190190
}
191191

192+
/**
193+
* Adds all {@code array2} at the end of the {@code array1}.
194+
*
195+
* @param array1 the array to add the element to
196+
* @param array2 the values to add
197+
*
198+
* @return a new array of bytes containing all the existing elements in arrays 1 and 2.
199+
*
200+
* @throws NullPointerException if any argument is {@code null}
201+
*/
202+
@Nonnull
203+
public static byte[] addAll(byte[] array1, byte... array2) {
204+
checkNotNull(array1, "array1");
205+
checkNotNull(array2, "array2");
206+
207+
final byte[] newArray = new byte[array1.length + array2.length];
208+
System.arraycopy(array1, 0, newArray, 0, array1.length);
209+
System.arraycopy(array2, 0, newArray, array1.length, array2.length);
210+
211+
return newArray;
212+
213+
}
214+
192215
/**
193216
* Removes the element at the specified {@code index} from the specified {@code array}.
194217
*

commons-core/src/main/java/fr/inria/atlanmod/commons/primitive/Bytes.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
import fr.inria.atlanmod.commons.Throwables;
1212
import fr.inria.atlanmod.commons.annotation.Static;
1313

14+
import javax.annotation.Nonnull;
15+
import javax.annotation.ParametersAreNonnullByDefault;
1416
import java.nio.charset.StandardCharsets;
1517
import java.nio.charset.UnsupportedCharsetException;
1618
import java.util.ArrayList;
17-
import java.util.Collection;
1819
import java.util.Collections;
1920
import java.util.List;
2021

21-
import javax.annotation.Nonnull;
22-
import javax.annotation.ParametersAreNonnullByDefault;
23-
2422
import static fr.inria.atlanmod.commons.Preconditions.checkEqualTo;
2523
import static fr.inria.atlanmod.commons.Preconditions.checkNotNull;
2624

@@ -260,7 +258,7 @@ public static List<Byte> asList(byte... primitiveArray) {
260258
}
261259
List<Byte> byteCollection = new ArrayList<>(primitiveArray.length);
262260
for (byte each : primitiveArray) {
263-
byteCollection.add(new Byte(each));
261+
byteCollection.add(Byte.valueOf(each));
264262
}
265263
return byteCollection;
266264
}

commons-core/src/test/java/fr/inria/atlanmod/commons/collect/MoreArraysTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ public void testAdd() {
125125
.isInstanceOf(IndexOutOfBoundsException.class);
126126
}
127127

128+
@Test
129+
public void testAddAllBytes() {
130+
byte[] array1 = new byte[] {6,5,4,3,2,1};
131+
byte[] array2 = new byte[] {1, 2, 3};
132+
byte[] expected = new byte[] {6,5,4,3,2,1,1,2,3};
133+
134+
byte[] joinedArray = MoreArrays.addAll(array1, array2);
135+
136+
assertThat(joinedArray).isEqualTo(expected);
137+
}
138+
128139
@Test
129140
public void testRemove() {
130141
Integer[] array0 = new Integer[]{0, 1, 2, 3, 4};

commons-core/src/test/java/fr/inria/atlanmod/commons/primitive/BytesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void testToArray() {
124124
byte[] expected = new byte[] {1, 2, 3, 4, 5};
125125
List<Byte> boxedList = new ArrayList<>();
126126
for(byte each : expected) {
127-
boxedList.add(new Byte(each));
127+
boxedList.add(Byte.valueOf(each));
128128
}
129129
assertThat(boxedList.size()).isEqualTo(expected.length);
130130
assertThat(Bytes.toArray(boxedList)).isEqualTo(expected);

0 commit comments

Comments
 (0)