Validating parameters according to a fixed reference
- by James P.
The following method is for setting the transfer type of an FTP connection. Basically, I'd like to validate the character input (see comments).
Is this going overboard? Is there a more elegant approach? How do you approach parameter validation in general? Any comments are welcome.
public void setTransferType(Character typeCharacter,
Character optionalSecondCharacter) throws NumberFormatException,
IOException {
// http://www.nsftools.com/tips/RawFTP.htm#TYPE
// Syntax: TYPE type-character [second-type-character]
//
// Sets the type of file to be transferred. type-character can be any
// of:
//
// * A - ASCII text
// * E - EBCDIC text
// * I - image (binary data)
// * L - local format
//
// For A and E, the second-type-character specifies how the text should
// be interpreted. It can be:
//
// * N - Non-print (not destined for printing). This is the default if
// second-type-character is omitted.
// * T - Telnet format control (<CR>, <FF>, etc.)
// * C - ASA Carriage Control
//
// For L, the second-type-character specifies the number of bits per
// byte on the local system, and may not be omitted.
final Set<Character> acceptedTypeCharacters = new HashSet<Character>(Arrays.asList(
new Character[] {'A','E','I','L'}
));
final Set<Character> acceptedOptionalSecondCharacters = new HashSet<Character>(Arrays.asList(
new Character[] {'N','T','C'}
));
if( acceptedTypeCharacters.contains(typeCharacter) ) {
if( new Character('A').equals( typeCharacter ) || new Character('E').equals( typeCharacter ) ){
if( acceptedOptionalSecondCharacters.contains(optionalSecondCharacter) ) {
executeCommand("TYPE " + typeCharacter + " " + optionalSecondCharacter );
}
} else {
executeCommand("TYPE " + typeCharacter );
}
}
}