nl_tokens: / * TODO ( cpovirk ) : remove and removeExactly currently accept null inputs only if occurrences == 0 . This satisfies both NullPointerTester and CollectionRemoveTester . testRemove_nullAllowed but it s not clear that it s a good policy especially because in order for the test to pass the parameter must be misleadingly annotated as
pl_tokens: @ CanIgnoreReturnValue @ Override public int remove ( @ Nullable Object element , int occurrences ) { if ( occurrences == 0 ) { return count ( element ) ; } CollectPreconditions . checkPositive ( occurrences , "occurences" ) ; AtomicInteger existingCounter = Maps . safeGet ( countMap , element ) ; if ( existingCounter == null ) { return 0 ; } while ( true ) { int oldValue = existingCounter . get ( ) ; if ( oldValue != 0 ) { int newValue = Math . <mask> ( 0 , oldValue - occurrences ) ; if ( existingCounter . compareAndSet ( oldValue , newValue ) ) { if ( newValue == 0 ) { // Just CASed to 0; remove the entry to clean up the map. If the removal fails, // another thread has already replaced it with a new counter, which is fine. countMap . remove ( element , existingCounter ) ; } return oldValue ; } } else { return 0 ; } } }