package components.commchannel;
import java.awt.Color;
import java.util.Enumeration;
import java.util.Hashtable;
import utils.tracer.TraceState;
import components.features.Feature;
import components.features.sensor.color.ColorSensor;
import components.features.sensor.proximity.ProximitySensor;
/**
* La clase <code>CommunicationChannelState</code> representa el estado
* de un canal de comunicaciones, y que puede ser almacenado en una traza.
*/
public class CommunicationChannelState extends TraceState implements Cloneable
{
public Hashtable featuresHash;
private StringBuffer buff;
private String[] features;
private String[] fields;
private String[] colors;
public CommunicationChannelState()
{
featuresHash = new Hashtable(11);
buff = new StringBuffer(256);
}
public Object clone()
{
try
{
CommunicationChannelState newCommChannelState = (CommunicationChannelState)super.clone();
newCommChannelState.featuresHash = new Hashtable(11);
for (Enumeration e = featuresHash.keys(); e.hasMoreElements(); )
{
String featureName = (String)e.nextElement();
Feature feature = (Feature)featuresHash.get(featureName);
newCommChannelState.featuresHash.put( featureName , feature );
}
return newCommChannelState;
}
catch (CloneNotSupportedException e)
{
throw new Error("Esto nunca pasa.");
}
}
public void setValuesFromString(String values)
{
if ( values == null )
return;
features = values.split(",");
for (int i=0; i<features.length; i++)
{
fields = features[i].split( "=" );
if ( fields.length == 2 )
{
if ( fields[0].trim().equals("COLOR_SENSOR") )
{
// Sensor color
colors = fields[1].split( "\\." );
if ( colors.length == 3 )
{
Color color = new Color(Integer.valueOf(colors[0]).intValue(),
Integer.valueOf(colors[1]).intValue(),
Integer.valueOf(colors[2]).intValue());
featuresHash.put( fields[0].trim() , new ColorSensor("", "", color) );
}
}
else
{
// Sensor B/W
Boolean bool = new Boolean(fields[1].trim());
featuresHash.put( fields[0].trim() , new ProximitySensor("", "", bool) );
}
}
}
}
public StringBuffer getValuesToString()
{
buff.setLength(0); // Vac�a el buffer.
for (Enumeration e = featuresHash.keys(); e.hasMoreElements(); )
{
String featureName = (String)e.nextElement();
Feature feature = (Feature)featuresHash.get(featureName);
Object value = feature.getValue();
buff.append( featureName );
buff.append( "=" );
if ( featureName.trim().equals(CommunicationChannel.COLOR_SENSOR_NAME) )
{
if ( value != null )
buff.append( ((Color)value).getRed() + "." +
((Color)value).getGreen() + "." +
((Color)value).getBlue() );
else
buff.append( "0.0.0" );
}
else
{
if ( value != null )
buff.append( value );
else
buff.append( "false" );
}
buff.append( ", " );
}
buff.setLength( buff.length()-2 ); // Quita la �ltima coma.
return buff;
}
public String toString()
{
return getValuesToString().toString();
}
}