1 package org.opentrafficsim.editor; 2 3 import java.util.NoSuchElementException; 4 5 /** 6 * Checks whether a child node is present, and if so can return it, in an efficient manner. 7 * <p> 8 * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 9 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 10 * </p> 11 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a> 12 */ 13 public class ChildNodeFinder 14 { 15 16 /** Parent. */ 17 private final XsdTreeNode parent; 18 19 /** Child. */ 20 private XsdTreeNode child; 21 22 /** 23 * Constructor. 24 * @param parent parent node 25 */ 26 public ChildNodeFinder(final XsdTreeNode parent) 27 { 28 this.parent = parent; 29 } 30 31 /** 32 * Whether the node has given child. 33 * @param name child name 34 * @return whether the node has given child 35 */ 36 public boolean hasChild(final String name) 37 { 38 try 39 { 40 this.child = this.parent.getFirstChild(name); 41 return true; 42 } 43 catch (NoSuchElementException ex) 44 { 45 return false; 46 } 47 } 48 49 /** 50 * Whether the node has given child which is also active. 51 * @param name child name 52 * @return whether the node has given child which is also active 53 */ 54 public boolean hasActiveChild(final String name) 55 { 56 return hasChild(name) && this.child.isActive(); 57 } 58 59 /** 60 * Returns the previously found node. 61 * @return previously found node 62 */ 63 public XsdTreeNode get() 64 { 65 return this.child; 66 } 67 68 }