/** * Gets the given object as a {@link List}. * * @param obj The object to translate * @return The list, if available */ public static Optional<List<?>> asList(@Nullable Object obj) { if (obj == null) { return Optional.empty(); } if (obj instanceof List) { return Optional.<List<?>>of((List<?>) obj); } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return Optional.<List<?>>of(Coerce.primitiveArrayToList(obj)); } return Optional.<List<?>>of(Arrays.asList((Object[])obj)); } return Optional.<List<?>>of(Coerce.parseStringToList(obj.toString())); }
/** * Coerce the supplied object to a list. Accepts lists and all types of 1D * arrays. Also (naively) supports lists in Strings in a format like * <code>{1,2,3,I,am,a,list}</code> * * @param obj Object to coerce * @return Some kind of List filled with unimaginable horrors */ public static List<?> toList(@Nullable Object obj) { if (obj == null) { return Collections.<Object>emptyList(); } if (obj instanceof List) { return (List<?>)obj; } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return Coerce.primitiveArrayToList(obj); } return Arrays.asList((Object[])obj); } return Coerce.parseStringToList(obj.toString()); }
/** * Gets the given object as a {@link List}. * * @param obj The object to translate * @return The list, if available */ public static Optional<List<?>> asList(@Nullable Object obj) { if (obj == null) { return Optional.empty(); } if (obj instanceof List) { return Optional.<List<?>>of((List<?>) obj); } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return Optional.<List<?>>of(Coerce.primitiveArrayToList(obj)); } return Optional.<List<?>>of(Arrays.asList((Object[])obj)); } return Optional.<List<?>>of(Coerce.parseStringToList(obj.toString())); }
/** * Coerce the supplied object to a list. Accepts lists and all types of 1D * arrays. Also (naively) supports lists in Strings in a format like * <code>{1,2,3,I,am,a,list}</code> * * @param obj Object to coerce * @return Some kind of List filled with unimaginable horrors */ public static List<?> toList(@Nullable Object obj) { if (obj == null) { return Collections.<Object>emptyList(); } if (obj instanceof List) { return (List<?>)obj; } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return Coerce.primitiveArrayToList(obj); } return Arrays.asList((Object[])obj); } return Coerce.parseStringToList(obj.toString()); }