public Item addAdjustment ( final InvoiceItem item , final UUID targetInvoiceId ) { final UUID targetId = item . getLinkedItemId ( ) ; final NodeInterval node = findNode ( new SearchCallback ( ) { @ Override public boolean isMatch ( final NodeInterval curNode ) { return ( ( ItemsNodeInterval ) curNode ) . getItemsInterval ( ) . findItem ( targetId ) != null ; } } ) ; Preconditions . checkNotNull ( node , "Unable to find item interval for id='%s', tree=%s" , targetId , this ) ; final ItemsInterval targetItemsInterval = ( ( ItemsNodeInterval ) node ) . getItemsInterval ( ) ; final Item targetItem = targetItemsInterval . findItem ( targetId ) ; Preconditions . checkNotNull ( targetItem , "Unable to find item with id='%s', itemsInterval=%s" , targetId , targetItemsInterval ) ; final BigDecimal adjustmentAmount = item . getAmount ( ) . negate ( ) ; if ( targetItem . getAmount ( ) . compareTo ( adjustmentAmount ) == 0 ) { // Full item adjustment - treat it like a repair addExistingItem ( new ItemsNodeInterval ( this , new Item ( item , targetItem . getStartDate ( ) , targetItem . getEndDate ( ) , targetInvoiceId , ItemAction . CANCEL ) ) ) ; return targetItem ; } else { targetItem . incrementAdjustedAmount ( adjustmentAmount ) ; return null ; } }
Add the adjustment amount on the item specified by the targetId .