java-org.hwo/src/org/hwo/bitfields/BitField.java

85 lines
1.6 KiB
Java
Raw Normal View History

2014-07-25 00:29:44 +02:00
package org.hwo.bitfields;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class BitField {
private List<Field> fields;
public BitField()
{
this.fields = new ArrayList<Field>();
initialize();
}
public BitField(Element fieldsNode)
{
this.fields = new ArrayList<Field>();
2014-07-25 10:02:06 +02:00
if (fieldsNode != null)
2014-07-25 00:29:44 +02:00
{
2014-07-25 10:02:06 +02:00
NodeList fields = fieldsNode.getElementsByTagName("Field");
for (int i=0;i<fields.getLength();i++)
{
Element fieldNode = (Element)fields.item(i);
int start = 0,
len = 1;
if (!fieldNode.getAttribute("len").equals(""))
len = Integer.decode(fieldNode.getAttribute("len"));
if (!fieldNode.getAttribute("start").equals(""))
start = Integer.decode(fieldNode.getAttribute("start"));
this.fields.add(new Field(this,start,len,fieldNode.getTextContent()));
}
2014-07-25 00:29:44 +02:00
}
}
private void initialize()
{
for (int i=0;i<32;i++)
this.fields.add(new Field(this, i, 1));
}
2014-07-25 10:02:06 +02:00
public synchronized String toText(Integer value)
2014-07-25 00:29:44 +02:00
{
StringBuilder sb = new StringBuilder();
if (value != null)
{
for (Field field:this.fields)
{
2014-07-25 10:02:06 +02:00
String st = field.toText(value);
2014-07-25 00:29:44 +02:00
if (st != null)
{
if (sb.length() > 0)
sb.append(", ");
sb.append(st);
}
}
}
return sb.toString();
}
2014-07-25 23:33:15 +02:00
public Field[] getFields()
{
return this.fields.toArray(new Field[0]);
}
2014-07-25 00:29:44 +02:00
2014-07-25 10:02:06 +02:00
/* public synchronized void setValue(int value,int start,int len)
2014-07-25 00:29:44 +02:00
{
if (this.value == null)
this.value = 0;
this.value &= ~((-1 >> (Integer.SIZE - len)) << start);
this.value |= (value & (-1 >> (Integer.SIZE - len))) << start;
}
2014-07-25 10:02:06 +02:00
*/
2014-07-25 00:29:44 +02:00
}