homegear
Class HomeGearArray

java.lang.Object
  extended by homegear.HomeGearArray
Direct Known Subclasses:
HomeGearResponse

public class HomeGearArray
extends java.lang.Object

Representation of the homegear RPC arrays and structs. A Usage demonstration is given in main(String[]).


Field Summary
protected  byte[] data
           
protected  int length
           
protected  int offs
           
static int TYPE_ARRAY
          Array identifier
static int TYPE_BASE64
          BASE64 identifier
static int TYPE_BOOL
          Boolean identifier
static int TYPE_FLOAT
          Float identifier
static int TYPE_INT
          Integer identifier
static int TYPE_STRING
          String identifier
static int TYPE_STRUCT
          Struct identifier
 
Constructor Summary
HomeGearArray(boolean struct, byte[] data_, int offs_)
          Constructs an array or struct from a raw data.
HomeGearArray(java.lang.Object... vals)
          Constructs an array from a list of variables.
HomeGearArray(java.lang.String[] names, java.lang.Object[] vals)
          Constructs an array or a struct from a list of variables
 
Method Summary
 boolean boolValue(int idx)
          Returns a boolean element.
 HomeGearArray child(int idx)
          Returns child array or struct.
protected  void compress(byte[] data_, int offs_)
          Replaces data buffer and releases duplicate buffers of children.
 HomeGearArray copy()
          This makes a copy of the instance.
 double doubleValue(int idx)
          Returns value of a float or integer object.
 int find(java.lang.String name)
          Returns the index of a element identified by field name.
protected  int intEncode(int pos)
          Encodes integer from data at a given position.
 int intValue(int idx)
          Returns a integer element.
 boolean isStruct()
          Returns true if it is a struct, i.e. if elements have names.
static void main(java.lang.String[] args)
           
 java.lang.String name(int idx)
          Returns the name of an element or null if its an array.
 int size()
          Returns number of elements.
 java.lang.String stringValue(java.lang.String lb, int idx)
          Returns a string representation of a element.
 java.lang.String toString()
          Returns a string representation.
 java.lang.String toString(java.lang.String lb)
          Returns a string representation.
 int type(int idx)
          Returns the type of an element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

protected byte[] data

offs

protected int offs

length

protected int length

TYPE_INT

public static final int TYPE_INT
Integer identifier

See Also:
Constant Field Values

TYPE_BOOL

public static final int TYPE_BOOL
Boolean identifier

See Also:
Constant Field Values

TYPE_STRING

public static final int TYPE_STRING
String identifier

See Also:
Constant Field Values

TYPE_FLOAT

public static final int TYPE_FLOAT
Float identifier

See Also:
Constant Field Values

TYPE_BASE64

public static final int TYPE_BASE64
BASE64 identifier

See Also:
Constant Field Values

TYPE_ARRAY

public static final int TYPE_ARRAY
Array identifier

See Also:
Constant Field Values

TYPE_STRUCT

public static final int TYPE_STRUCT
Struct identifier

See Also:
Constant Field Values
Constructor Detail

HomeGearArray

public HomeGearArray(java.lang.String[] names,
                     java.lang.Object[] vals)
              throws java.lang.IllegalArgumentException,
                     java.io.IOException
Constructs an array or a struct from a list of variables

Parameters:
names - The field names. If length of names is > 0 and names != null its a struct, otherwise an array.
vals - Values of the struct or array.
Throws:
java.lang.IllegalArgumentException - if a parameter is invalid.
java.io.IOException - if an I/O error occurs.

HomeGearArray

public HomeGearArray(java.lang.Object... vals)
              throws java.lang.IllegalArgumentException,
                     java.io.IOException
Constructs an array from a list of variables.

Parameters:
vals - Values of the array.
Throws:
java.lang.IllegalArgumentException - if a parameter is invalid.
java.io.IOException - if an I/O error occurs.

HomeGearArray

public HomeGearArray(boolean struct,
                     byte[] data_,
                     int offs_)
              throws java.lang.IllegalArgumentException
Constructs an array or struct from a raw data. The data is not copied.

Parameters:
struct - True if it is a struct.
data_ - Buffer containing data.
offs_ - Offset of data.
Throws:
java.lang.IllegalArgumentException - if a parameter is invalid.
Method Detail

copy

public HomeGearArray copy()
This makes a copy of the instance. If instances are created from raw data using the constructor the data buffer is re-used in order to save resources. This method copies the part of data buffer which is used by the instance and creates a new one from it. If only a small fraction of a large data set need to be saved this method should be used.

Returns:
a copy of this instance.

compress

protected void compress(byte[] data_,
                        int offs_)
Replaces data buffer and releases duplicate buffers of children.

Parameters:
data_ - New data buffer.
offs_ - New offset.

size

public int size()
Returns number of elements.

Returns:
number of elements.

isStruct

public boolean isStruct()
Returns true if it is a struct, i.e. if elements have names.

Returns:
true if it is a struct.

intEncode

protected int intEncode(int pos)
                 throws java.lang.IllegalArgumentException
Encodes integer from data at a given position.

Parameters:
pos - The position.
Returns:
the integer.
Throws:
java.lang.IllegalArgumentException - if Index is out of bounds.

find

public int find(java.lang.String name)
         throws java.io.UnsupportedEncodingException
Returns the index of a element identified by field name. Result is -1 if the HomeGearArray is not a struct or if element was not found

Parameters:
name - The name of the field.
Returns:
the index of a element identified by field name.
Throws:
java.io.UnsupportedEncodingException - if your computer does not support ASCII.

type

public int type(int idx)
         throws java.lang.IndexOutOfBoundsException
Returns the type of an element.

Parameters:
idx - The element index.
Returns:
type of the element.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.

boolValue

public boolean boolValue(int idx)
                  throws java.lang.IndexOutOfBoundsException,
                         java.lang.IllegalArgumentException
Returns a boolean element.

Parameters:
idx - The element index.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.lang.IllegalArgumentException - if element type is invalid.

intValue

public int intValue(int idx)
             throws java.lang.IndexOutOfBoundsException,
                    java.lang.IllegalArgumentException
Returns a integer element.

Parameters:
idx - The element index.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.lang.IllegalArgumentException - if element type is invalid.

doubleValue

public double doubleValue(int idx)
                   throws java.lang.IndexOutOfBoundsException,
                          java.lang.IllegalArgumentException
Returns value of a float or integer object.

Parameters:
idx - The element index.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.lang.IllegalArgumentException - if element type is invalid.

child

public HomeGearArray child(int idx)
                    throws java.lang.IndexOutOfBoundsException,
                           java.lang.IllegalArgumentException
Returns child array or struct.

Parameters:
idx - the element index.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.lang.IllegalArgumentException - if element type is invalid.

stringValue

public java.lang.String stringValue(java.lang.String lb,
                                    int idx)
                             throws java.lang.IndexOutOfBoundsException,
                                    java.lang.IllegalArgumentException,
                                    java.io.UnsupportedEncodingException
Returns a string representation of a element.

Parameters:
idx - the element index.
lb - line break string (including indent).
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.lang.IllegalArgumentException - if element type is invalid.
java.io.UnsupportedEncodingException - if your computer does not support ASCII.

name

public java.lang.String name(int idx)
                      throws java.lang.IndexOutOfBoundsException,
                             java.io.UnsupportedEncodingException
Returns the name of an element or null if its an array.

Parameters:
idx - the element index.
Throws:
java.lang.IndexOutOfBoundsException - if Index is out of bounds.
java.io.UnsupportedEncodingException - if your computer does not support ASCII.

toString

public java.lang.String toString(java.lang.String lb)
Returns a string representation.

Parameters:
lb - line break string (including indent).
Returns:
a string representation.

toString

public java.lang.String toString()
Returns a string representation.

Overrides:
toString in class java.lang.Object
Returns:
a string representation.
Throws:
java.lang.IllegalArgumentException - if data is corrupt.

main

public static void main(java.lang.String[] args)