📄 rangedata.java
字号:
/*
* Created on 2005-4-14
*/
package cie.mobile.sick;
import java.io.*;
import name.lxm.robot.arch.Message;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
/**
* @author lxm
*/
public class RangeData implements Message
{
/* start angle of the range, in degree, relative to the sick itself */
private int start_angle;
/* end angle of the range, in degree, relative to the sick itself */
private int end_angle;
/* how many valid data in the buffer */
private int data_count;
/* the buffer that contains the valid data */
private int[] data;
/***
* Constructor.
* @param c total count of sickdata
* @param sa start angle
* @param ea end angle
*/
public RangeData(int c, int sa, int ea)
{
data = new int[c];
data_count = c;
start_angle = sa;
end_angle = ea;
}
public RangeData()
{
}
public int getLength()
{
return data_count;
}
public synchronized int[] getData(int a[])
{
if(a.length < data_count)
{
return null;
}
for(int i=0; i<data_count; i++)
{
a[i] = data[i];
}
return a;
}
/**
* Copy data from the given array to the local data buffer
* @param sickdata
*/
public synchronized void setData(int[] sickdata) {
for(int i=0; i<data_count; i++)
{
data[i] = sickdata[i];
}
}
public synchronized String toString()
{
StringWriter sw = new StringWriter();
for(int i=0; i<data_count-1; i++)
{
sw.write(Integer.toString(data[i]));
sw.write("-");
}
sw.write(Integer.toString(data[data_count-1]));
return sw.toString();
}
/**
* Serialize itself to an XML document, and send
* through the writer provided.
*
* @param w Writer - a writer through which the xml document will write to
* @throws IOException
*/
public void send2Writer(Writer w) throws IOException
{
Element e = new Element("RangeData");
Document doc = new Document(e);
e.setAttribute("start_angle", Integer.toString(start_angle));
e.setAttribute("end_angle", Integer.toString(end_angle));
e.setAttribute("data_count", Integer.toString(data_count));
Element child = new Element("data");
child.setText(toString());
e.addContent(child);
Format format = Format.getRawFormat();
format = format.setTextMode(Format.TextMode.TRIM);
XMLOutputter outer = new XMLOutputter(format);
outer.output(doc, w);
}
/**
* Serialize itself from an XML document,
* this documen will be recieved from the reader provided.
*
* @param r Reader - a reader through which the xml document will be recieved.
* @throws IOException
*/
public synchronized void loadFromReader(Reader r) throws IOException
{
SAXBuilder saxb = new SAXBuilder();
Document doc;
try{
doc = saxb.build(r);
Element e = doc.getRootElement();
if(e.getName().equals("RangeData"))
{
start_angle = Integer.parseInt(e.getAttributeValue("start_angle"));
end_angle = Integer.parseInt(e.getAttributeValue("end_angle"));
data_count = Integer.parseInt(e.getAttributeValue("data_count"));
Element child = e.getChild("data");
if(child != null)
{
String s = child.getText();
String[] v = s.split("-");
//to do - boundary checking
//initialize
data = new int[data_count];
for(int i=0; i<v.length; i++)
{
data[i] = Integer.parseInt(v[i]);
}
}
}
}catch(JDOMException e)
{
e.printStackTrace();
}
}
public int getStartAngle()
{
return start_angle;
}
public int getEndAngle()
{
return end_angle;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -