Utils { @NotNull public static Set<String> getStrings(@Nullable final Object input) { Set<String> strings = new LinkedHashSet<>(); if (input != null) { Class clazz = input.getClass(); if (Collection.class.isAssignableFrom(clazz)) { for (Object obj : (Collection)input) { if (obj != null) { strings.add(obj.toString()); } } } else if (Object[].class.isAssignableFrom(clazz)) { for (Object obj : (Object[]) input) { if (obj != null) { strings.add(obj.toString()); } } } else if (String.class.isAssignableFrom(clazz)) { for (String str : ((String)input).split(",")) { if (StringUtils.isNotBlank(str)) { strings.add(str.trim()); } } } } return strings; } private  Utils(); @NotNull static String getURL(@NotNull SlingHttpServletRequest request, @NotNull PageManager pageManager, @NotNull String path); @NotNull static String getURL(@NotNull SlingHttpServletRequest request, @NotNull Page page); static String generateId(String prefix, String path); @NotNull static Set<String> getPageResourceTypes(@NotNull Page page, @NotNull SlingHttpServletRequest request, @NotNull ModelFactory modelFactory); @NotNull static Set<String> getResourceTypes(@NotNull Resource resource, @NotNull SlingHttpServletRequest request, @NotNull ModelFactory modelFactory); @NotNull static Set<String> getXFResourceTypes(@NotNull Resource resource, @NotNull SlingHttpServletRequest request, @NotNull ModelFactory modelFactory); @NotNull static Set<String> getTemplateResourceTypes(@NotNull Page page, @NotNull SlingHttpServletRequest request, @NotNull ModelFactory modelFactory); @NotNull static Set<String> getSuperTypes(@NotNull final String resourceType, @NotNull final ResourceResolver resourceResolver); @NotNull static Set<String> getStrings(@Nullable final Object input); static final String ID_SEPARATOR; static final String COMPONENTS_SERVICE; }
@Test void testGetStrings() { Set<String> reference = new LinkedHashSet<String>() {{ add("test1"); add("test2"); }}; List<String> list = new LinkedList<String>() {{ add("test1"); add("test2"); add("test1"); }}; assertEquals(reference, Utils.getStrings(list)); List<Object> objectList = new LinkedList<Object>() {{ add(new Object() { @Override public String toString() { return "test1"; } }); add(new Object() { @Override public String toString() { return "test2"; } }); }}; assertEquals(reference, Utils.getStrings(objectList)); String[] array = new String[] {"test2", "test2", "test1"}; assertEquals(reference, Utils.getStrings(array)); String csv = "test1, test2,test1 ,test2"; assertEquals(reference, Utils.getStrings(csv)); reference = new HashSet<String>() {{ add("test"); }}; assertEquals(reference, Utils.getStrings("test")); assertEquals(new LinkedHashSet<String>(), Utils.getStrings(null)); }