Android Open Source - wigle-wifi-wardriving-badfork Concurrent Linked Hash Map






From Project

Back to project page wigle-wifi-wardriving-badfork.

License

The source code is released under:

/* * Copyright (c) 2010-2012, Andrew Carra, Robert Hagemann, Hugh Kennedy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permi...

If you think the Android project wigle-wifi-wardriving-badfork listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.wigle.wigleandroid;
//ww  w  . ja  va  2s  . c  o  m
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * try to be all things to all people. a remove-eldest feature, concurrent except during put, 
 * which is usually single-threaded in this app anyway.
 * @param <K> key
 * @param <V> value
 */
public final class ConcurrentLinkedHashMap<K,V> {
  private final ConcurrentHashMap<K,V> map;
  private final LinkedBlockingQueue<K> queue;
  private int count = 0;
  
  private final int maxSize;
  private final Object WRITE_LOCK = new Object();
  
  public ConcurrentLinkedHashMap() {
    this( Integer.MAX_VALUE );
  }
  
  public ConcurrentLinkedHashMap( final int maxSize ) {
    map = new ConcurrentHashMap<K,V>();
    queue = new LinkedBlockingQueue<K>();
    this.maxSize = maxSize;
  }
  
  public V put( final K key, final V value ) {
    V previous = null;
    synchronized( WRITE_LOCK ) {
      previous = map.put(key, value);
      if ( previous == null ) {
        // new key! add to queue
        queue.add( key );
        // check if this puts us over
        if ( count + 1 > maxSize ) {
          // remove eldest
          K eldest = queue.remove();
          map.remove( eldest );
        }
        else {
          // this doesn't put us over, just add to the count
          count++;
        }
      }
    }
    return previous;
  }
  
  public V get( K key ) {
    return map.get( key );
  }
  
  /** 
   * make sure this is only used for reading (we only use it for reading currently.) the map is concurrent safe, but it will bugger 
   * our internal accounting for size() if the set is mutated
   */
  public Set<Map.Entry<K,V>> entrySet() {
    return map.entrySet();
  }
  
  public Collection<V> values() {
    return map.values();
  }
  
  public boolean isEmpty() {
    return map.isEmpty();
  }
  
  public int size() {
    return count; 
  }
  
}




Java Source Code List

net.wigle.wigleandroid.ConcurrentLinkedHashMap.java
net.wigle.wigleandroid.DBException.java
net.wigle.wigleandroid.DBResultActivity.java
net.wigle.wigleandroid.DashboardActivity.java
net.wigle.wigleandroid.DataActivity.java
net.wigle.wigleandroid.DatabaseHelper.java
net.wigle.wigleandroid.ErrorReportActivity.java
net.wigle.wigleandroid.LatLon.java
net.wigle.wigleandroid.ListActivity.java
net.wigle.wigleandroid.MainActivity.java
net.wigle.wigleandroid.MappingActivity.java
net.wigle.wigleandroid.NetworkActivity.java
net.wigle.wigleandroid.NetworkListAdapter.java
net.wigle.wigleandroid.NetworkType.java
net.wigle.wigleandroid.Network.java
net.wigle.wigleandroid.OpenStreetMapViewWrapper.java
net.wigle.wigleandroid.Pair.java
net.wigle.wigleandroid.QueryArgs.java
net.wigle.wigleandroid.QueryThread.java
net.wigle.wigleandroid.SSLConfigurator.java
net.wigle.wigleandroid.SettingsActivity.java
net.wigle.wigleandroid.SpeechActivity.java
net.wigle.wigleandroid.TTS.java
net.wigle.wigleandroid.WigleAndroid.java
net.wigle.wigleandroid.WigleService.java
net.wigle.wigleandroid.WigleUncaughtExceptionHandler.java
net.wigle.wigleandroid.background.AbstractBackgroundTask.java
net.wigle.wigleandroid.background.AlertSettable.java
net.wigle.wigleandroid.background.BackgroundGuiHandler.java
net.wigle.wigleandroid.background.FileUploaderListener.java
net.wigle.wigleandroid.background.FileUploaderTask.java
net.wigle.wigleandroid.background.HttpDownloader.java
net.wigle.wigleandroid.background.HttpFileUploader.java
net.wigle.wigleandroid.background.KmlWriter.java
net.wigle.wigleandroid.background.Status.java
net.wigle.wigleandroid.listener.BatteryLevelReceiver.java
net.wigle.wigleandroid.listener.GPSListener.java
net.wigle.wigleandroid.listener.PhoneState7.java
net.wigle.wigleandroid.listener.PhoneStateFactory.java
net.wigle.wigleandroid.listener.PhoneState.java
net.wigle.wigleandroid.listener.SsidSpeaker.java
net.wigle.wigleandroid.listener.WifiReceiver.java