package org.apache.slide.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
 * Created on 01.12.2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

/**
 * @author olli
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class DFAMap implements Map {

	State startState = new State();

	/* (non-Javadoc)
	 * @see java.util.Map#size()
	 */
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#isEmpty()
	 */
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsKey(java.lang.Object)
	 */
	public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsValue(java.lang.Object)
	 */
	public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#get(java.lang.Object)
	 */
	public Object get(Object key) {
		return startState.get(key.toString().toCharArray(), 0);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
	 */
	public Object put(Object key, Object value) {
		return startState.put(key.toString().toCharArray(), 0, value);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#remove(java.lang.Object)
	 */
	public Object remove(Object key) {
		return startState.remove(key.toString().toCharArray(), 0);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#putAll(java.util.Map)
	 */
	public void putAll(Map t) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.util.Map#clear()
	 */
	public void clear() {
		startState = new State();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#keySet()
	 */
	public Set keySet() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#values()
	 */
	public Collection values() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#entrySet()
	 */
	public Set entrySet() {
		// TODO Auto-generated method stub
		return null;
	}
	private class State {
		Object value = null;
		Object key = null;

		// XXX uses HashMap itself
		// Character --> State
		Map transitions = new HashMap();
 
		State transit(Character c) {
			return (State) transitions.get(c);
		}

		void putTransition(Character c, State state) {
			transitions.put(c, state);
		}

		Object get(char[] key, int pos) {
			if (pos == key.length) {
				return value;
			} else {
				Character c = new Character(key[pos]);
				State nextState = transit(c);
				if (nextState == null) {
					return null;
				} else {
					return nextState.get(key, pos + 1);
				}

			}
		}

		Object remove(char[] key, int pos) {
			// XXX simply overwrite
			return put(key, pos, null);
		}

		Object put(char[] key, int pos, Object value) {
			if (pos == key.length) {
				this.key = new String(key);
				Object oldValue = this.value;
				this.value = value;
				return oldValue;
			} else {
				Character c = new Character(key[pos]);
				State nextState = transit(c);
				if (nextState == null) {
					nextState = new State();
					putTransition(c, nextState);
				}
				return nextState.put(key, pos + 1, value);
			}
		}
	}

}
