Skip to content

Commit 4c010d7

Browse files
authored
Merge pull request #2 from sunye/master
Array conversions for byte and Byte types.
2 parents ac061b9 + 5e25f1c commit 4c010d7

4 files changed

Lines changed: 159 additions & 3 deletions

File tree

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

Lines changed: 58 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
*
@@ -275,4 +298,39 @@ public static <T> int lastIndexOf(T[] array, @Nullable T value) {
275298

276299
return NO_INDEX;
277300
}
301+
302+
/**
303+
* Converts an array of {@link Byte} to an array of primitive types {@link byte}.
304+
*
305+
* @param boxedArray The array of {@link Byte} to convert.
306+
* @return An array of {@link byte} containing the same elements as {@code boxedArray},
307+
* in the same order, converted to primitives.
308+
*/
309+
@Nonnull
310+
public static byte[] toPrimitive(final Byte... boxedArray) {
311+
checkNotNull(boxedArray, "boxedArray");
312+
byte[] primitiveArray = new byte[boxedArray.length];
313+
for (int i = 0; i < boxedArray.length; i++) {
314+
primitiveArray[i] = boxedArray[i].byteValue();
315+
}
316+
return primitiveArray;
317+
}
318+
319+
/**
320+
* Converts an array of primitive types {@link byte} to an array of Objects {@Byte}.
321+
*
322+
* @param primitiveArray The array of {@linke byte} to convert.
323+
*
324+
* @return An array of {@link Byte} containing the same elements as {@code primitiveArray}, in the same order,
325+
* converted to boxed types.
326+
*/
327+
public static Byte[] toObject(final byte... primitiveArray) {
328+
checkNotNull(primitiveArray, "primitiveArray");
329+
Byte[] boxedArray = new Byte[primitiveArray.length];
330+
331+
for (int i = 0; i < boxedArray.length; i++) {
332+
boxedArray[i] = Byte.valueOf(primitiveArray[i]);
333+
}
334+
return boxedArray;
335+
}
278336
}

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

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

14-
import java.nio.charset.StandardCharsets;
15-
import java.nio.charset.UnsupportedCharsetException;
16-
1714
import javax.annotation.Nonnull;
1815
import javax.annotation.ParametersAreNonnullByDefault;
16+
import java.nio.charset.StandardCharsets;
17+
import java.nio.charset.UnsupportedCharsetException;
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
1921

2022
import static fr.inria.atlanmod.commons.Preconditions.checkEqualTo;
2123
import static fr.inria.atlanmod.commons.Preconditions.checkNotNull;
@@ -238,4 +240,46 @@ public static String toStringBinary(final byte[] bytes) {
238240

239241
return new String(result);
240242
}
243+
244+
245+
/**
246+
* Converts a {@code byte} array into a {@link List} of {@link Byte}.
247+
*
248+
* @param primitiveArray the {@code byte} array to convert
249+
* @return A {@link List} containing the same elements as {@code primitiveArray}, in the same order,
250+
* coverted to wrapper objects.
251+
*/
252+
@Nonnull
253+
public static List<Byte> asList(byte... primitiveArray) {
254+
checkNotNull(primitiveArray, "primitiveArray");
255+
256+
if (primitiveArray.length == 0) {
257+
return Collections.emptyList();
258+
}
259+
List<Byte> byteCollection = new ArrayList<>(primitiveArray.length);
260+
for (byte each : primitiveArray) {
261+
byteCollection.add(Byte.valueOf(each));
262+
}
263+
return byteCollection;
264+
}
265+
266+
267+
/**
268+
* Converts a {@link List} of {@link Byte} into a {@code byte} array.
269+
*
270+
* @param boxedList the {@link List} of {@link Byte} to convert.
271+
* @return an array containing the same elements as {@code boxedList}, in the same order, converted to primitives.
272+
*/
273+
@Nonnull
274+
public static byte[] toArray(List<Byte> boxedList) {
275+
checkNotNull(boxedList, "boxedList");
276+
277+
int i = 0;
278+
byte[] elements = new byte[boxedList.size()];
279+
for (Byte each : boxedList) {
280+
elements[i++] = each.byteValue();
281+
}
282+
return elements;
283+
}
284+
241285
}

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

Lines changed: 27 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};
@@ -185,4 +196,20 @@ public void testLastIndexOf() {
185196

186197
assertThat(MoreArrays.lastIndexOf(array0, 10)).isEqualTo(MoreArrays.NO_INDEX);
187198
}
199+
200+
@Test
201+
public void testBytesToPrimitive() {
202+
Byte[] boxedArray = new Byte[] {0, 1, 2, 3, 4};
203+
byte[] primitiveArray = new byte[] {0, 1, 2, 3, 4};
204+
205+
assertThat(primitiveArray).isEqualTo(MoreArrays.toPrimitive(boxedArray));
206+
}
207+
208+
@Test
209+
public void testBytesToObject() {
210+
Byte[] boxedArray = new Byte[] {0, 1, 2, 3, 4};
211+
byte[] primitiveArray = new byte[] {0, 1, 2, 3, 4};
212+
213+
assertThat(boxedArray).isEqualTo(MoreArrays.toObject(primitiveArray));
214+
}
188215
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.junit.jupiter.api.Test;
1414

1515
import java.nio.ByteBuffer;
16+
import java.util.ArrayList;
17+
import java.util.List;
1618

1719
import javax.annotation.ParametersAreNonnullByDefault;
1820

@@ -102,4 +104,29 @@ public void testToStringBinaryAndReverse() {
102104

103105
assertThat(Bytes.toString(actualBytes0)).isEqualTo(expected0);
104106
}
107+
108+
@Test
109+
public void testAsList() {
110+
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
111+
List<Byte> boxedList = Bytes.asList(bytes);
112+
113+
assertThat(boxedList.size()).isEqualTo(bytes.length);
114+
115+
List<Byte> expected = new ArrayList<>(bytes.length);
116+
for(byte each : bytes) {
117+
expected.add(each);
118+
}
119+
assertThat(expected).isEqualTo(boxedList);
120+
}
121+
122+
@Test
123+
public void testToArray() {
124+
byte[] expected = new byte[] {1, 2, 3, 4, 5};
125+
List<Byte> boxedList = new ArrayList<>();
126+
for(byte each : expected) {
127+
boxedList.add(Byte.valueOf(each));
128+
}
129+
assertThat(boxedList.size()).isEqualTo(expected.length);
130+
assertThat(Bytes.toArray(boxedList)).isEqualTo(expected);
131+
}
105132
}

0 commit comments

Comments
 (0)