package cgeo.geocaching;

import cgeo.geocaching.cgData.StorageLocation;
import cgeo.geocaching.connector.gc.Tile;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.utils.LeastRecentlyUsedMap;
import cgeo.geocaching.utils.LeastRecentlyUsedMap.RemoveHandler;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Cache for Caches. Every cache is stored in memory while c:geo is active to
 * speed up the app and to minimize network request - which are slow.
 */
public class CacheCache {

    private static final int MAX_CACHED_CACHES = 1000;
    final private LeastRecentlyUsedMap<String, cgCache> cachesCache;

    public CacheCache() {
        cachesCache = new LeastRecentlyUsedMap.LruCache<String, cgCache>(MAX_CACHED_CACHES);
        cachesCache.setRemoveHandler(new CacheRemoveHandler());
    }

    public void removeAllFromCache() {
        cachesCache.clear();
    }

    /**
     * @param geocode
     *            Geocode of the cache to remove from the cache
     */
    public void removeCacheFromCache(final String geocode) {
        if (StringUtils.isBlank(geocode)) {
            throw new IllegalArgumentException("geocode must not be empty");
        }
        cachesCache.remove(geocode);
    }

    /**
     * "Store" a cache in the CacheCache. If the cache is already in the CacheCache the cache gets replaced.
     *
     * @param cache
     *            Cache
     *
     */
    public void putCacheInCache(final cgCache cache) {
        if (cache == null) {
            throw new IllegalArgumentException("cache must not be null");
        }
        if (StringUtils.isBlank(cache.getGeocode())) {
            throw new IllegalArgumentException("geocode must not be empty");
        }
        cache.addStorageLocation(StorageLocation.CACHE);
        cachesCache.put(cache.getGeocode(), cache);
    }

    /**
     * @param geocode
     *            Geocode of the cache to retrieve from the cache
     * @return cache if found, null else
     */
    public cgCache getCacheFromCache(final String geocode) {
        if (StringUtils.isBlank(geocode)) {
            throw new IllegalArgumentException("geocode must not be empty");
        }
        return cachesCache.get(geocode);
    }

    public Set<String> getInViewport(final Viewport viewport, final CacheType cacheType) {
        final Set<String> geocodes = new HashSet<String>();
        for (final cgCache cache : cachesCache.values()) {
            if (cache.getCoords() == null) {
                // FIXME: this kludge must be removed, it is only present to help us debug the cases where
                // caches contain null coordinates.
                Log.e("CacheCache.getInViewport: got cache with null coordinates: " + cache.getGeocode());
                continue;
            }
            if ((CacheType.ALL == cacheType || cache.getType() == cacheType) && viewport.contains(cache)) {
                geocodes.add(cache.getGeocode());
            }
        }
        return geocodes;
    }

    @Override
    public String toString() {
        return StringUtils.join(cachesCache.keySet(), ' ');
    }

    private static class CacheRemoveHandler implements RemoveHandler<cgCache> {

        @Override
        public void onRemove(cgCache removed) {
            Tile.Cache.removeFromTileCache(removed);
        }
    }

}

@@ -944,32 +944,45 @@
   %7D%0A%0A    public 
+synchronized 
 void removeAllFr
@@ -1315,32 +1315,65 @@
 ty%22);%0A        %7D%0A
+        synchronized(this) %7B%0A    
         cachesCa
@@ -1385,32 +1385,42 @@
 emove(geocode);%0A
+        %7D%0A
     %7D%0A%0A    /**%0A 
@@ -1895,32 +1895,65 @@
 ty%22);%0A        %7D%0A
+        synchronized(this) %7B%0A    
         cache.ad
@@ -1993,32 +1993,36 @@
 CACHE);%0A        
+   
+ 
 cachesCache.put(
@@ -2045,24 +2045,34 @@
 (), cache);%0A
+        %7D%0A
     %7D%0A%0A    /
@@ -2395,32 +2395,65 @@
 ty%22);%0A        %7D%0A
+        synchronized(this) %7B%0A    
         return c
@@ -2473,24 +2473,34 @@
 t(geocode);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2505,16 +2505,29 @@
  public 
+synchronized 
 Set%3CStri
@@ -3307,16 +3307,29 @@
  public 
+synchronized 
 String t

Make the cache cache synchronized