/** * 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); }
/** * Attempt to determine if a iterable-like is empty, preferably without iterating. * @param obj the iterable-like to check for emptiness * @return emptiness to fill your heart */ public static boolean isEmpty(final Object obj) { if (obj == null) { throw new IllegalArgumentException("cannot determine emptiness of null"); } if (obj instanceof Collection) { return ((Collection<?>) obj).isEmpty(); } if (obj instanceof Iterable) { return !((Iterable<?>) obj).iterator().hasNext(); } if (obj.getClass().isArray()) { return Array.getLength(obj) == 0; } throw new IllegalArgumentException(getTypeWarning(obj.getClass())); }
/** Given an iterable-like object, try to determine its static (i.e, without looking at contents) element type. */ public static Optional<Type> elementTypeOf(Object iterable) { return elementTypeOf(iterable.getClass()); }
final boolean singleValue = method.getParameters()[paramIdx].isAnnotationPresent(SingleValue.class); final Object arg = args[paramIdx]; if (!singleValue && IterableLike.isIterable(arg)) { extras.add(IterableLike.of(arg)); foundIterator = true; } else {
@Test public void testIsEmptyList() { final List<String> in = new ArrayList<String>(); in.add("1"); in.add("2"); in.add("3"); assertThat(IterableLike.isEmpty(in)).isFalse(); }
@Override public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) { final BindList bindList = (BindList) annotation; final String name = ParameterUtil.findParameterName(bindList.value(), param) .orElseThrow(() -> new UnsupportedOperationException("A @BindList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param)); return (stmt, arg) -> { if (arg == null || IterableLike.isEmpty(arg)) { bindList.onEmpty().define(stmt, name); } else { stmt.bindList(name, IterableLike.toList(arg)); } }; } }
SqlArrayArgument(SqlArrayType<T> arrayType, Object newArray) { this.typeName = arrayType.getTypeName(); @SuppressWarnings("unchecked") Stream<T> stream = (Stream<T>) IterableLike.stream(newArray); array = stream.map(arrayType::convertArrayElement).toArray(); }
@Override public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type paramType) { final BindMethodsList bindMethodsList = (BindMethodsList) annotation; final String name = ParameterUtil.findParameterName(bindMethodsList.value(), param) .orElseThrow(() -> new UnsupportedOperationException("A @BindMethodsList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param)); return (stmt, arg) -> { if (arg == null) { throw new IllegalArgumentException("argument is null; null was explicitly forbidden on BindMethodsList"); } stmt.bindMethodsList(name, IterableLike.iterable(arg), Arrays.asList(bindMethodsList.methodNames())); }; } }
@Test public void testIsEmptyNull() { assertThatThrownBy(() -> IterableLike.isEmpty(null)).isInstanceOf(IllegalArgumentException.class); } }
/** * 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); }
@Test public void testIsEmptyEmptyPrimitiveArray() { assertThat(IterableLike.isEmpty(new int[]{})).isTrue(); }
/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return an iterator over its (possibly boxed) elements. * * @param iterable the iterable-like * @return an iterator of the given array's elements */ @SuppressWarnings("unchecked") public static Iterator<Object> of(Object iterable) { if (iterable == null) { throw new IllegalArgumentException("can't iterate null"); } if (iterable instanceof Iterator<?>) { return (Iterator<Object>) iterable; } else if (iterable instanceof Iterable<?>) { return ((Iterable<Object>) iterable).iterator(); } Class<?> klass = iterable.getClass(); if (!klass.isArray()) { throw new IllegalArgumentException(getTypeWarning(klass)); } if (klass.getComponentType().isPrimitive()) { return new PrimitiveArrayIterator(iterable); } return Arrays.asList((Object[]) iterable).iterator(); }
@Override public Optional<Argument> build(Type type, Object value, ConfigRegistry config) { return IterableLike.elementTypeOf(type) .flatMap(config.get(SqlArrayTypes.class)::findFor) .map(arrayType -> arrayArgument(value, arrayType)); }
/** * 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 testIsEmptyPrimitive() { assertThatThrownBy(() -> IterableLike.isEmpty(5)).isInstanceOf(IllegalArgumentException.class); }
/** * Attempt to determine if a iterable-like is empty, preferably without iterating. * @param obj the iterable-like to check for emptiness * @return emptiness to fill your heart */ public static boolean isEmpty(final Object obj) { if (obj == null) { throw new IllegalArgumentException("cannot determine emptiness of null"); } if (obj instanceof Collection) { return ((Collection<?>) obj).isEmpty(); } if (obj instanceof Iterable) { return !((Iterable<?>) obj).iterator().hasNext(); } if (obj.getClass().isArray()) { return Array.getLength(obj) == 0; } throw new IllegalArgumentException(getTypeWarning(obj.getClass())); }
@Test public void testOverflowOnEmpty() { final Iterator<?> it = IterableLike.of(new int[]{}); assertThatThrownBy(it::next).isInstanceOf(NoSuchElementException.class); }
@Test public void testIsEmptyEmptyList() { assertThat(IterableLike.isEmpty(new ArrayList<String>())).isTrue(); }
/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return an iterator over its (possibly boxed) elements. * * @return an iterator of the given array's elements */ @SuppressWarnings("unchecked") public static Iterator<Object> of(Object iterable) { if (iterable == null) { throw new IllegalArgumentException("can't iterate null"); } if (iterable instanceof Iterator<?>) { return (Iterator<Object>) iterable; } else if (iterable instanceof Iterable<?>) { return ((Iterable<Object>) iterable).iterator(); } Class<? extends Object> klass = iterable.getClass(); if(!klass.isArray()) { throw new IllegalArgumentException(getTypeWarning(klass)); } if (klass.getComponentType().isPrimitive()) { return new PrimitiveArrayIterator(iterable); } return Arrays.asList((Object[])iterable).iterator(); }
@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); }