rescuecore
Class Handy

java.lang.Object
  extended byrescuecore.Handy
All Implemented Interfaces:
RescueConstants

public class Handy
extends java.lang.Object
implements RescueConstants

A collection of useful methods


Field Summary
 
Fields inherited from interface rescuecore.RescueConstants
AGENT_TYPE_AMBULANCE_CENTER, AGENT_TYPE_AMBULANCE_TEAM, AGENT_TYPE_ANY, AGENT_TYPE_ANY_AGENT, AGENT_TYPE_ANY_BUILDING, AGENT_TYPE_ANY_MOBILE, AGENT_TYPE_CIVILIAN, AGENT_TYPE_FIRE_BRIGADE, AGENT_TYPE_FIRE_STATION, AGENT_TYPE_POLICE_FORCE, AGENT_TYPE_POLICE_OFFICE, AK_ACKNOWLEDGE, AK_CLEAR, AK_CONNECT, AK_EXTINGUISH, AK_LOAD, AK_MOVE, AK_RESCUE, AK_REST, AK_SAY, AK_TELL, AK_UNLOAD, BYTE_SIZE, COMMAND_ACKNOWLEDGE, COMMAND_CLEAR, COMMAND_CONNECT, COMMAND_EXTINGUISH, COMMAND_LOAD, COMMAND_MOVE, COMMAND_RESCUE, COMMAND_REST, COMMAND_SAY, COMMAND_TELL, COMMAND_UNLOAD, FIERYNESS_BURNING, FIERYNESS_BURNT_OUT, FIERYNESS_HEATING, FIERYNESS_INFERNO, FIERYNESS_MODERATELY_BURNT, FIERYNESS_NOT_BURNT, FIERYNESS_SLIGHTLY_BURNT, FIERYNESS_VERY_BURNT, GK_CONNECT_ERROR, GK_CONNECT_OK, HEADER_NULL, INT_SIZE, INTERNAL_TYPE_AMBULANCE_CENTER, INTERNAL_TYPE_AMBULANCE_TEAM, INTERNAL_TYPE_ANY_BUILDING, INTERNAL_TYPE_ANY_HUMANOID, INTERNAL_TYPE_ANY_PLATOON, INTERNAL_TYPE_BUILDING, INTERNAL_TYPE_CAR, INTERNAL_TYPE_CIVILIAN, INTERNAL_TYPE_FIRE_BRIGADE, INTERNAL_TYPE_FIRE_STATION, INTERNAL_TYPE_NODE, INTERNAL_TYPE_POLICE_FORCE, INTERNAL_TYPE_POLICE_OFFICE, INTERNAL_TYPE_REFUGE, INTERNAL_TYPE_RIVER, INTERNAL_TYPE_RIVER_NODE, INTERNAL_TYPE_ROAD, INTERNAL_TYPE_WORLD, KA_CONNECT_ERROR, KA_CONNECT_OK, KA_HEAR, KA_SENSE, KG_ACKNOWLEDGE, KG_CONNECT, KG_UPDATE, KS_COMMANDS, KS_CONNECT_ERROR, KS_CONNECT_OK, KS_UPDATE, KV_CONNECT_ERROR, KV_CONNECT_OK, KV_UPDATE, MAX_CENTER_MESSAGES, MAX_EXTINGUISH_DISTANCE, MAX_EXTINGUISH_POWER, MAX_HP, MAX_PLATOON_MESSAGES, MAX_RESCUE_DISTANCE, MAX_WATER, PROPERTY_BLOCK, PROPERTY_BROKENNESS, PROPERTY_BUILDING_APEXES, PROPERTY_BUILDING_AREA_GROUND, PROPERTY_BUILDING_AREA_TOTAL, PROPERTY_BUILDING_ATTRIBUTES, PROPERTY_BUILDING_CODE, PROPERTY_BUILDING_SHAPE_ID, PROPERTY_BURIEDNESS, PROPERTY_CARS_PASS_TO_HEAD, PROPERTY_CARS_PASS_TO_TAIL, PROPERTY_DAMAGE, PROPERTY_DIRECTION, PROPERTY_EDGES, PROPERTY_ENTRANCES, PROPERTY_FIERYNESS, PROPERTY_FLOORS, PROPERTY_HEAD, PROPERTY_HP, PROPERTY_HUMANS_PASS_TO_HEAD, PROPERTY_HUMANS_PASS_TO_TAIL, PROPERTY_IGNITION, PROPERTY_LATITUDE, PROPERTY_LENGTH, PROPERTY_LINES_TO_HEAD, PROPERTY_LINES_TO_TAIL, PROPERTY_LONGITUDE, PROPERTY_MAX, PROPERTY_MEDIAN_STRIP, PROPERTY_NULL, PROPERTY_POCKET_TO_TURN_ACROSS, PROPERTY_POSITION, PROPERTY_POSITION_EXTRA, PROPERTY_POSITION_HISTORY, PROPERTY_REPAIR_COST, PROPERTY_ROAD_KIND, PROPERTY_SHORTCUT_TO_TURN, PROPERTY_SIGNAL, PROPERTY_SIGNAL_TIMING, PROPERTY_STAMINA, PROPERTY_START_TIME, PROPERTY_STRETCHED_LENGTH, PROPERTY_TAIL, PROPERTY_TYPE_BOOLEAN, PROPERTY_TYPE_INT, PROPERTY_TYPE_INT_ARRAY, PROPERTY_WATER_QUANTITY, PROPERTY_WIDTH, PROPERTY_WIDTH_FOR_WALKERS, PROPERTY_WIND_DIRECTION, PROPERTY_WIND_FORCE, PROPERTY_X, PROPERTY_Y, SAY_LENGTH, SHORT_SIZE, SK_ACKNOWLEDGE, SK_CONNECT, SK_UPDATE, TELL_LENGTH, TYPE_AMBULANCE_CENTER, TYPE_AMBULANCE_TEAM, TYPE_BUILDING, TYPE_CAR, TYPE_CIVILIAN, TYPE_FIRE_BRIGADE, TYPE_FIRE_STATION, TYPE_NODE, TYPE_NULL, TYPE_POLICE_FORCE, TYPE_POLICE_OFFICE, TYPE_REFUGE, TYPE_RIVER, TYPE_RIVER_NODE, TYPE_ROAD, TYPE_WORLD, VK_ACKNOWLEDGE, VK_CONNECT
 
Method Summary
static java.lang.String arrayAsString(int[] array)
          Turn an array of integers into a String containing a comma-seperated list of numbers
static byte decodeByte(byte[] buffer, int off)
          Decode a byte from a buffer
static byte[] decodeBytes(byte[] buffer, int off, int length)
          Decode a byte array from a buffer
static int decodeInt(byte[] buffer, int off)
          Decode an int from a buffer
static short decodeShort(byte[] buffer, int off)
          Decode a short from a buffer
static java.lang.String decodeString(byte[] buffer, int off, int length)
          Decode a String from a buffer
static byte[] encodeByte(int value)
          Encode a byte into a byte array
static void encodeByte(int value, byte[] buf, int off)
          Encode a byte into a buffer
static void encodeBytes(byte[] bytes, byte[] buf, int off)
          Encode a byte arrray into a buffer
static void encodeBytes(byte[] bytes, int bytesOffset, int bytesLength, byte[] buf, int off)
          Encode part of a byte array into a buffer
static byte[] encodeInt(int value)
          Encode an int into a byte array
static void encodeInt(int value, byte[] buf, int off)
          Encode an int into a buffer
static byte[] encodeShort(int value)
          Encode a short into a byte array
static void encodeShort(int value, byte[] buf, int off)
          Encode a short into a buffer
static byte[] encodeString(java.lang.String value, int length)
          Encode a String into a byte array
static void encodeString(java.lang.String value, int length, byte[] buf, int off)
          Encode a String into a buffer
static java.lang.String getAgentTypeName(int type)
          Turn an agent type into a human-readable String
static java.lang.String getCommandTypeName(int header)
          Translate a command type into a human-readable string
static java.lang.String getPropertyName(int type)
          Translate a property name into a human-readable string
static java.lang.String getTypeName(int type)
          Translate a type name into a human-readable string
static java.lang.String hex(byte b)
          Turn a byte into a hexadecimal String
static boolean isDifferent(int[] a, int[] b)
          Find out if two int arrays are different.
static void printBytes(byte[] data)
          Print an array of bytes to System.out in a nice way
static void printBytes(int startIndex, byte[] data, java.lang.String description)
          Print four bytes to System.out in a nice way
static void printBytes(java.lang.String header, byte[] data)
          Print an array of bytes to System.out in a nice way
static void printCommand(Command c)
          Write out the contents and description of a command - this is useful for debugging
static void printKA_CONNECT_OK(byte[] data)
          Write out the contents and description of a KA_CONNECT_OK command
static void printKA_SENSE(byte[] data)
          Write out the contents and description of a KA_SENSE command
static int printObject(int index, byte[] data)
          Write out the contents of a RescueObject update with the relevant bytes
static int printProperty(int index, byte[] data)
          Write out the contents of a property update with the relevant bytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

printBytes

public static void printBytes(byte[] data)
Print an array of bytes to System.out in a nice way

Parameters:
data - The bytes to print out

printBytes

public static void printBytes(java.lang.String header,
                              byte[] data)
Print an array of bytes to System.out in a nice way

Parameters:
header - A string to print out as a title
data - The bytes to print out

printCommand

public static void printCommand(Command c)
Write out the contents and description of a command - this is useful for debugging

Parameters:
c - The command to print out

printKA_CONNECT_OK

public static void printKA_CONNECT_OK(byte[] data)
Write out the contents and description of a KA_CONNECT_OK command

Parameters:
data - The body of the command

printKA_SENSE

public static void printKA_SENSE(byte[] data)
Write out the contents and description of a KA_SENSE command

Parameters:
data - The body of the command

printObject

public static int printObject(int index,
                              byte[] data)
Write out the contents of a RescueObject update with the relevant bytes

Parameters:
index - The index into the buffer to read this RescueObject from
data - The buffer containing update data
Returns:
The index into the buffer immediately after the last byte used by this RescueObject

printProperty

public static int printProperty(int index,
                                byte[] data)
Write out the contents of a property update with the relevant bytes

Parameters:
index - The index into the buffer to read this property from
data - The buffer containing update data
Returns:
The index into the buffer immediately after the last byte used by this property

printBytes

public static void printBytes(int startIndex,
                              byte[] data,
                              java.lang.String description)
Print four bytes to System.out in a nice way

Parameters:
startIndex - The index of the first byte
data - The buffer containing the bytes to print out
description - A description of what these four bytes actually mean

hex

public static java.lang.String hex(byte b)
Turn a byte into a hexadecimal String

Parameters:
b - The byte to convert
Returns:
The byte in hexadecimal, as a String

decodeByte

public static byte decodeByte(byte[] buffer,
                              int off)
Decode a byte from a buffer

Parameters:
buffer - The buffer we are looking at
off - The offset into the buffer to start decoding from
Returns:
The next byte in the buffer

decodeBytes

public static byte[] decodeBytes(byte[] buffer,
                                 int off,
                                 int length)
Decode a byte array from a buffer

Parameters:
buffer - The buffer we are looking at
off - The offset into the buffer to start decoding from
length - The number of bytes to read
Returns:
The next byte array in the buffer

decodeShort

public static short decodeShort(byte[] buffer,
                                int off)
Decode a short from a buffer

Parameters:
buffer - The buffer we are looking at
off - The offset into the buffer to start decoding from
Returns:
The next short in the buffer

decodeInt

public static int decodeInt(byte[] buffer,
                            int off)
Decode an int from a buffer

Parameters:
buffer - The buffer we are looking at
off - The offset into the buffer to start decoding from
Returns:
The next int in the buffer

decodeString

public static java.lang.String decodeString(byte[] buffer,
                                            int off,
                                            int length)
Decode a String from a buffer

Parameters:
buffer - The buffer we are looking at
off - The offset into the buffer to start decoding from
length - The maximum number of characters in the String
Returns:
The next String in the buffer

encodeByte

public static byte[] encodeByte(int value)
Encode a byte into a byte array

Parameters:
value - The byte to encode
Returns:
A byte array representation of the input value

encodeByte

public static void encodeByte(int value,
                              byte[] buf,
                              int off)
Encode a byte into a buffer

Parameters:
value - The byte to encode
buf - The buffer to write the result into
off - The offset to start writing at

encodeBytes

public static void encodeBytes(byte[] bytes,
                               byte[] buf,
                               int off)
Encode a byte arrray into a buffer

Parameters:
bytes - The byte array to encode
buf - The buffer to write the result into
off - The offset to start writing at

encodeBytes

public static void encodeBytes(byte[] bytes,
                               int bytesOffset,
                               int bytesLength,
                               byte[] buf,
                               int off)
Encode part of a byte array into a buffer

Parameters:
bytes - The byte arrray to encode
bytesOffset - The offset into bytes to start writing from
bytesLength - The number of bytes to write
buf - The buffer to write the result into
off - The offset to start writing at

encodeShort

public static byte[] encodeShort(int value)
Encode a short into a byte array

Parameters:
value - The short to encode
Returns:
A byte array representation of the input value

encodeShort

public static void encodeShort(int value,
                               byte[] buf,
                               int off)
Encode a short into a buffer

Parameters:
value - The short to encode
buf - The buffer to write the result into
off - The offset to start writing at

encodeInt

public static byte[] encodeInt(int value)
Encode an int into a byte array

Parameters:
value - The int to encode
Returns:
A byte array representation of the input value

encodeInt

public static void encodeInt(int value,
                             byte[] buf,
                             int off)
Encode an int into a buffer

Parameters:
value - The int to encode
buf - The buffer to write the result into
off - The offset to start writing at

encodeString

public static byte[] encodeString(java.lang.String value,
                                  int length)
Encode a String into a byte array

Parameters:
value - The String to encode
length - The maximum number of bytes to use
Returns:
A byte array representation of the input byte

encodeString

public static void encodeString(java.lang.String value,
                                int length,
                                byte[] buf,
                                int off)
Encode a String into a buffer

Parameters:
value - The String to encode
length - The maximum number of bytes to write
buf - The buffer to write the result into
off - The offset to start writing at

isDifferent

public static boolean isDifferent(int[] a,
                                  int[] b)
Find out if two int arrays are different. The two arrays are not different if and only if they are the same size and contain the same elements (possibly out of order).

Parameters:
a - The first array
b - The second array
Returns:
true if and only if the two arrays are different

getTypeName

public static java.lang.String getTypeName(int type)
Translate a type name into a human-readable string

Parameters:
type - The type we want to convert
Returns:
A human-readable String showing the type given
See Also:
RescueConstants.TYPE_CIVILIAN, RescueConstants.TYPE_FIRE_BRIGADE, RescueConstants.TYPE_AMBULANCE_TEAM, RescueConstants.TYPE_POLICE_FORCE, RescueConstants.TYPE_ROAD, RescueConstants.TYPE_NODE, RescueConstants.TYPE_RIVER, RescueConstants.TYPE_RIVER_NODE, RescueConstants.TYPE_BUILDING, RescueConstants.TYPE_REFUGE, RescueConstants.TYPE_FIRE_STATION, RescueConstants.TYPE_AMBULANCE_CENTER, RescueConstants.TYPE_POLICE_OFFICE, RescueConstants.TYPE_WORLD, RescueConstants.TYPE_CAR

getPropertyName

public static java.lang.String getPropertyName(int type)
Translate a property name into a human-readable string

Parameters:
type - The property we want to convert
Returns:
A human-readable String showing the property given
See Also:
RescueConstants.PROPERTY_NULL, RescueConstants.PROPERTY_START_TIME, RescueConstants.PROPERTY_LONGITUDE, RescueConstants.PROPERTY_LATITUDE, RescueConstants.PROPERTY_WIND_FORCE, RescueConstants.PROPERTY_WIND_DIRECTION, RescueConstants.PROPERTY_X, RescueConstants.PROPERTY_Y, RescueConstants.PROPERTY_DIRECTION, RescueConstants.PROPERTY_POSITION, RescueConstants.PROPERTY_POSITION_HISTORY, RescueConstants.PROPERTY_POSITION_EXTRA, RescueConstants.PROPERTY_STAMINA, RescueConstants.PROPERTY_HP, RescueConstants.PROPERTY_DAMAGE, RescueConstants.PROPERTY_BURIEDNESS, RescueConstants.PROPERTY_FLOORS, RescueConstants.PROPERTY_BUILDING_ATTRIBUTES, RescueConstants.PROPERTY_IGNITION, RescueConstants.PROPERTY_BROKENNESS, RescueConstants.PROPERTY_FIERYNESS, RescueConstants.PROPERTY_ENTRANCES, RescueConstants.PROPERTY_BUILDING_SHAPE_ID, RescueConstants.PROPERTY_BUILDING_CODE, RescueConstants.PROPERTY_BUILDING_AREA_GROUND, RescueConstants.PROPERTY_BUILDING_AREA_TOTAL, RescueConstants.PROPERTY_BUILDING_APEXES, RescueConstants.PROPERTY_WATER_QUANTITY, RescueConstants.PROPERTY_STRETCHED_LENGTH, RescueConstants.PROPERTY_HEAD, RescueConstants.PROPERTY_TAIL, RescueConstants.PROPERTY_LENGTH, RescueConstants.PROPERTY_ROAD_KIND, RescueConstants.PROPERTY_CARS_PASS_TO_HEAD, RescueConstants.PROPERTY_CARS_PASS_TO_TAIL, RescueConstants.PROPERTY_HUMANS_PASS_TO_HEAD, RescueConstants.PROPERTY_HUMANS_PASS_TO_TAIL, RescueConstants.PROPERTY_WIDTH, RescueConstants.PROPERTY_BLOCK, RescueConstants.PROPERTY_REPAIR_COST, RescueConstants.PROPERTY_MEDIAN_STRIP, RescueConstants.PROPERTY_LINES_TO_HEAD, RescueConstants.PROPERTY_LINES_TO_TAIL, RescueConstants.PROPERTY_WIDTH_FOR_WALKERS, RescueConstants.PROPERTY_EDGES, RescueConstants.PROPERTY_SIGNAL, RescueConstants.PROPERTY_SIGNAL_TIMING, RescueConstants.PROPERTY_SHORTCUT_TO_TURN, RescueConstants.PROPERTY_POCKET_TO_TURN_ACROSS

getCommandTypeName

public static java.lang.String getCommandTypeName(int header)
Translate a command type into a human-readable string

Parameters:
header - The type we want to convert
Returns:
A human-readable String showing the type given
See Also:
RescueConstants.HEADER_NULL, RescueConstants.AK_CONNECT, RescueConstants.AK_ACKNOWLEDGE, RescueConstants.AK_REST, RescueConstants.AK_MOVE, RescueConstants.AK_EXTINGUISH, RescueConstants.AK_RESCUE, RescueConstants.AK_CLEAR, RescueConstants.AK_LOAD, RescueConstants.AK_UNLOAD, RescueConstants.KA_CONNECT_OK, RescueConstants.KA_CONNECT_ERROR, RescueConstants.KA_SENSE, RescueConstants.KA_HEAR, RescueConstants.SK_CONNECT, RescueConstants.SK_ACKNOWLEDGE, RescueConstants.SK_UPDATE, RescueConstants.KS_CONNECT_OK, RescueConstants.KS_CONNECT_ERROR, RescueConstants.KS_COMMANDS, RescueConstants.KG_CONNECT, RescueConstants.KG_ACKNOWLEDGE, RescueConstants.KG_UPDATE, RescueConstants.GK_CONNECT_OK, RescueConstants.GK_CONNECT_ERROR

getAgentTypeName

public static java.lang.String getAgentTypeName(int type)
Turn an agent type into a human-readable String

Parameters:
type - The agent type to convert
Returns:
A human-readable version of the type given

arrayAsString

public static java.lang.String arrayAsString(int[] array)
Turn an array of integers into a String containing a comma-seperated list of numbers

Parameters:
array - The array to convert
Returns:
A human-readable String containing a comma-seperated list of numbers