eap.freeciv
Class ArrayField

java.lang.Object
  extended byeap.freeciv.Field
      extended byeap.freeciv.ArrayField

public class ArrayField
extends Field

Represents a set of repeated fields in a packet. An array is composed of zero or more elements. Each element is composed of one or more fields. Each element has the same fields. These fields can be of any type, including other arrays - though currently the freeciv packets don't do that.

When referencing the data in a packet, this class indicates the element by calling Field.addIndex(int). That means that in general, you indicate the element you want by appending "[index]" to the keyword for a datum in a packet.

There are three ways to specify the number of elements in an array.

  1. The array can have a fixed number of elements
  2. The number of elements can be specified by a field in the packet
  3. The array continues to the end of the packet. If you are not reading a packet, then the array continues until you reach an element for which at least one field is unset.


Constructor Summary
ArrayField(String repeat)
          Create a new array with the given repeat count.
 
Method Summary
 void addField(Field field)
          Append a field to the end of the list of fields in this array.
 void addIndex(int index)
          Add an index to this file.
 int getSize(Packet packet)
          Sum the sizes of all the elements in this array.
 boolean isSet(Packet packet)
          Returns true if all the fields for all the elements in the array are set in the given packet.
 void read(Packet packet, PacketSource source)
          Read the field from the given source and write its contents to the given packet.
 void removeIndex()
          calls removeIndex for all of the fields in this array.
 void setRepeat(String repeat)
          Sets the number of elements according to a given string.
 void write(Packet packet, OutputStream out)
          Write this field to the given stream, taking values from the given packet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayField

public ArrayField(String repeat)
Create a new array with the given repeat count. The repeat count is interpreted as by setRepeat(String).

Method Detail

addIndex

public void addIndex(int index)
Add an index to this file. This method just calls addIndex for all of the fields contained in the array. This method would only be used if an array contained another array, which currently doesn't happen in freeciv.

Specified by:
addIndex in class Field
See Also:
ArrayField

removeIndex

public void removeIndex()
calls removeIndex for all of the fields in this array.

Specified by:
removeIndex in class Field
See Also:
addIndex(int)

isSet

public boolean isSet(Packet packet)
Returns true if all the fields for all the elements in the array are set in the given packet. Note that if this array "fills" to the end of the packet, this method will always return true.

Specified by:
isSet in class Field

setRepeat

public void setRepeat(String repeat)
Sets the number of elements according to a given string. If the string can be parsed by Integer.parseInt(String), then it is interpreted as a fixed number of elements. If the string is null or empty, then the array continues to the end of the packet. Otherwise the string is assumed to be the name of a field in the packet. The value of this field will be interpreted as an integer indicating the repeat count.


addField

public void addField(Field field)
Append a field to the end of the list of fields in this array.


read

public void read(Packet packet,
                 PacketSource source)
          throws IOException
Description copied from class: Field
Read the field from the given source and write its contents to the given packet. Remember that a packet is just a storage repository and a structure (composed of fields) describes how the packet is serialized.

Specified by:
read in class Field
Throws:
IOException

write

public void write(Packet packet,
                  OutputStream out)
           throws IOException
Description copied from class: Field
Write this field to the given stream, taking values from the given packet.

Specified by:
write in class Field
Throws:
IOException

getSize

public int getSize(Packet packet)
Sum the sizes of all the elements in this array. Each element has a size equal to the sum of the sizes of its fields.

Specified by:
getSize in class Field