@ Override public Object put ( Object object ) { Object returnVal = null ; long currentTime = CHFWBundle . getApproxTime ( ) ; synchronized ( this ) { // get next free position, or oldest position if none free lastEntry ++ ; // If last entry is past end of array, go back to the beginning if ( lastEntry == poolSize ) { lastEntry = 0 ; } returnVal = free [ lastEntry ] ; // get whatever was in that slot for return // value free [ lastEntry ] = object ; timeFreed [ lastEntry ] = currentTime ; // if we overlaid the first/oldest entry, reset the first entry position if ( lastEntry == firstEntry ) { firstEntry ++ ; if ( firstEntry == poolSize ) { firstEntry = 0 ; } } if ( firstEntry == - 1 ) { // if pool was empty before, this is first entry now firstEntry = lastEntry ; // should always be at '0', this may // overwrite the oldest entry, in which case // the oldest one will be garbage collected } if ( returnVal != null && destroyer != null ) { // @PK36998A destroyer . destroy ( returnVal ) ; } // check timestamp of oldest entry, and delete if older than 60 seconds // we should do a batch cleanup of all pools based on a timer rather than // waiting for a buffer to be released to trigger it, maybe in the next // release if ( cleanUpOld ) { while ( firstEntry != lastEntry ) { if ( currentTime > ( timeFreed [ firstEntry ] + 60000L ) ) { if ( destroyer != null && free [ firstEntry ] != null ) { // @PK36998A destroyer . destroy ( free [ firstEntry ] ) ; } free [ firstEntry ] = null ; firstEntry ++ ; if ( firstEntry == poolSize ) { firstEntry = 0 ; } } else break ; } } } return returnVal ; }
Puts an Object into the free pool . If the free pool is full then this object will overlay the oldest object in the pool .