/**
 * Methods to simulate a Java Map.
 *
 * Version 1.3
 * - implement keySet( ) and values( )
 *
 * Version 1.2
 * - use === instead of == for testing key equality
 *
 * Version 1.1
 * - renamed set to put in order to follow java.util.Map conventions
 *
 * Version 1.0
 * 2005/07/10
 * - initial version
 *
 * @author Chris Fairbanks, <a href='http://www.williamsburger.com'>http://www.williamsburger.com</a>
 */


/**
 * Object to simulate Java's Map.Entry class.  This is a simple
 * key/value pair.
 */

function ArrayMapEntry( key, value ) {
  this.key = key;
  this.value = value;
}


/**
 * Put a value into the Map for the given key.
 */

function arrayMapPut( key, value ) {
  for ( var i = 0; i < this.size( ); i++ ) {
    if ( this._backingArray[ i ].key === key ) {
      this._backingArray[ i ].value = value;
      return;
    }
  }

  this._backingArray[ this.size( ) ] = new ArrayMapEntry( key, value );
}


/**
 * Given a key, return the value from the Map; if the key is not
 * present, return null.
 */

function arrayMapGet( key ) {
  for ( var i = 0; i < this.size( ); i++) {
    if ( this._backingArray[ i ].key === key ) {
      return this._backingArray[ i ].value;
    }
  }

  return null;
}


/**
 * Remove the key (and its value) from the Map.
 */

function arrayMapRemove( key )
{
  for ( var i = 0; i < this.size( ); i++ ) {
    var v = this._backingArray.pop( );

    if ( v.key === key ) {
      continue;
    }

    this._backingArray.unshift( v );
  }
}


/**
 * Return the size of this Map (the number of keys in it)
 */

function arrayMapSize( ) {
  return this._backingArray.length;
}


/**
 * Return whether or not this Map is empty (defined by having no keys)
 */

function arrayMapIsEmpty( ) {
  return this.size( ) <= 0;
}


/**
 * Return an Array of the ArrayMapEntry objects in this Map.
 */

function arrayMapEntrySet( ) {
  return this._backingArray;
}


/**
 * Return a set of the keys contained in this map.
 */

function arrayMapKeySet( ) {
  var keys = new Array( this.size );

  for ( i = 0; i < this._backingArray.length; i++ ) {
    keys[ i ] = this._backingArray[ i ].key;
  }

  return keys;
}


/**
 * Return a collection view of the values contained in this map.
 */

function arrayMapValues( ) {
  var values = new Array( this.size );

  for ( i = 0; i < this._backingArray.length; i++ ) {
    values[ i ] = this._backingArray[ i ].value;
  }

  return values;
}


/**
 * Create a new ArrayMap; map the function names from java.util.Map
 * onto the implementations here.
 */

function ArrayMap( ) {
  this._backingArray = new Array( );

  this.get = arrayMapGet;
  this.put = arrayMapPut;
  this.remove = arrayMapRemove;
  this.size = arrayMapSize;
  this.isEmpty = arrayMapIsEmpty;
  this.entrySet = arrayMapEntrySet;
  this.keySet = arrayMapKeySet;
  this.values = arrayMapValues;
}