Code example for TreeMap

Methods: get, lastKey, put, remove, size

0
     * @return the bitmap, or null if there's no such bitmap store in storage. 
     */ 
    public Bitmap retrieveBitmap(String bitmapKey) {
        if ( bitmapKey == null ) return null;
 
        Bitmap aBitmap = mFileCache.get( bitmapKey );
        if ( aBitmap == null ) {
            try { 
                aBitmap = retrieveBitmapFromFile( bitmapKey );
                putInCache( bitmapKey, aBitmap );
            } catch ( FileNotFoundException fnfe ) {
                Log.d( getClass().getSimpleName(), String.format(
                        "File %s doesn't exist on device.", bitmapKey ) );
                aBitmap = mNullBitmap;
            } 
        } 
        return aBitmap;
    } 
 
    /** 
     * Serves a freshly baked drawable. If the drawable was not in cache, put it 
     * in there, assuming you might need it again soon enough. 
     *  
     * @param drawableKey 
     *            is the key identifying the drawable. 
     * @return the drawable, or null if there's no such drawable store in 
     *         storage. 
     */ 
    public Drawable retrieveDrawable(String drawableKey) {
        Bitmap aBM = retrieveBitmap( drawableKey );
        if ( aBM == null ) return null;
        return new BitmapDrawable( mContext.getResources(), aBM );
 
    } 
 
    /** 
     * Delete any trace of the bitmap, both in memory and on local storage. 
     *  
     * @param bitmapKey 
     *            the key of the bitmap to delete 
     * @return true if the bitmap was deleted, false if it didn't exist. 
     */ 
    public boolean deleteBitmap(String bitmapKey) {
        if ( bitmapKey == null ) return false;
 
        boolean isCacheDeleted = mFileCache.remove( bitmapKey ) != null;
        boolean isFileDeleted = mContext.deleteFile( bitmapKey );
 
        if ( isFileDeleted != isCacheDeleted ) {
            Log.e( getClass().getSimpleName(),
                    "Inconsistent state between storage and cache." ); 
        } 
 
        return isFileDeleted && isCacheDeleted;
    } 
 
    /** 
     * Delete any trace of the drawable, both in memory and on local storage. 
     *  
     * @param drawableKey 
     *            the key of the drawable to delete 
     * @return true if the drawable was deleted, false if it didn't exist. 
     */ 
    public boolean deleteDrawable(String drawableKey) {
        return deleteBitmap( drawableKey );
    } 
 
    // //////////////////////////////////////////////////////////////////////// 
    // Helpers 
    // //////////////////////////////////////////////////////////////////////// 
 
    /** 
     * Dumb expiring cache 
     *  
     * @param bitmapKey 
     * @param aBitmap 
     */ 
    private void putInCache(String bitmapKey, Bitmap aBitmap) {
        if ( mFileCache.size() >= mMaxCacheCount ) {
            // An explicit ordering (by time) would make this a bit more 
            // relevant 
            String lastKey = mFileCache.lastKey();
            mFileCache.remove( lastKey );
        } 
        mFileCache.put( bitmapKey, aBitmap );
    } 
 
    private Bitmap retrieveBitmapFromFile(String bitmapKey)
            throws FileNotFoundException { 
        FileInputStream is = mContext.openFileInput( bitmapKey );