A char array in java is nothing more than a String
in java. You can use string in switch-cases in Java7, but I don't know the efficiency of the comparisions.
Because only the last element of you char array seems to have a meaning, you could do a switch case with it. Something like
private static final int VERSION_INDEX = 3;
...
char[] version = // get it somehow
switch (version[VERSION_INDEX]) {
case '5':
break;
// etc
}
...
EDIT
More object oriented version.
public interface Command {
void execute();
}
public class Version {
private final Integer versionRepresentation;
private Version (Integer versionRep) {
this.versionRepresentation = versionRep;
}
public static Version get(char[] version) {
return new Version(Integer.valueOf(new String(version, "US-ASCII")));
}
@Override
public int hashCode() {
return this.versionRepresentation.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Version) {
Version that = (Version) obj;
return that.versionRepresentation.equals(this.versionRepresentation);
}
return false;
}
}
public class VersionOrientedCommandSet {
private final Command EMPTY_COMMAND = new Command() { public void execute() {}};
private final Map<Version, Command> versionOrientedCommands;
private class VersionOrientedCommandSet() {
this.versionOrientedCommands = new HashMap<Version, Command>();
}
public void add(Version version, Command command) {
this.versionOrientedCommands.put(version, command);
}
public void execute(Version version) throw CommandNotFoundException {
Command command = this.versionOrientedCommands.get(version);
if (command != null) {
command.execute();
} else {
throw new CommandNotFoundException("No command registered for version " + version);
}
}
}
// register you commands to VersionOrientedCommandSet
char[] versionChar = // got it from somewhere
Version version = Version.get(versionChar);
versionOrientedCommandSet.execute(version);
lots of code hehe You will have a little cost of warm-up, but if you program is executed multiple times, you will gain efficiency with the map :P
short convertBytesToBCD(byte[])
– Senna0x30 << 24 + 0x30 << 16 + 0x30 << 8 + 0x35
– Rodmannstatic final int V0005 = 808464437
. Instead I'd have an Enum likepublic enum FileVersion { V0005(808464437) }
? – Rodmannpublic static final int
afterall. Thanks everyone. – Rodmann