示例#1
0
  public void provideTile(TileData<T> tile) {
    if (null == tile) return;

    TileIndex index = tile.getDefinition();
    TileCacheEntry<T> entry = _cache.get(index);
    if (null != entry) entry.setTile(tile);
  }
示例#2
0
 @Override
 public boolean onTileReceived(TileIndex index, TileData<T> tile) {
   TileCacheEntry<T> entry = _cache.get(index);
   if (null != entry) {
     _haveData.add(new Pair<TileIndex, Long>(index, entry.initialRequestTime()));
   }
   // Notify any global listeners
   if (null != _globalCallbacks && !_globalCallbacks.isEmpty()) {
     for (CacheRequestCallback<T> callback : _globalCallbacks) {
       callback.onTileReceived(index, tile);
     }
   }
   return false;
 }
示例#3
0
  /**
   * Take a list of tiles to request, and return the subset that are new requests - i.e., ones not
   * already in the cache.
   *
   * <p>Each new request will be placed in the cache.
   *
   * @param requests The list of tiles needed
   * @return A sublist of just those tiles not already requested
   */
  public List<TileIndex> getNewRequests(Iterable<TileIndex> requests) {
    List<TileIndex> needed = new ArrayList<>();
    synchronized (_cache) {
      for (TileIndex index : requests) {
        if (!_cache.containsKey(index)) {
          // Create the tile request, and listen for its fulfilment
          TileCacheEntry<T> entry = new TileCacheEntry<T>(index);
          entry.requestTile(_entryListener);

          // Add to cache
          _cache.put(index, entry);
          // Add to list of keys in request order
          _orderedKeys.add(index);

          needed.add(index);
        }
      }
    }

    return needed;
  }
示例#4
0
    @Override
    public boolean shouldRemove(
        Entry<TileIndex, TileCacheEntry<T>> entry,
        Map<TileIndex, TileCacheEntry<T>> map,
        int suggestedMaxSize) {
      if (null == entry) {
        return false;
      } else if (null == entry.getValue()) {
        return true;
      } else {
        TileCacheEntry<T> value = entry.getValue();
        if (value.hasBeenRetrieved() || value.age() > _maxTileAge) {
          // This entry has been received, or is to old for us to
          // care; just remove it.
          return true;
        } else {
          // First see if there is anything which has already been
          // handled, so can be freely deleted.
          for (Pair<TileIndex, Long> hasData : _haveData) {
            TileIndex index = hasData.getFirst();
            TileCacheEntry<T> entryWithData = _cache.get(index);
            if (entryWithData.hasBeenRetrieved()) {
              _cache.remove(index);
              return false;
            }
          }

          // If we get this far, there was nothing already handled.
          //
          // See if there's anything so old we just don't care about
          // its safety any more.
          if (_orderedKeys.size() > 0) {
            TileIndex oldestKey = _orderedKeys.get(0);
            TileCacheEntry<T> oldestEntry = _cache.get(oldestKey);
            if (oldestEntry.age() > _maxTileAge) {
              _cache.remove(oldestKey);
            }
          }
          return false;
        }
      }
    }
示例#5
0
 @Override
 public void onElementRemoved(TileIndex key, TileCacheEntry<T> value) {
   value.abandonTile();
 }
示例#6
0
 public void provideEmptyTile(TileIndex index) {
   TileCacheEntry<T> entry = _cache.get(index);
   if (null != entry) entry.setTile(null);
 }