public AbstractHtml getFirstChild ( ) { // this block must be synchronized otherwise may get null or // ConcurrentModificationException // the test cases are written to check its thread safety and can be // reproduce by uncommenting this synchronized block, checkout // AbstractHtmlTest class for it. // synchronized (children) { // // } // it's been replaced with locking final Lock lock = sharedObject . getLock ( ACCESS_OBJECT ) . readLock ( ) ; try { lock . lock ( ) ; // this must be most efficient because the javadoc of findFirst says // "This is a short-circuiting terminal operation." // return (T) children.stream().findFirst().orElse(null); // but as per CodePerformanceTest.testPerformanceOfFindFirst // the below is faster final Iterator < AbstractHtml > iterator = children . iterator ( ) ; if ( iterator . hasNext ( ) ) { return iterator . next ( ) ; } return null ; } finally { lock . unlock ( ) ; } }
Gets the first child of this tag . The efficient way to get the first child .