public boolean addIfAbsent ( E e ) { final ReentrantLock lock = this . lock ; lock . lock ( ) ; try { // Copy while checking if already present. // This wins in the most common case where it is not present Object [ ] elements = getArray ( ) ; int len = elements . length ; Object [ ] newElements = new Object [ len + 1 ] ; for ( int i = 0 ; i < len ; ++ i ) { if ( eq ( e , elements [ i ] ) ) return false ; // exit, throwing away copy else newElements [ i ] = elements [ i ] ; } newElements [ len ] = e ; setArray ( newElements ) ; return true ; } finally { lock . unlock ( ) ; } }
Append the element if not present .