/* MIT ICT4DLab SMS Lab framework
 * 
 * Copyright (C) 2008, Massachusetts Institute of Technology
 * 
 * ICT4DLab SMS is distributed under the terms of the Apache License version 2.0
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * original author: Luis F. G. Sarmenta ( lfgs @ mit . edu ) 
 * v0.1.0: 20080228
 * 
 * This particular file is partly based on an original design from ADMUCMP, 
 * copyright 2003 by Ateneo Java Wireless Competency Center, 
 * Ateneo de Manila University, Philippines
 * Used with permission.
 * 
 * Last modified by:
 * date:
 * comment:
 */
package org.ict4dlab.sms.core;

import java.util.Enumeration;
import java.util.Hashtable;

public class SMSMessage {

    private String destAddr;
    private String srcAddr;
    private String text;
    private Hashtable attribs = null;

    /**
     * Constructor
     * 
     * @param srcAddr
     * @param destAddr
     * @param text
     */
    public SMSMessage(String srcAddr, String destAddr, String text) {
	super();
	this.destAddr = destAddr;
	this.srcAddr = srcAddr;
	this.text = text;
    }

    public String getDestAddr() {
	return destAddr;
    }

    public void setDestAddr(String destAddr) {
	this.destAddr = destAddr;
    }

    public String getSrcAddr() {
	return srcAddr;
    }

    public void setSrcAddr(String srcAddr) {
	this.srcAddr = srcAddr;
    }

    public String getText() {
	return text;
    }

    public void setText(String text) {
	this.text = text;
    }

    public void setAttrib(String key, Object value) {
	if (this.attribs == null) {
	    this.attribs = new Hashtable();
	}

	this.attribs.put(key, value);
    }

    public Object getAttrib(String key) {
	if (this.attribs == null) {
	    return null;
	} else {
	    return this.attribs.get(key);
	}

    }

    /**
     * Gets an attribute that should be a String. Returns null if the attribute
     * value doesn't exist or if it is not a String.
     * 
     * @param key
     * @return attribute value, or null if non-existent or non-String
     */
    public String getAttribString(String key) {
	Object o = this.getAttrib(key);
	if (o instanceof String) {
	    return (String) o;
	} else {
	    return null;
	}
    }

    public String toString() {
	String s = "SMSMessage\n" + "from: " + srcAddr + "\n" + "to: "
		+ destAddr + "\n" + "msg: " + text;
	if ((this.attribs != null) && (!this.attribs.isEmpty())) {
	    Enumeration keys = this.attribs.keys();
	    while (keys.hasMoreElements()) {
		Object o = keys.nextElement();
		s = s + "\n" + o + ": " + attribs.get(o);
	    }
	}
	return s;
    }
}
