/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return an iterable over its (possibly boxed) elements. * @param iterable the iterable-like to create a real Iterable for * @return the created Iterable */ public static Iterable<Object> iterable(Object iterable) { return () -> of(iterable); }
/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return a {@link Stream} over its (possibly boxed) elements. * * @return a stream of the given array's elements */ public static Stream<Object> stream(Object iterable) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(of(iterable), Spliterator.ORDERED), false); }
/** * Collect an iterable-like into a newly allocated ArrayList. * @param iterable the iterable-like to collect * @return a new list with the elements */ public static List<Object> toList(Object iterable) { List<Object> result = new ArrayList<Object>(); of(iterable).forEachRemaining(result::add); return result; }
@Test public void testPrimitiveToIterator() { assertThatThrownBy(() -> IterableLike.of(1)).isInstanceOf(IllegalArgumentException.class); }
@Test public void testOverflowOnEmpty() { final Iterator<?> it = IterableLike.of(new int[]{}); assertThatThrownBy(it::next).isInstanceOf(NoSuchElementException.class); }
@Test public void testOverflow() { final Iterator<?> it = IterableLike.of(new int[]{1}); assertThat(it.hasNext()).isTrue(); assertThat(it.next()).isEqualTo(1); assertThatThrownBy(it::next).isInstanceOf(NoSuchElementException.class); }
@Test public void testNullToIterator() { assertThatThrownBy(() -> IterableLike.of(null)).isInstanceOf(IllegalArgumentException.class); }
@Test public void testOtherClassToIterator() { assertThatThrownBy(() -> IterableLike.of("bla")).isInstanceOf(IllegalArgumentException.class); // or any other kind of object that isn't a java.lang.Object }
@Test public void testObjectToIterator() { assertThatThrownBy(() -> IterableLike.of(new Object())).isInstanceOf(IllegalArgumentException.class); }
final Object arg = args[paramIdx]; if (!singleValue && IterableLike.isIterable(arg)) { extras.add(IterableLike.of(arg)); foundIterator = true; } else {
@Test public void testEmptyArray() { final Iterator<Object> it = IterableLike.of(new int[]{}); assertThat(it).isEmpty(); }
@Test public void testListToIterator() { final List<String> in = new ArrayList<String>(2); in.add("1"); in.add("2"); final Object[] out = toArray(IterableLike.of(in)); assertThat(out).containsExactly("1", "2"); }
@Test public void testIntArray() { final Iterator<Object> it = IterableLike.of(new int[]{1, 2, 3}); assertThat(it).containsExactly(1, 2, 3); }
@Test public void testSetToIterator() { final Set<String> in = new HashSet<String>(2); in.add("1"); in.add("2"); final Object[] out = toArray(IterableLike.of(in)); assertThat(out).containsExactly("1", "2"); }
@Test public void testEmptyListToIterator() { final Object[] out = toArray(IterableLike.of(new ArrayList<Integer>())); assertThat(out).isEmpty(); }
@Test public void testPrimitiveArrayToIterator() { final int[] in = new int[]{1, 2}; final Object[] out = toArray(IterableLike.of(in)); assertThat(out).containsExactly(1, 2); }
@Test public void testEmptyArrayToIterator() { final Object[] out = toArray(IterableLike.of(new int[]{})); assertThat(out).isEmpty(); }
@Test public void testStringArrayToIterator() { final String[] in = new String[]{"1", "2"}; final Object[] out = toArray(IterableLike.of(in)); assertThat(out).containsExactly("1", "2"); }
@Test public void testIterableToIterator() { final Iterable<String> in = new Iterable<String>() { @Override public Iterator<String> iterator() { final List<String> tmp = new ArrayList<String>(); tmp.add("1"); tmp.add("2"); return tmp.iterator(); } }; final Object[] out = toArray(IterableLike.of(in)); assertThat(out).containsExactly("1", "2"); }
@SuppressWarnings("unchecked") SqlArrayArgument(SqlArrayType<T> arrayType, Object newArray) { this.typeName = arrayType.getTypeName(); List<Object> elements = new ArrayList<>( newArray.getClass().isArray() ? Array.getLength(newArray) : 10); IterableLike.of(newArray).forEachRemaining( e -> elements.add(arrayType.convertArrayElement((T) e))); array = elements.toArray(); }