I am devicing an abstract class that will override toString() with an automated message, as such:
public abstract class AbstractTask {
abstract void run();
abstract int calculate();
private String makeMessage() {
String raw = this.getClass().toString();
// do regex stuff here!
}
@Override
public String toString() {
return makeMessage();
}
My implemented (extends AbstractTask) classes might be called something like
TaskXRQMinimize
TaskListParallell
TaskVerifyRepositoryEmpty
TaskCreateXRQs
TaskCreateZRQs
TaskSendAllSRQToTLS
The output I want to have when calling the classes toString() methods are:
Task XRQ Minimize
Task List Parallell
Task Verify Repository Empty
Task Create XRQs
Task Create ZRQs
Task Send All SRQ To TLS
I made a simple regex pattern to find these words:
([A-Z]{1}[a-z]+|[A-Z]{3}[a-z]?)
How do I implement this?
Make your
toString()abstract as well, force subclasses to implement it by returning fixed string. Throw away parsing class name and using regular expressions – too fragile and with poor performance. If you really want to make it this way, at least do the computations once per class, not every timetoString()is called.As for the regular expression, I have something better: StringUtils#splitByCharacterTypeCamelCase() from Apache Commons Lang (there is really no need to reinvent some things):
In your case: