diff --git a/org.jhotdraw8.base/src/main/java/org.jhotdraw8.base/org/jhotdraw8/base/converter/IdResolver.java b/org.jhotdraw8.base/src/main/java/org.jhotdraw8.base/org/jhotdraw8/base/converter/IdResolver.java index 632ed4dcc..e5f64d367 100644 --- a/org.jhotdraw8.base/src/main/java/org.jhotdraw8.base/org/jhotdraw8/base/converter/IdResolver.java +++ b/org.jhotdraw8.base/src/main/java/org.jhotdraw8.base/org/jhotdraw8/base/converter/IdResolver.java @@ -17,7 +17,21 @@ public interface IdResolver { * @param id the id * @return the object */ - @Nullable Object getObject(String id); + @Nullable Object getObject(@Nullable String id); + + /** + * Gets the object of the specified class for the specified id. + * Returns null if the id has no object of this type. + * + * @param clazz the clazz + * @param id the id + * @return the object + */ + default @Nullable T getObject(@NonNull Class clazz, @Nullable String id) { + Object object = getObject(id); + if (clazz.isInstance(object)) return clazz.cast(object); + return null; + } /** * Absolutize the given external URI, so that it can be used inside diff --git a/org.jhotdraw8.collection/src/main/java/org.jhotdraw8.collection/org/jhotdraw8/collection/pair/UnorderedPair.java b/org.jhotdraw8.collection/src/main/java/org.jhotdraw8.collection/org/jhotdraw8/collection/pair/UnorderedPair.java index f8dfbdddc..968632fba 100644 --- a/org.jhotdraw8.collection/src/main/java/org.jhotdraw8.collection/org/jhotdraw8/collection/pair/UnorderedPair.java +++ b/org.jhotdraw8.collection/src/main/java/org.jhotdraw8.collection/org/jhotdraw8/collection/pair/UnorderedPair.java @@ -14,6 +14,17 @@ * @author Werner Randelshofer */ public interface UnorderedPair { + /** + * Returns other() if someone is equal to either(), + * returns either() otherwise. + * + * @param someone either or other + * @return + */ + default V getOther(V someone) { + V either = either(); + return Objects.equals(either, someone) ? other() : either; + } V either(); diff --git a/org.jhotdraw8.fxbase/src/main/java/org.jhotdraw8.fxbase/org/jhotdraw8/fxbase/tree/TreeNode.java b/org.jhotdraw8.fxbase/src/main/java/org.jhotdraw8.fxbase/org/jhotdraw8/fxbase/tree/TreeNode.java index e7b0cd5de..086463443 100755 --- a/org.jhotdraw8.fxbase/src/main/java/org.jhotdraw8.fxbase/org/jhotdraw8/fxbase/tree/TreeNode.java +++ b/org.jhotdraw8.fxbase/src/main/java/org.jhotdraw8.fxbase/org/jhotdraw8/fxbase/tree/TreeNode.java @@ -214,6 +214,19 @@ default T getChild(int index) { return path; } + /** + * Returns the depth of this node. + * + * @return depth (0 if the node is the root) + */ + default @NonNull int getDepth() { + int depth = 0; + for (T node = getParent(); node != null; node = node.getParent()) { + depth++; + } + return depth; + } + /** * Returns an iterable which can iterate through this figure and all its * descendants in postorder sequence.