Question: Enum Direction java.lang.Object java.lang.Enum edu.ccave.common.Direction All Implemented Interfaces: java.io.Serializable, java.lang.Comparable public enum Direction extends java.lang.Enum All possible directions that a player can travel. We read

Enum Direction

java.lang.Object

java.lang.Enum

edu.ccave.common.Direction

All Implemented Interfaces:

java.io.Serializable, java.lang.Comparable

public enum Direction extends java.lang.Enum 

All possible directions that a player can travel. We read a properties file to determine the synonyms for each direction. This enum reads a properties file for each enum value in the resources directory, such as

n.properties

s.properties

etc.

The keys of these properties files don't really matter--the values do. So for Direction.N, in n.properties

n=north

n1=othernorthvalue

n2=anothernorthvalue

will all define synonym String values for Direction.N.

Enum Constant Summary

Enum Constants
Enum Constant and Description
DOWN
E
N
NE
NW
S
SE
SW
UNKNOWN
UP
W

Field Summary

Fields
Modifier and Type Field and Description
static java.lang.String RESOURCE_DIR

Method Summary

All Methods Static Methods Instance Methods Concrete Methods
Modifier and Type Method and Description
static Direction findSynonym(java.lang.String possibleAlt)

enum-wide method to provide a more friendly valueOf method.

boolean isSynonym(java.lang.String possibleAlt)

Determine whether a given String value represents this Direction value ignoring case, or one of its synonyms ignoring case.

static Direction valueOf(java.lang.String name)

Returns the enum constant of this type with the specified name.

static Direction[] values()

Returns an array containing the constants of this enum type, in the order they are declared.

Methods inherited from class java.lang.Enum

compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf

Methods inherited from class java.lang.Object

getClass, notify, notifyAll, wait, wait, wait

Enum Constant Detail

N

public static final Direction N 

S

public static final Direction S 

E

public static final Direction E 

W

public static final Direction W 

NE

public static final Direction NE 

NW

public static final Direction NW 

SE

public static final Direction SE 

SW

public static final Direction SW 

UP

public static final Direction UP 

DOWN

public static final Direction DOWN 

UNKNOWN

public static final Direction UNKNOWN 

Field Detail

RESOURCE_DIR

public static final java.lang.String RESOURCE_DIR 

See Also:

Constant Field Values

Method Detail

values

public static Direction[] values() 

Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:

for (Direction c : Direction.values()) System.out.println(c); 

Returns:

an array containing the constants of this enum type, in the order they are declared

valueOf

public static Direction valueOf(java.lang.String name) 

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:

name - the name of the enum constant to be returned.

Returns:

the enum constant with the specified name

Throws:

java.lang.IllegalArgumentException - if this enum type has no constant with the specified name

java.lang.NullPointerException - if the argument is null

findSynonym

public static Direction findSynonym(java.lang.String possibleAlt) 

enum-wide method to provide a more friendly valueOf method. We take a String representation of an enum value or synonym and, ignoring case, return the corresponding Direction enum value. This method steps through every possible enum value, which it can do using values(). It then tests whether possibleAlt matches one of those values. (See isSynonym)

Parameters:

possibleAlt - String representing a case-insensitive, synonym-based Direction value

Returns:

the Direction value associated with possibleAlt, or Direction.UNKNOWN

isSynonym

public boolean isSynonym(java.lang.String possibleAlt) 

Determine whether a given String value represents this Direction value ignoring case, or one of its synonyms ignoring case.

Parameters:

possibleAlt - a possible String representation of this Direction or one of its synonym values

Returns:

true if possibleAlt is a valid String value for this Direction or one of its synonyms, all ignoring case

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Enum Constants |

Field |

Method

Detail:

Enum Constants |

Field |

Method

: In the resources directory, create a properties file for each direction except UNKNOWN. For example, resources will contain a file n.properties This file will contain a series of synonyms for Direction.N, where the property key is meaningless and the property value is a synonym for N, for example, "north". So n.properties would have at least one line n=north it may also contain other synonyms. For example, for Direction.UP you wish to define up=u up1=higher At a minimum, define long forms for each direction: 
enum value long form synonym
n north
s south
e east
w west
nw northwest
ne northeast
sw southwest
se southeast
up u
down d

You may add other synonyms as you wish.

3: The Direction enum has an instance variable of type ArrayList that holds the enum's given synonyms from the corresponding properties file information.

4: The Direction enum's constructor reads the associated properties file from the resources directory and adds the synonyms to the ArrayList instance variable.

5: The attached zip file contains Javadoc pages describing the details of Direction in greater detail, and also describes a minimal tester. Unzip it and use a browser to open index.html in the directory structure.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!