Created utility classes that unify our direction constant handling so that

we can interchange direction constants without requiring conversion and so
that we only have to define this stuff once.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@817 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-12-17 03:32:32 +00:00
parent 65401007de
commit f51a2e0d0c
2 changed files with 97 additions and 0 deletions
@@ -0,0 +1,41 @@
//
// $Id: DirectionCodes.java,v 1.1 2001/12/17 03:32:32 mdb Exp $
package com.threerings.util;
/**
* A single, top-level location for the definition of compass direction
* constants, which are used by a variety of Narya services.
*/
public interface DirectionCodes
{
/** A direction code indicating no direction. */
public static final int NONE = -1;
/** A direction code indicating southwest. */
public static final int SOUTHWEST = 0;
/** A direction code indicating west. */
public static final int WEST = 1;
/** A direction code indicating northwest. */
public static final int NORTHWEST = 2;
/** A direction code indicating north. */
public static final int NORTH = 3;
/** A direction code indicating northeast. */
public static final int NORTHEAST = 4;
/** A direction code indicating east. */
public static final int EAST = 5;
/** A direction code indicating southeast. */
public static final int SOUTHEAST = 6;
/** A direction code indicating south. */
public static final int SOUTH = 7;
/** The total number of directions. */
public static final int DIRECTION_COUNT = 8;
}
@@ -0,0 +1,56 @@
//
// $Id: DirectionUtil.java,v 1.1 2001/12/17 03:32:32 mdb Exp $
package com.threerings.util;
/**
* Direction related utility functions.
*/
public class DirectionUtil implements DirectionCodes
{
/**
* Returns a string representation of the supplied direction code.
*/
public static String toString (int direction)
{
return ((direction >= SOUTHWEST) && (direction <= SOUTH)) ?
DIR_STRINGS[direction] : "INVALID";
}
/**
* Returns an abbreviated string representation of the supplied
* direction code.
*/
public static String toShortString (int direction)
{
return ((direction >= SOUTHWEST) && (direction <= SOUTH)) ?
SHORT_DIR_STRINGS[direction] : "?";
}
/**
* Returns a string representation of an array of direction codes. The
* directions are represented by the abbreviated names.
*/
public static String toString (int[] directions)
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < directions.length; i++) {
if (i > 0) {
buf.append(", ");
}
buf.append(toShortString(directions[i]));
}
return buf.toString();
}
/** Direction constant string names. */
protected static final String[] DIR_STRINGS = {
"SOUTHWEST", "WEST", "NORTHWEST", "NORTH",
"NORTHEAST", "EAST", "SOUTHEAST", "SOUTH"
};
/** Abbreviated direction constant string names. */
protected static final String[] SHORT_DIR_STRINGS = {
"SW", "W", "NW", "N", "NE", "E", "SE", "S"
};
}