public int getIntFromHeader(Request req, String headerName) {
if(req != null && req.getHeaders() != null) {
String header = req.getHeaders().get(headerName);
if(header != null) {
try {
return Integer.parseInt(header);
} catch(Exception e) {
return -1;
}
}
}
return -1;
}
as public int getIntFromHeader(Request req, String headerName) {
return Integer.parseInt(req.getHeaders().get(headerName));
}
That way it either returns with a valid int or throws an exception of some sort (null or otherwise).Also, -1 is a perfectly valid integer and so this function can't distinguish between a header with -1 in it and an error. The fact that there's a req != null check seems silly. Surely, the caller should be responsible for that and if they aren't they should get an exception.