cdc.applic.dictionaries.edit.EnElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdc-applic-dictionaries-edit Show documentation
Show all versions of cdc-applic-dictionaries-edit Show documentation
Applicabilities Dictionaries Edition.
package cdc.applic.dictionaries.edit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public interface EnElement {
public static final Comparator ID_COMPARATOR =
Comparator.comparing(EnElement::getId);
public String getId();
public String getKind();
/**
* @return The owner element of this element, possibly {@code null}.
*/
public EnElement getOwner();
public default boolean hasOwner() {
return getOwner() != null;
}
/**
* @return The depth (1 for root element) of the element.
*/
public default int getDepth() {
int depth = 0;
EnElement index = this;
while (index != null) {
index = index.getOwner();
depth++;
}
return depth;
}
/**
* @return A list of ancestors.
*/
public default List getHierarchy() {
final List list = new ArrayList<>();
EnElement iter = this;
while (iter != null) {
list.add(iter);
iter = iter.getOwner();
}
Collections.reverse(list);
return list;
}
/**
* @return The index of this element among the children of its parent.
* {@code -1} if this element has no parent.
*/
public int getIndex();
public default List getIndices() {
final List list = new ArrayList<>();
EnElement iter = this;
while (iter != null && iter.getIndex() >= 0) {
list.add(iter.getIndex());
iter = iter.getOwner();
}
Collections.reverse(list);
return list;
}
/**
* @param The owner type.
* @param ownerClass The owner class.
* @return The owner element of this element,
* possibly {@code null} or converted to {@code ownerClass}.
* @throws ClassCastException When the owner can not be converted to {@code ownerClass}.
*/
public default O getOwner(Class ownerClass) {
return ownerClass.cast(getOwner());
}
/**
* @return All children of this element.
*/
public List extends EnElement> getChildren();
/**
* @param The child type.
* @param childClass The child class.
* @return All children elements that are instances of {@code childClass}.
*/
public default List getChildren(Class childClass) {
return getChildren().stream()
.filter(childClass::isInstance)
.map(childClass::cast)
.toList();
}
/**
* @param The child type.
* @param childClass The child class.
* @param predicate The predicate.
* @return All children elements that are instances of {@code childClass} and match {@code predicate}.
*/
public default List getChildren(Class childClass,
Predicate super C> predicate) {
return getChildren().stream()
.filter(childClass::isInstance)
.map(childClass::cast)
.filter(predicate)
.toList();
}
/**
* @param The child type.
* @param childClass The child class.
* @return {@code true} if this element has {@code childClass} children.
*/
public default boolean hasChildren(Class childClass) {
return getChildren().stream()
.anyMatch(childClass::isInstance);
}
/**
* @return The {@link EnRepository} of this element.
*/
public EnRepository getRepository();
/**
* Traverses all elements and invokes a consumer on elements that match a class and predicate.
*
* @param The element type.
* @param cls The element class.
* @param consumer The element consumer.
* @param predicate The element predicate.
*/
public default void traverse(Class cls,
Consumer super X> consumer,
Predicate super X> predicate) {
if (cls.isInstance(this)) {
final X x = cls.cast(this);
if (predicate.test(x)) {
consumer.accept(x);
}
}
for (final EnElement child : getChildren()) {
child.traverse(cls, consumer, predicate);
}
}
/**
* Traverses all elements and invokes a consumer on elements that match a class.
*
* @param The element type.
* @param cls The element class.
* @param consumer The element consumer.
*/
public default void traverse(Class cls,
Consumer super X> consumer) {
traverse(cls, consumer, x -> true);
}
/**
* Traverses all elements and invokes a consumer on each traversed element.
*
* @param consumer The element consumer.
*/
public default void traverse(Consumer super EnElement> consumer) {
traverse(EnElement.class, consumer);
consumer.accept(this);
}
/**
* @param The element type.
* @param cls The element class.
* @param predicate The element predicate.
* @return A List of all elements that are instances of {@code cls} and match {@code predicate}.
*/
public default List collect(Class cls,
Predicate super X> predicate) {
final List list = new ArrayList<>();
traverse(cls, list::add, predicate);
return list;
}
/**
* @param The element type.
* @param cls The element class.
* @return A List of all elements that are instances of {@code cls}.
*/
public default List collect(Class cls) {
return collect(cls, x -> true);
}
public static interface Builder> {
public Class extends EnElement> getBuiltClass();
public B self();
public EnElement build();
}
}