Java Enum handling
I have a Java question about the best method to work with the following enum file that has been provided to me.
public enum Foo{
PLANE(1, "Plane", "plane"),
CASTLE(2, "Castle", "castle"),
FEILD(3, "Feild", "field"),
ROAD(4, "Road", new String[] {"road", "pavement"});
}
There are roughly 100ish entries of the above formats
/**
* Stores a map of the names for fast access. Stores a map of the IDs for fast access.
*/
private static final Map<Integer,BlockType> ids = new HashMap<Integer,BlockType>();
private 开发者_如何学JAVAstatic final Map<String,BlockType> lookup = new HashMap<String,BlockType>();
private final int id;
private final String name;
private final String[] lookupKeys;
static {
for(Foo type : EnumSet.allOf(Foo.class)) {
ids.put(type.id, type);
for (String key : type.lookupKeys) {
lookup.put(key, type);
}
}
}
/**
* Construct the type.
*
* @param id
* @param name
*/
Foo(int id, String name, String lookupKey) {
this.id = id;
this.name = name;
this.lookupKeys = new String[]{lookupKey};
}
/**
* Construct the type.
*
* @param id
* @param name
*/
Foo(int id, String name, String[] lookupKeys) {
this.id = id;
this.name = name;
this.lookupKeys = lookupKeys;
}
/**
* Return type from ID. May return null.
*
* @param id
* @return
*/
public static Foo fromID(int id) {
return ids.get(id);
}
/**
* Return type from name. May return null.
*
* @param name
* @return
*/
public static Foo lookup(String name) {
return lookup.get(name.toLowerCase());
}
/**
* Get block numeric ID.
*
* @return
*/
public int getID() {
return id;
}
/**
* Get user-friendly name.
*
* @return
*/
public String getName() {
return name;
}
So what I am trying to do with this enum is the following: A user issues a command, for example: /bounce 1
The program recognizes bounce as a command and looks for the following argument. Since 1 follows the command it now checks that 1 is a valid id# in the enum. If it is it performs the command.
A user issues a command for example 2 in the following form: /bounce plane. This time it checks to see if the string is a valid string name in the enum and if it is grab the id# associated with it to perform the command.
How can I check for these criteria that either the id or one of the string id's exist and always return the id# for later usage. Any help would be appreciated.
It should also be noted that this enum file is it's own independent class file being called from the main.
Thanks to all who helped me get on the right track I have awarded Wolfcastle the correct answer, here was what I got to work for me but I am still open to any critiques on logic or performance flaws with my method.
String s = null;
try {
s = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
id = Integer.parseInt(s);
} catch (NumberFormatException nfe) {
}
if (BlockType.lookup(s) != null
|| BlockType.fromID(id) != null) {
if (BlockType.lookup(s)!= null) {
blockname = BlockType.lookup(s).getName();
blockid = BlockType.lookup(s).getID();
} else {
blockname = BlockType.fromID(id).getName();
blockid = BlockType.fromID(id).getID();
}
}
So if I understand you correctly, the code you have posted is not your code, but rather code you must use, and you are wondering how you can use it.
String param = getParam(); // get the '1' or 'plane' string from your input
BlockType type = null;
try {
// Check for an integer
int id = Integer.parseInt(param);
type = BlockType.getFromId(id);
} catch( NumberFormatException e ) {
// Okay, not an integer, check the lookup value
type = BlockType.lookup(param);
}
if( type != null ) {
int blockid = type.getID();
String blockname = type.getName();
// continue on
} else {
// else the input parameter was invalid and we can't get a BlockType for it
// return some kind of error
}
In this case you could just check if the returned value is null. Or what exactly do you want to check?
Also note that you don't need the EnumSet:
static {
for(Foo type : values()) {
ids.put(type.id, type);
for (String key : type.lookupKeys) {
lookup.put(key, type);
}
}
}
精彩评论