Location: PHPKode > scripts > RogePHP > RoguePHP-0.9.4/RoguePHP-0.9.4/types/Map.php
<?php
/**
 * @author Cory Marsh
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 */

require_once ROGUE_DIR . '/types/List.php';

interface Map
{
		/**
	 * Removes all mappings from this map (optional operation).
	 */
	public function clear();

	/**
	 * Returns true if this map contains a mapping for the specified key.
	 * @return bool true if map contains the key
	 */
 	public function containsKey($key);

	/**
	 * Returns true if this map maps one or more keys to the specified value.
	 * @return bool true if this map maps one or more keys to the specified value.
	 */
	public function containsValue($value);

	/**
	 * retrun a list of all the keys in the map
	 */
	public function getKeys();

	/**
	 * return a list of all of the values in the map
	 */
	public function getValues();

        /**
	 * Returns the value to which this map maps the specified key.
	 * @return Object the value associated with this key
	 */
	public function get($key);

	/**
         * Returns true if this map contains no key-value mappings.
	 * @retrun true if the map is empty
	 */
 	public function isEmpty();

        /**
	 * Associates the specified value with the specified key in this map (optional operation).
	 * @param Object $key - key with which the specified value is to be associated.
    	 * @param Object $value - value to be associated with the specified key.
	 */
 	public function	put($key, $value);

        /**
	 * Removes the mapping for this key from this map if it is present (optional operation).
	 * @param object $key the entry to remove 
	 */
	public function	remove($key);

	/**
	 * Returns the number of key-value mappings in this map.
	 */
	public function size();
}


class HashMap implements Map
{
	// the data storage (PHP array)
	private $map;

	// the type of key we allow (or leave null, for any Object)
	private $keyType;
	// the type of value we allow
	private $valueType;

	public function __construct($keyType = null, $valueType = null)
	{
		$this->keyType = null;
		$this->valueType = null;
		$this->map = array();
		
		if($keyType != null)
			$this->setKeyType($keyType);
		if($valueType != null)
			$this->setValueType($valueType);
	}

        /**
	 * Removes all mappings from this map (optional operation).
	 */
	public function clear()
	{
		$this->map = array();
	}

	/**
	 * Returns true if this map contains a mapping for the specified key.
	 * @return bool true if map contains the key, else false
	 */
 	public function containsKey($key)
	{
		if($this->keyType != null && gettype($key) != $this->keyType && !($key instanceof $this->keyType))
			throw new Exception('cannot test a type ' . gettype($key) . " element to HashMap of key type {$this->keyType}");

                if (isset($this->map[$key]))
                {
                    return true;
                }

		return false;
	}

	/**
	 * Returns true if this map maps one or more keys to the specified value.
	 * @return bool true if this map maps one or more keys to the specified value.
	 */
	public function containsValue($value)
	{
		if($this->valueType != null && gettype($key) != $this->valueType && !($key instanceof $this->valueType))
			throw new Exception('cannot test a value type ' . gettype($value) . " element to HashMap of value type {$this->valueType}");
		$values = array_values($this->map);
		foreach($values as $val)
		{
			if($val == $value)
				return true;
		}
		return false;
	}

	/**
	 * retrun a list of all the keys in the map
	 * @return List all the keys in the map
	 */
	public function getKeys()
	{
		$mapKeys = array_keys($this->map);
		$keys = new ArrayList($this->keyType);
		$keys->setArray($mapKeys);
		return $keys;
	}

	/**
	 * return a list of all of the values in the map
	 * @return List all the keys in the map
	 */
	public function getValues()
	{
		$keys = new ArrayList($this->valueType);
		$keys->setArray(array_values($this->map));
		return $keys;
	}

    /**
	 * Returns the value to which this map maps the specified key.
	 * @return mixed the value associated with this key, else false
	 */
	public function get($key)
	{
		if($this->keyType != null && gettype($key) != $this->keyType && !($key instanceof $this->keyType))
			throw new Exception('cannot get a type ' . gettype($key) . " element from HashMap of type {$this->keyType}");
		if (isset($this->map[$key]))
			return $this->map[$key];
		return null;
	}

	/**
     * Returns true if this map contains no key-value mappings.
	 * @retrun true if the map is empty
	 */
 	public function isEmpty()
	{
		if (count($this->map) < 1)
			return true;
		return false;
	}

    /**
	 * Associates the specified value with the specified key in this map (optional operation).
	 * @param Object $key - key with which the specified value is to be associated.
    	 * @param Object $value - value to be associated with the specified key.
	 */
 	public function	put($key, $value)
	{
		if($this->keyType != null && gettype($key) != $this->keyType && !($key instanceof $this->keyType))
			throw new Exception('cannot put a key type ' . gettype($key) . " element from HashMap of type {$this->keyType}");
		if($this->valueType != null && gettype($key) != $this->valueType && !($key instanceof $this->valueType))
			throw new Exception('cannot put a value type ' . gettype($value) . " element to HashMap of value type {$this->valueType}");
		$this->map[$key] = $value;
	}

        /**
	 * Removes the mapping for this key from this map if it is present (optional operation).
	 * @param object $key the entry to remove 
	 */
	public function	remove($key)
	{
		if($this->keyType != null && gettype($key) != $this->keyType && !($key instanceof $this->keyType))
			throw new Exception('cannot put a type ' . gettype($key) . " element from HashMap of type {$this->keyType}");
		unset($this->map[$key]);
	}

	/**
	 * Returns the number of key-value mappings in this map.
	 */
	public function size()
	{
		return (count($this->map));
	}

	/**
	 * set the type of Object this List accepts
	 * @param String the type to set this list to (as returned by gettype();
	 */
	public function setKeyType($type)
	{
		$this->keyType = $type;
	}


	/**
	 * set the type of Object this List accepts
	 * @param String the type to set this list to (as returned by gettype();
	 */
	public function setValueType($type)
	{
		$this->valuetype = $type;
	}
	
	/**
	 * get the type of Object this List accepts
	 * @return String the type to set this list to (as returned by gettype();
	 */
	public function getType()
	{
		return $this->type;
	}
}
Return current item: RogePHP