/*
Tiamat - a role-playing game framework
StringTable.java - stores key-value pairs of strings
 
 Copyright  (C) 2004-2005 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat;

import com.huguesjohnson.serialization.Formatter;
import com.huguesjohnson.serialization.Storable;
import java.util.Enumeration;
import java.util.Properties;

/** StringTable - stores key-value pairs of strings
 * @author Hugues Johnson
 */
public class StringTable implements Storable, Cloneable{
    
/* begin private members */
    //underlying implementation
    private Properties table;
    /** Returns the Properties object this StringTable contains.
     * @return The Properties object this StringTable contains.
     */
    public Properties getProperties(){ 
        return(this.table); 
    }
    /** Sets the Properties object this StringTable contains.
     * @param table The new Properties object for this StringTable.
     */
    public void setProperties(Properties table){ 
        this.table=table; 
    }
/* end private members */

/* begin constructors */
    /** Creates an empty StringTable.
     */
    public StringTable(){
        this.table=new Properties();
    }
/* end constructors */

/* begin public methods */
    /** Sets an entry in the table.
     * @param key The key for the entry.
     * @param value The value for the entry.
     */
    public void set(String key,String value){
        this.table.setProperty(key,value);
    }
    /** Returns the entry with the specified key, defaultValue if not found. 
     * @param key The key for the entry to return.
     * @param defaultValue The default value if the key is not found.
     * @return The entry with the specified key, defaultValue if not found. 
     */
    public String get(String key,String defaultValue){
        return(this.table.getProperty(key,defaultValue));
    }
    /** Returns all key-value pairs in the collection, [0][n]=key for item n, [1][n]=value for item n. 
     * @return All key-value pairs in the collection.
     */
    public String[][] getAll(){
        String[][] allEntries;
        int length=this.table.size(); 
        allEntries=new String[2][length];
        Enumeration keys=this.table.propertyNames();
        int counter=0;
        while(keys.hasMoreElements()){
            String key=(String)keys.nextElement();
            allEntries[0][counter]=key;
            allEntries[1][counter]=this.table.getProperty(key);
            counter++;
        }
        return(allEntries);
    }       
    /** Returns a string representation of the object. 
     * @return A string representation of the object.
     */
    public String toString(){
        StringBuffer tostring=new StringBuffer(this.getClass().getName());
        tostring.append("\n  table="+this.table.toString());
        return(tostring.toString());
    }
    
    /** The onDeserialize event should be fired after an object is de-serialized.
     * This allows objects to load transient members, set their working directory, and load Storable members if needed.
     * @param formatter the formatter to use to load any storable members
     * @param absoluteDir the working directory since all paths stored in the object should be relative
     */    
    public void onDeserialize(Formatter formatter,String absoluteDir){ }

	public StringTable clone(){
		StringTable clone=new StringTable();
		clone.setProperties((Properties)this.table.clone());
		return(clone);
	}
    
/* end public methods */
}