public static < T > Optional < T > findStatisticOnDescendants ( Object context , String tag , String statName ) { @ SuppressWarnings ( "unchecked" ) Set < TreeNode > statResult = queryBuilder ( ) . descendants ( ) . filter ( context ( attributes ( Matchers . allOf ( hasAttribute ( "name" , statName ) , hasTag ( tag ) ) ) ) ) . build ( ) . execute ( Collections . singleton ( ContextManager . nodeFor ( context ) ) ) ; if ( statResult . size ( ) > 1 ) { throw new RuntimeException ( "One stat expected for " + statName + " but found " + statResult . size ( ) ) ; } if ( statResult . size ( ) == 1 ) { @ SuppressWarnings ( "unchecked" ) T result = ( T ) statResult . iterator ( ) . next ( ) . getContext ( ) . attributes ( ) . get ( "this" ) ; return Optional . ofNullable ( result ) ; } // No such stat in this context return Optional . empty ( ) ; }
Search for a statistic on the descendant of the context that matches the tag and statistic name .