private < T > boolean tryAdd ( ConcurrentHashMultiset < T > multiset , T value , int max ) { // Repeatedly attempt to add a new value to the given multiset until we // explicitly succeed or explicitly fail while ( true ) { // Get current number of values int count = multiset . count ( value ) ; // Bail out if the maximum has already been reached if ( count >= max && max != 0 ) return false ; // Attempt to add one more value if ( multiset . setCount ( value , count , count + 1 ) ) return true ; // Try again if unsuccessful } }
Attempts to add a single instance of the given value to the given multiset without exceeding the specified maximum number of values . If the value cannot be added without exceeding the maximum false is returned .