/** * Selects the list of collection elements that satisfy the given filter. * If the filter is <code>null</code>, all the collection elements are returned. * * @param collection The collection to be filtered * @param filter The filter to be applied * @param <T> The type of collection elements * @return A list of elements satisfying the filter */ public static <T> List<T> where(Collection<T> collection, Predicate<T> filter) { List<T> result = null; if (collection != null) { if (filter != null) { result = where(collection.stream(), filter); } else { result = (collection instanceof List ? (List<T>) collection : new ArrayList<>(collection)); } } return result; }
private String findKeyFile(String key) { if (key == null || key.isEmpty()) return null; String ret = files.get(key); if (ret == null) { String namePart = FileUtils.getFilenameWithoutExtension(FileUtils.getFileNameFromPath(key)); String extPart = FileUtils.getExtension(key); ret = firstOrDefault(files.keySet(), k -> { String name = FileUtils.getFilenameWithoutExtension(FileUtils.getFileNameFromPath(k)); name = name.substring(name.lastIndexOf("/") + 1); return (extPart != null && extPart.equalsIgnoreCase(FileUtils.getExtension(k))) && namePart.equals(name); }); //If no identical name found, look for a name that could be a truncated name of key (needed for some Deimos products) if(ret == null) { ret = firstOrDefault(files.keySet(), k -> { String name = FileUtils.getFilenameWithoutExtension(FileUtils.getFileNameFromPath(k)); name = name.substring(name.lastIndexOf("/") + 1); return (extPart != null && extPart.equalsIgnoreCase(FileUtils.getExtension(k))) && namePart.startsWith(name); }); } } return ret; } }
/** * Selects the list of array elements that satisfy the given filter. * If the filter is <code>null</code>, all the array elements are returned. * * @param array The array to be filtered * @param filter The filter to be applied * @param <T> The type of array elements * @return A list of elements satisfying the filter */ public static <T> List<T> where(T[] array, Predicate<T> filter) { List<T> result = null; if (array != null) { if (filter != null) { result = where(Arrays.stream(array), filter); } else { result = Arrays.asList(array); } } return result; }
/** * Returns the first element of the array that satisfies the given condition, or <code>null</code> if no such * element exists. If the condition is <code>null</code>, the first element of the array is returned. * * @param array The array to be searched * @param condition The condition to be applied * @param <T> The type of the array elements * @return The first element of the array matching the condition, or <code>null</code> */ public static <T> T firstOrDefault(T[] array, Predicate<T> condition) { T result = null; if (array != null) { if (condition != null) { List<T> subset = where(array, condition); if (subset != null && subset.size() > 0) result = subset.get(0); } else { result = array[0]; } } return result; }
/** * Returns the first element of the collection that satisfies the given condition, or <code>null</code> if no such * element exists. If the condition is <code>null</code>, the first element of the collection is returned. * * @param collection The collection to be searched * @param condition The condition to be applied * @param <T> The type of the collection elements * @return The first element of the collection matching the condition, or <code>null</code> */ public static <T> T firstOrDefault(Collection<T> collection, Predicate<T> condition) { T result = null; if (collection != null) { if (condition != null) { List<T> subset = where(collection, condition); if (subset != null && subset.size() > 0) result = subset.get(0); } else { result = collection.iterator().next(); } } return result; }
List<M> rasterMetadataList = CollectionHelper.where(metadata, m -> m.getFileName().endsWith(getMetadataFileSuffix()));