protected void addStatement ( Statement statement , boolean isNew ) { PropertyIdValue pid = statement . getMainSnak ( ) . getPropertyId ( ) ; // This code maintains the following properties: // (1) the toKeep structure does not contain two statements with the // same statement id // (2) the toKeep structure does not contain two statements that can // be merged if ( this . toKeep . containsKey ( pid ) ) { List < StatementWithUpdate > statements = this . toKeep . get ( pid ) ; for ( int i = 0 ; i < statements . size ( ) ; i ++ ) { Statement currentStatement = statements . get ( i ) . statement ; boolean currentIsNew = statements . get ( i ) . write ; if ( ! "" . equals ( currentStatement . getStatementId ( ) ) && currentStatement . getStatementId ( ) . equals ( statement . getStatementId ( ) ) ) { // Same, non-empty id: ignore existing statement as if // deleted return ; } Statement newStatement = mergeStatements ( statement , currentStatement ) ; if ( newStatement != null ) { boolean writeNewStatement = ( isNew || ! newStatement . equals ( statement ) ) && ( currentIsNew || ! newStatement . equals ( currentStatement ) ) ; // noWrite: (newS == statement && !isNew) // || (newS == cur && !curIsNew) // Write: (newS != statement || isNew ) // && (newS != cur || curIsNew) statements . set ( i , new StatementWithUpdate ( newStatement , writeNewStatement ) ) ; // Impossible with default merge code: // Kept here for future extensions that may choose to not // reuse this id. if ( ! "" . equals ( statement . getStatementId ( ) ) && ! newStatement . getStatementId ( ) . equals ( statement . getStatementId ( ) ) ) { this . toDelete . add ( statement . getStatementId ( ) ) ; } if ( ! "" . equals ( currentStatement . getStatementId ( ) ) && ! newStatement . getStatementId ( ) . equals ( currentStatement . getStatementId ( ) ) ) { this . toDelete . add ( currentStatement . getStatementId ( ) ) ; } return ; } } statements . add ( new StatementWithUpdate ( statement , isNew ) ) ; } else { List < StatementWithUpdate > statements = new ArrayList <> ( ) ; statements . add ( new StatementWithUpdate ( statement , isNew ) ) ; this . toKeep . put ( pid , statements ) ; } }
Adds one statement to the list of statements to be kept possibly merging it with other statements to be kept if possible . When two existing statements are merged one of them will be updated and the other will be marked for deletion .