Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/main/java/com/igormaznitsa/jbbp/model/JBBPAbstractField.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import java.io.Serializable;
import java.util.Map;

/**
* The Class is the ancestor for all fields and arrays of fields.
* @since 1.0
*/
public class JBBPAbstractField implements Serializable {
public abstract class JBBPAbstractField implements Serializable {
private static final long serialVersionUID = 8142829902016660630L;

/**
Expand All @@ -45,7 +46,55 @@ public JBBPAbstractField(final JBBPNamedFieldInfo namedField){
public JBBPNamedFieldInfo getNameInfo(){
return this.fieldNameInfo;
}


/**
* Get key in order to store in a {@link Map}. If the field is named, the name is
* returned as the key, otherwise a key is generated based on the field type and
* the index within the enclosing array of fields.
*
* @param index the index of the field within the enclosing array of fields
* @return the key
*/
protected String getKey(int index) {

if (getNameInfo() != null) {
return getNameInfo().getFieldName();
} else {
return getKeyPrefix() + "_" + index;
}
}

/**
* Returns the key prefix used for generated keys.
*
* @return the key prefix
*/
protected abstract String getKeyPrefix();

/**
* Returns an object representation of the field and its children. The intent
* of this method is to produce a hierarchy consisting only of Java primitive
* Objects (e.g., Boolean, Long, Integer, String) and collection (e.g., Map,
* List) classes that can be converted to other representations, such as JSON
* or XML. The contract for the return Object is as follows:
*
* <ul>
* <li>{@link java.lang.Boolean} - for <code>bool</code> fields</li>
* <li>extending from {@link java.lang.Number} - for numeric fields (i.e.,
* <code>bit, byte, ubyte, short, ushort, int, long</code>, or other numeric
* custom fields)</li>
* <li>{@link java.lang.String} - for custom fields that read strings</li>
* <li>{@link java.util.List} - for arrays of primitive types</li>
* <li>{@link java.util.Map} - for structs that contain other structs and/or
* fields</li>
* </ul>
*
* New custom field types must adhere to this contract.
*
* @return the value object
*/
protected abstract Object getValue();

/**
* Get the field path.
* @return the field path or null if the field doesn't contain any field name info
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldArrayBit.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes an array contains bit fields.
* @since 1.0
Expand Down Expand Up @@ -61,6 +64,20 @@ public byte[] getArray() {
return this.array.clone();
}

@Override
protected String getKeyPrefix() {
return "array_bit";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (byte b : getArray()) {
valueList.add((int)b);
}
return valueList;
}

/**
* Get the valuable bit number of values in the array.
* @return the valuable bit number, must not be null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes an array of boolean values.
* @since 1.0
Expand All @@ -39,15 +42,28 @@ public JBBPFieldArrayBoolean(final JBBPNamedFieldInfo name, final boolean [] arr
JBBPUtils.assertNotNull(array, "Array must not be null");
this.array = array;
}

/**
* Get values of the array.
* @return values as a boolean array
*/
public boolean [] getArray(){
return this.array.clone();
}


@Override
protected String getKeyPrefix() {
return "array_boolean";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (boolean bool : getArray()) {
valueList.add(bool);
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldArrayByte.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a byte array.
* @since 1.0
Expand All @@ -41,6 +44,20 @@ public byte[] getArray(){
return this.array.clone();
}

@Override
protected String getKeyPrefix() {
return "array_byte";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (byte b : getArray()) {
valueList.add((int)b);
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldArrayInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes an array of integers.
* @since 1.0
Expand Down Expand Up @@ -47,7 +50,21 @@ public JBBPFieldArrayInt(final JBBPNamedFieldInfo name, final int[] array) {
public int [] getArray(){
return this.array.clone();
}


@Override
protected String getKeyPrefix() {
return "array_int";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (int i : getArray()) {
valueList.add(i);
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldArrayLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a long array.
* @since 1.0
Expand Down Expand Up @@ -48,6 +51,20 @@ public JBBPFieldArrayLong(final JBBPNamedFieldInfo name,final long[] array) {
return this.array.clone();
}

@Override
protected String getKeyPrefix() {
return "array_long";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (long l : getArray()) {
valueList.add(l);
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a short array.
* @since 1.0
Expand Down Expand Up @@ -47,7 +50,21 @@ public JBBPFieldArrayShort(final JBBPNamedFieldInfo name, final short[] array) {
public short [] getArray(){
return this.array.clone();
}


@Override
protected String getKeyPrefix() {
return "array_short";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (short sh : getArray()) {
valueList.add((int)sh);
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a structure array. It doesn't support operations to get an array value as a numeric one.
* @since 1.0
Expand Down Expand Up @@ -47,7 +50,21 @@ public JBBPFieldArrayStruct(final JBBPNamedFieldInfo name, final JBBPFieldStruct
public JBBPFieldStruct [] getArray(){
return this.structs.clone();
}


@Override
protected String getKeyPrefix() {
return "array_struct";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (JBBPFieldStruct struct : getArray()) {
valueList.add(struct.getValue());
}
return valueList;
}

@Override
public int size() {
return this.structs.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a unsigned byte array.
* @since 1.0
Expand All @@ -40,7 +43,21 @@ public JBBPFieldArrayUByte(final JBBPNamedFieldInfo name, final byte[] array) {
public byte [] getArray(){
return this.array.clone();
}


@Override
protected String getKeyPrefix() {
return "array_ubyte";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (int i = 0; i < size(); i++) {
valueList.add(getAsInt(i));
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import com.igormaznitsa.jbbp.utils.JBBPUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Describes a unsigned short array.
* @since 1.0
Expand Down Expand Up @@ -48,6 +51,20 @@ public JBBPFieldArrayUShort(final JBBPNamedFieldInfo name, final short[] array)
return this.array.clone();
}

@Override
protected String getKeyPrefix() {
return "array_ushort";
}

@Override
protected Object getValue() {
List<Object> valueList = new ArrayList<Object>();
for (int i = 0; i < size(); i++) {
valueList.add(getAsInt(i));
}
return valueList;
}

@Override
public int size() {
return this.array.length;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldBit.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ public JBBPFieldBit(final JBBPNamedFieldInfo name,final int value, final JBBPBit
this.bitNumber = bitNumber;
this.value = value;
}


@Override
protected String getKeyPrefix() {
return "field_bit";
}

@Override
protected Object getValue() {
return getAsInt();
}

/**
* Get number of valuable bits in the value. It plays informative role and doesn't play role during numeric value getting.
* @return the number of valuable bits in the value.
Expand Down
Loading