Helpers to the rescue

Whenever a scripting language wants to make a good appearance, they compare themselves with Java with deliberately verbose code snippets. Reading a file, some regular expression tricks, or even a Hello World application.

Java indeed is not a new language and not really suitable for some scripting tasks by default. However, sometimes for such tasks (or for any task actually) you don't have time to learn a script language, it may be handy to use helper libraries. Actually that is the spirit of Java, that if something can be reused, make it a small library instead of doing everything with core language or libraries over and over.

There are several good helper libraries available. Like apache commons, or Google collections. but commons suffer from age and fragmentation. Google collections is modern but may not be suitable for small applications. So i decided to mix and match (In other words I stole code form those libs), and write a little bit by myself for some of our applications. My aim is to keep the library size always less than 100kb. it may have a lot of flaws, bu so far it worked for me. i call the project jmate, project code is here..

Nothing really fancy, but as a result you can write such code now:

Read a UTF-8 file as a String
String content = new SimpleFileReader("foo.txt", "utf-8").asString();
Read a file as a String List
List<String> lst = new SimpleFileReader("foo.txt").asStringList();

Read a utf-8 file as a string list, but skip the non-text lines. trim the lines. Regular expression filters also can be built.
List<String> lst = new SimpleFileReader.Builder("foo.txt")
.encoding(
"utf-8")
.filters(StringFilters.PASS_ONLY_TEXT
)
.trim()
.build()
.asStringList();

Iterate through a text file's lines.
LineIterator li = new SimpleFileReader("foo.txt").getLineIterator();
while (li.hasNext())
out.println(li.next());
IOs.
closeSilently(li);

Get a text file's lines in an enhanced for loop
for (String s : new SimpleFileReader("foo.txt").getIterableReader())
out.println(s);

Write a string as a file
new SimpleFileWriter(tmpFile).writeString("Hello World!");

write a String list as a utf-8 file, but keep the writer open after the writeLines() call. By default SimpleFileWriter closes the stream after any of its method calls.
new SimpleFileWriter
.Builder(
"foo.txt")
.encoding(
"utf-8")
.keepOpen(
true)
.build()
.writeLines(Collects.
newArrayList("Hello", "World"));

Save a web page as a file
new SimpleFileWriter("foo.txt").copyFromURL("http://google.com");

Get all jsp, tag files in a directory and sub directories
List<File> files = Files.crawlDirectory(
new File("pages"),
new Files.ExtensionFilter("jsp", "tag"));
There are more to write but it is the basic idea. neither API nor the code is stabilized. but if you are interested you can check the code. Beware.. There is shameless copying form Google collections and Apache Commons libraries.

3 comments:

Anonymous said...

i think we all have projects like that, in mine i use Copy&Paste Design Pattern(the core java pattern, made femous by IO api and useless decorators design pattern), so every method is ready for being copy and I tried to have no dependencies ;)

The sad thing is that someone should put all the usefull things together in really great Helper project, commons-lang has some great stuff. I hope you can make you can make it happen. However in most enterprise app they dont let you just drop jars that u want so you must use Copy&Paste Design Pattern.

plus, use Arrays.asList("hej","ho")

Anonymous said...

The Spiffy Framework (http://spiffyframework.sourceforge.net/) is about taking the best of various areas and put them forward as a jar/jars or allow people to get 'inspired' and use 'copy/paste inheritance'. Feel free to contribute your helpers you use often...

Marcio Aguiar said...

Kudos to you my friend!