I have a class that can be created by passing a list of strings or by passing a file. The file will be parsed, creating a list of strings. So it's actually a helper constructor.
Parsing the file may take a long time however and the resulting list of strings may not be used ever (even though the object is created anyways). Therefore I got the idea of "lazy parsing" the file so that the file is actually only parsed right before some methods of this objects are used.
Is there any way to do this in groovy or even a better way to achieve what I want?
Don't parse the content in the constructor, just store the
String
or theFile
constructor parameters in attributes in your class. Then create a new method with the slow parse using the previous attributes. Finally, you can cache the result annotating this method with the @Memoized Groovy annotation.