/** Returns the length of the supplied array. */ static public int getLength (Object array) { return java.lang.reflect.Array.getLength(array); }
/** Returns the length of the supplied array. */ static public int getLength (Object array) { return java.lang.reflect.Array.getLength(array); }
public boolean hasNext() { return currentIndex < Array.getLength(value); }
public static int getSize(Object object) { if (object == null) { return 0; } if (object instanceof Collection<?>) { return ((Collection<?>) object).size(); } else if (object instanceof Map<?, ?>) { return ((Map<?, ?>) object).size(); } else if (object.getClass().isArray()) { return Array.getLength(object); } else { return -1; } }
public static int getSize(Object object) { if (object == null) { return 0; } if (object instanceof Collection<?>) { return ((Collection<?>) object).size(); } else if (object instanceof Map<?, ?>) { return ((Map<?, ?>) object).size(); } else if (object.getClass().isArray()) { return Array.getLength(object); } else { return -1; } }
/** * Checks if the specified array is neither null nor empty. * * @param array the array to check * @throws IllegalArgumentException if {@code array} is either {@code null} or empty */ private static void validateArray(final Object array) { Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(Array.getLength(array) != 0, "Array cannot be empty."); }
@Override void apply(RequestBuilder builder, @Nullable Object values) throws IOException { if (values == null) return; // Skip null values. for (int i = 0, size = Array.getLength(values); i < size; i++) { //noinspection unchecked ParameterHandler.this.apply(builder, (T) Array.get(values, i)); } } };
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType) throws NoSuchMethodException { Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType()); Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array)); for (int i = 0; i < array.length; i++) { resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i])); } return resultColl; }
@Override public Object apply(@Nullable Object input) { if (input == null || Array.getLength(input) == 0) { return Collections.singletonList("null"); } return Collections.singletonList(input); } }
static boolean areArrayElementsEqual(Object o1, Object o2) { for (int i = 0; i < Array.getLength(o1); i++) { if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false; } return true; }
/** * Check if the supplied value is the first entry in the array represented by the possibleArray value. * @param value the value to check for in the array * @param possibleArray an array object that may have the supplied value as the first element * @return true if the supplied value is the first entry in the array */ private static boolean isFirstEntryInArray(Object value, @Nullable Object possibleArray) { if (possibleArray == null) { return false; } Class<?> type = possibleArray.getClass(); if (!type.isArray() || Array.getLength(possibleArray) == 0 || !ClassUtils.isAssignableValue(type.getComponentType(), value)) { return false; } Object arrayValue = Array.get(possibleArray, 0); return (type.getComponentType().isPrimitive() ? arrayValue.equals(value) : arrayValue == value); }
private Object growArrayIfNecessary(Object array, int index, String name) { if (!isAutoGrowNestedPaths()) { return array; } int length = Array.getLength(array); if (index >= length && index < this.autoGrowCollectionLimit) { Class<?> componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, index + 1); System.arraycopy(array, 0, newArray, 0, length); for (int i = length; i < Array.getLength(newArray); i++) { Array.set(newArray, i, newValue(componentType, null, name)); } setPropertyValue(name, newArray); Object defaultValue = getPropertyValue(name); Assert.state(defaultValue != null, "Default value must not be null"); return defaultValue; } else { return array; } }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (sourceType.isAssignableTo(targetType)) { return source; } if (Array.getLength(source) == 0) { return null; } Object firstElement = Array.get(source, 0); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); }
private void addArrayValues(ParameterSignature sig, String name, List<PotentialAssignment> list, Object array) { for (int i = 0; i < Array.getLength(array); i++) { Object value = Array.get(array, i); if (sig.canAcceptValue(value)) { list.add(PotentialAssignment.forValue(name + "[" + i + "]", value)); } } }
@Override public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return Optional.empty(); } else if (source instanceof Optional) { return source; } else if (targetType.getResolvableType().hasGenerics()) { Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType)); if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) || (target instanceof Collection && ((Collection<?>) target).isEmpty())) { return Optional.empty(); } return Optional.of(target); } else { return Optional.of(source); } }
private Object getToStringableArrayElement(Object array, int length, int index) { if (index < length) { Object element = Array.get(array, index); if (isArray(element)) { return objectWithToString(componentTypeName(element.getClass()) + "[" + Array.getLength(element) + "]"); } else { return element; } } else { return END_OF_ARRAY_SENTINEL; } }
@Override public T deserialize(JsonParser p, DeserializationContext ctxt, T existing) throws IOException { T newValue = deserialize(p, ctxt); if (existing == null) { return newValue; } int len = Array.getLength(existing); if (len == 0) { return newValue; } return _concat(existing, newValue); }