io.substrait.relation.CopyOnWriteUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Create a well-defined, cross-language specification for data compute operations
package io.substrait.relation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
/** Provides common utilities for copy-on-write visitations */
public class CopyOnWriteUtils {
public static boolean allEmpty(Optional>... optionals) {
return Arrays.stream(optionals).noneMatch(Optional::isPresent);
}
/** The `or` method on Optional instances is a Java 9+ feature */
public static Optional or(Optional left, Supplier extends Optional> right) {
if (left.isPresent()) {
return left;
} else {
return right.get();
}
}
@FunctionalInterface
public interface TransformFunction {
Optional apply(T t) throws E;
}
/**
* Applies the given transformation function to each item in the list. If any of the list items
* are transformed, returns a new list in which each item is either
*
*
* - a transformed new item replacing an old item
*
- the original item in the position it was in
*
*
* @param items the list of items to transform
* @param transform the transformation function to apply to each item
* @return An empty optional if none of the items have changed. An optional containing a new list
* otherwise.
*/
public static - Optional
> transformList(
List- items, TransformFunction
- transform) throws E {
List
- newItems = new ArrayList<>();
boolean listUpdated = false;
for (ITEM item : items) {
Optional
- newItem = transform.apply(item);
if (newItem.isPresent()) {
newItems.add(newItem.get());
listUpdated = true;
} else {
newItems.add(item);
}
}
return listUpdated ? Optional.of(newItems) : Optional.empty();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy