I am a fan of the try / do pattern (or trier / doer), which is best implemented in C # using out parameters, for example:
DateTime date; if (DateTime.TryParse("2012-06-18", out date)) {
I am currently working on a Java 1.5 project for which I am implementing a try / do pattern using the new TryResult class, which is returned from any methods that implement the try / do pattern:
public class TryResult<ResultType> { private boolean mSuccess = false; private ResultType mResult = null; public TryResult(boolean success, ResultType result) { super(); this.mSuccess = success; this.mResult = result; } public boolean isSuccess() { return mSuccess; } public ResultType getResult() { return mResult; } }
This works well, but I will port this code to another platform that uses J2ME, and therefore generics are not available.
My current options are to either remove the generics from the TryResult class above, and use the plain old Object and casting, or create a new class for the types that I ended up using (e.g. StringTryResult ).
Is there a better way to implement this template on J2ME / Java 1.3?
Ian newson
source share