The Gamer Corner
I am all about mathematic dick measuring that no one cares about
Talraen
Lost your password?

Elegant solution to cascading try-catch block issue

Elegant solution to cascading try-catch block issue – January 11, 2011 3:52 PM (edited 1/11/11 10:52 AM)
chaoscat (452 posts) Ambassador of Good Will
Rating: Not Rated
So based on our discussion yesterday, I cleaned up my ugly try-catch code to the following, which I think is pretty neat. I'm curious if you guys have any thoughts on other/better ways to do this. Also, in case it wasn't clear, I am becoming a huge fan of java enums.


public class ParseDate {

private enum Parsers {
RFC3339_LONG( ISODateTimeFormat.dateTime() ),
RFC3339_SHORT( ISODateTimeFormat.dateTimeNoMillis() ),
APACHE_COMBINED( DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z") );

private final DateTimeFormatter parser;

Parsers( DateTimeFormatter fmt) {
this.parser = fmt;
}

public DateTime parseDateTime(String input) {
return this.parser.parseDateTime(input);
}
}


public DateTime parse(String input) {
return parse(input, DateTimeZone.UTC);
}

public DateTime parse(String input, DateTimeZone zone) {

DateTime res = null;

for (Parsers parser : Parsers.values()) {
try {
res = parser.parseDateTime(input);
return res.withZone(zone);
}
catch (IllegalArgumentException e) {
// indicates failed parse. Ignore for now, we'll throw our own if nothing matches
}
}

throw new IllegalArgumentException("failed to parse date string: " + input);
}
}

_________________________________________________
Syllabic (4:14 PM): tozzi are you like dowd's jiminy cricket
Re: Elegant solution to cascading try-catch block issue – January 11, 2011 4:47 PM (edited 1/11/11 11:48 AM)
Cuzzdog (1522 posts) Head of Gamer Corner R&D
Rating: Not Rated
That looks pretty solid. The only suggestion I would have would be to create a private static Parsers variable that would save the last successful parsed format. Then you would try to use that saved format first and the only loop if it doesn't work. That means it would be more efficient if you expect the date format to be the same for the most part in a single, large block of input, but less efficient if you expect the date format to change frequently since you'd be testing one format twice.


public class ParseDate {

private enum Parsers {
RFC3339_LONG( ISODateTimeFormat.dateTime() ),
RFC3339_SHORT( ISODateTimeFormat.dateTimeNoMillis() ),
APACHE_COMBINED( DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z") );

private final DateTimeFormatter parser;

Parsers( DateTimeFormatter fmt) {
this.parser = fmt;
}

public DateTime parseDateTime(String input) {
return this.parser.parseDateTime(input);
}
}

private static Parsers lastParsed;

public DateTime parse(String input) {
return parse(input, DateTimeZone.UTC);
}

public DateTime parse(String input, DateTimeZone zone) {

DateTime res = null;

try {
res = parser.parseDateTime(input);
return res.withZone(zone);
}
catch (Exception e) {

for (Parsers parser : Parsers.values()) {
try {
res = parser.parseDateTime(input);
lastParsed = parser;
return res.withZone(zone);
}
catch (IllegalArgumentException e) {
// indicates failed parse. Ignore for now, we'll throw our own if nothing matches
}
}
}

throw new IllegalArgumentException("failed to parse date string: " + input);
}
}

Re: Elegant solution to cascading try-catch block issue – January 11, 2011 4:53 PM (edited 1/11/11 11:53 AM)
chaoscat (452 posts) Ambassador of Good Will
Rating: Not Rated
Yeah, I've got that in the back of my head to optimize if I need to. I tend to follow the "make it work, make it elegant, make it fast" process of development, and right now I'm still (in the project sense, not on this specific piece) on the "make it work" step. I don't want to start optimizing anything until I can see where it's slow points are, but if I end up needing to tune this routine, caching the last successful parser is the first thing I'll try.

_________________________________________________
Syllabic (4:14 PM): tozzi are you like dowd's jiminy cricket
Active Users: (guests only)
1 user viewing | Refresh