Best Assertj code snippet using org.assertj.core.util.IterableUtil.newArray
Source:IterableUtil.java
...11 * Copyright 2012-2018 the original author or authors.12 */13package org.assertj.core.util;14import static java.util.Collections.emptyList;15import static org.assertj.core.util.Lists.newArrayList;16import static org.assertj.core.util.Preconditions.checkNotNull;17import java.lang.reflect.Array;18import java.util.ArrayList;19import java.util.Collection;20import java.util.Iterator;21import java.util.List;22public final class IterableUtil {23 /**24 * Indicates whether the given {@link Iterable} is {@code null} or empty.25 * 26 * @param iterable the given {@code Iterable} to check.27 * @return {@code true} if the given {@code Iterable} is {@code null} or empty, otherwise {@code false}.28 */29 public static boolean isNullOrEmpty(Iterable<?> iterable) {30 if (iterable == null) return true;31 if (iterable instanceof Collection && ((Collection<?>) iterable).isEmpty()) return true;32 return !iterable.iterator().hasNext();33 }34 /**35 * Returns the size of the given {@link Iterable}.36 * 37 * @param iterable the {@link Iterable} to get size.38 * @return the size of the given {@link Iterable}.39 * @throws NullPointerException if given {@link Iterable} is null.40 */41 public static int sizeOf(Iterable<?> iterable) {42 checkNotNull(iterable, "Iterable must not be null");43 if (iterable instanceof Collection) return ((Collection<?>) iterable).size();44 int size = 0;45 Iterator<?> iterator = iterable.iterator();46 while (iterator.hasNext()) {47 size++;48 iterator.next();49 }50 return size;51 }52 /**53 * Returns all the non-{@code null} elements in the given {@link Iterable}.54 * 55 * @param <T> the type of elements of the {@code Iterable}.56 * @param i the given {@code Iterable}.57 * @return all the non-{@code null} elements in the given {@code Iterable}. An empty list is returned if the given58 * {@code Iterable} is {@code null}.59 */60 public static <T> List<T> nonNullElementsIn(Iterable<? extends T> i) {61 if (isNullOrEmpty(i)) return emptyList();62 List<T> nonNull = new ArrayList<>();63 for (T element : i) {64 if (element != null) nonNull.add(element);65 }66 return nonNull;67 }68 /**69 * Create an array from an {@link Iterable}.70 * <p>71 * Note: this method will return Object[]. If you require a typed array please use {@link #toArray(Iterable, Class)}.72 * It's main usage is to keep the generic type for chaining call like in:73 * <pre><code class='java'> public S containsOnlyElementsOf(Iterable<? extends T> iterable) {74 * return containsOnly(toArray(iterable));75 * }</code></pre>76 * 77 * @param iterable an {@link Iterable} to translate in an array.78 * @param <T> the type of elements of the {@code Iterable}.79 * @return all the elements from the given {@link Iterable} in an array. {@code null} if given {@link Iterable} is80 * null.81 */82 @SuppressWarnings("unchecked")83 public static <T> T[] toArray(Iterable<? extends T> iterable) {84 if (iterable == null) return null;85 return (T[]) newArrayList(iterable).toArray();86 }87 /**88 * Create an typed array from an {@link Iterable}.89 *90 * @param iterable an {@link Iterable} to translate in an array.91 * @param type the type of the resulting array.92 * @param <T> the type of elements of the {@code Iterable}.93 * @return all the elements from the given {@link Iterable} in an array. {@code null} if given {@link Iterable} is94 * null.95 */96 public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {97 if (iterable == null) return null;98 Collection<? extends T> collection = toCollection(iterable);99 T[] array = newArray(type, collection.size());100 return collection.toArray(array);101 }102 public static <T> Collection<T> toCollection(Iterable<T> iterable) {103 return iterable instanceof Collection ? (Collection<T>) iterable : newArrayList(iterable);104 }105 @SafeVarargs106 public static <T> Iterable<T> iterable(T... elements) {107 if (elements == null) return null;108 ArrayList<T> list = newArrayList();109 java.util.Collections.addAll(list, elements);110 return list;111 }112 @SafeVarargs113 public static <T> Iterator<T> iterator(T... elements) {114 if (elements == null) return null;115 return iterable(elements).iterator();116 }117 @SuppressWarnings("unchecked")118 private static <T> T[] newArray(Class<T> type, int length) {119 return (T[]) Array.newInstance(type, length);120 }121 122 private IterableUtil() {}123}...
newArray
Using AI Code Generation
1import static org.assertj.core.util.IterableUtil.newArray;2import static org.assertj.core.util.Lists.newArrayList;3import static org.assertj.core.util.Maps.newHashMap;4import static org.assertj.core.util.Sets.newHashSet;5import static org.assertj.core.util.Sets.newLinkedHashSet;6import static org.assertj.core.util.Lists.newLinkedList;7import static org.assertj.core.util.Sets.newTreeSet;8import static org.assertj.core.util.Lists.newVector;9import static org.assertj.core.util.Maps.newConcurrentHashMap;10import static org.assertj.core.util.Queues.newConcurrentLinkedQueue;11import static org.assertj.core.util.Maps.newConcurrentSkipListMap;12import static org.assertj.core.util.Sets.newConcurrentSkipListSet;13import static org.assertj.core.util.Lists.newCopyOnWriteArrayList;14import static org.assertj.core.util.Sets.newCopyOnWriteArraySet;15import static org.assertj.core.util.Queues.newLinkedBlockingDeque;16import static org.assertj.core.util.Queues.newLinkedBlockingQueue;17import static org.assertj.core
newArray
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.util.IterableUtil.newArray;3public class IterableUtil_newArray_Test {4 public void should_create_new_array_from_iterable() {5 Iterable<String> iterable = Arrays.asList("foo", "bar");6 String[] array = newArray(iterable, String.class);7 assertThat(array).containsExactly("foo", "bar");8 }9}10import static org.assertj.core.api.Assertions.assertThat;11import static org.assertj.core.util.IterableUtil.newArray;12public class IterableUtil_newArray_Test {13 public void should_create_new_array_from_iterable() {14 Iterable<String> iterable = Arrays.asList("foo", "bar");15 String[] array = newArray(iterable, String.class);16 assertThat(array).containsExactly("foo", "bar");17 }18}
newArray
Using AI Code Generation
1import org.assertj.core.util.IterableUtil;2Iterable<String> iterable = Arrays.asList("one", "two", "three");3String[] array = IterableUtil.newArray(iterable, String.class);4import org.assertj.core.util.Arrays;5List<String> list = Arrays.asList("one", "two", "three");6String[] array = Arrays.newArray(list, String.class);7import org.assertj.core.util.Arrays;8Collection<String> collection = Arrays.asList("one", "two", "three");9String[] array = Arrays.newArray(collection, String.class);10import org.assertj.core.util.Arrays;11Iterable<String> iterable = Arrays.asList("one", "two", "three");12String[] array = Arrays.newArray(iterable, String.class);13import org.assertj.core.util.Arrays;14String[] array = {"one", "two", "three"};15String[] newArray = Arrays.newArray(array, String.class);16import org.assertj.core.util.Arrays;17int[] array = {1, 2, 3};18Integer[] newArray = Arrays.newArray(array);19import org.assertj.core.util.Arrays;20double[] array = {1.0, 2.0, 3.0};21Double[] newArray = Arrays.newArray(array);22import org.assertj.core.util.Arrays;23long[] array = {1L, 2L, 3L};24Long[] newArray = Arrays.newArray(array);25import org.assertj.core.util.Arrays;26boolean[] array = {true, false, true};27Boolean[] newArray = Arrays.newArray(array);
newArray
Using AI Code Generation
1List<Integer> list = Arrays.asList(1, 2, 3);2Integer[] array = IterableUtil.toArray(list, Integer.class);3assertThat(array).containsExactly(1, 2, 3);4Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));5Integer[] array = IterableUtil.toArray(set, Integer.class);6assertThat(array).containsExactly(1, 2, 3);7Integer[] array = new Integer[]{1, 2, 3};8Integer[] array = IterableUtil.toArray(array, Integer.class);9assertThat(array).containsExactly(1, 2, 3);10Iterable<Integer> iterable = Arrays.asList(1, 2, 3);11Integer[] array = IterableUtil.toArray(iterable, Integer.class);12assertThat(array).containsExactly(1, 2, 3);13Iterator<Integer> iterator = Arrays.asList(1, 2, 3).iterator();14Integer[] array = IterableUtil.toArray(iterator, Integer.class);15assertThat(array).containsExactly(1, 2, 3);16Enumeration<Integer> enumeration = Collections.enumeration(Arrays.asList(1, 2, 3));17Integer[] array = IterableUtil.toArray(enumeration, Integer.class);18assertThat(array).containsExactly(1, 2, 3);19Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3));20Integer[] array = IterableUtil.toArray(collection, Integer.class);21assertThat(array).containsExactly(1, 2, 3);22Map<Integer, String> map = new HashMap<>();23map.put(1, "
newArray
Using AI Code Generation
1 String[] array = {"a", "b", "c"};2 List<String> list = newArrayList(array);3 assertThat(list).contains("a", "b", "c");4 String[] array = {"a", "b", "c"};5 List<String> list = newArrayList(array);6 assertThat(list).contains("a", "b", "c");7 String[] array = {"a", "b", "c"};8 List<String> list = newArrayList(array);9 assertThat(list).contains("a", "b", "c");10 String[] array = {"a", "b", "c"};11 List<String> list = newArrayList(array);12 assertThat(list).contains("a", "b", "c");13 String[] array = {"a", "b", "c"};14 List<String> list = newArrayList(array);15 assertThat(list).contains("a", "b", "c");16 String[] array = {"a", "b", "c"};17 List<String> list = newArrayList(array);18 assertThat(list).contains("a", "b", "c");19 String[] array = {"a", "b", "c"};20 List<String> list = newArrayList(array);21 assertThat(list).contains("a", "b", "c");22 String[] array = {"a", "b", "c"};23 List<String> list = newArrayList(array);24 assertThat(list).contains("a", "b", "c");25 String[] array = {"a", "b", "c"};26 List<String> list = newArrayList(array);27 assertThat(list).contains("a", "b", "c");
newArray
Using AI Code Generation
1import org.assertj.core.util.IterableUtil;2import org.junit.jupiter.api.Test;3import java.util.Arrays;4import java.util.List;5import static org.assertj.core.api.Assertions.assertThat;6class ArrayFromListTest {7 void arrayFromList() {8 List<String> list = Arrays.asList("a", "b", "c");9 String[] array = IterableUtil.toArray(list, String.class);10 assertThat(array).containsExactly("a", "b", "c");11 }12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!