The 90th Percentile
My top list of java tools
Lack of imagination is one of our worst sins as software developers. We do the same things over and over again, but we rarely modify our ways - me at least. After some years, these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.Chances are you are already using at least some of these, but here we go anyways:StringUtilsThe bread and butter of the commons-lang library, this utility class includes some methods that should seriously have been included in String long time ago.StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // trueStringUtils.isBlank(" \n\t"); // trueStringUtils.substringAfterLast("foo.bar.baz", "."); // "baz"StringUtils.substringBeforeLast("foo.bar.baz", "."); // "foo.bar"StringUtils.split("foo.bar.baz", '.'); // { "foo", "bar", "baz" }StringUtils.split("foo, bar,baz", ", "); // { "foo", "bar", "baz" }StringUtils.leftPad("1", 3, '0'); // "001"IOUtils and FileUtilsA must-have for the rare occasions where you need to manipulate files by hand. Both are pretty much alike (FileUtils for File, IOUtils for InputStream and Reader classes) and come bundled in commons-io.File file1;File file2;InputStream inputStream;OutputStream outputStream;// copy one file into anotherFileUtils.copyFile(file1, file2);IOUtils.copy(inputStream, outputStream);// read a file into a StringString s1 = FileUtils.readFileToString(file1);String s2 = IOUtils.toString(inputStream);// read a file into a list of Strings, one item per lineList<String> l1 = FileUtils.readLines(file1);List<String> l2 = IOUtils.readLines(inputStream);// put this in your finally() clause after manipulating streamsIOUtils.closeQuietly(inputStream);// return the list of xml and text files in the specified folder and any subfoldersCollection<File> c1 = FileUtils.listFiles(file1, { "xml", "txt" }, true);// copy one folder and its contents into anotherFileUtils.copyDirectoryToDirectory(file1, file2);// delete one folder and its contentsFileUtils.deleteDirectory(file1);Google collectionsThis is the best implementation of a collections extension that I know of. Some of these are shouting to be included in the JDK:// create an ArrayList with three argumentsList<String> list = Lists.newArrayList("foo", "bar", "baz");// notice that there is no generics or class cast,// and still this line does not generate a warning.Set<String> s = Sets.newConcurrentHashSet();// intersect and union are basic features of a Set, if you ask meSet<String> s = Sets.intersect(s1, s2);// Example of multiple values in a MapListMultimap<String, Validator> validators = new ArrayListMultimap<String, Validator>();validators.put("save", new RequiredValidator());validators.put("save", new StringValidator());validators.put("delete", new NumberValidator());validators.get("save"); // { RequiredValidator, StringValidator }validators.get("foo"); // empty List (not null)validators.values(); // { RequiredValidator, StringValidator, NumberValidator }java.util.concurrentNot everybody needs the heavy lifting of java.util.concurrent, but the concurrent collections are handy:// a map that may be modified (by the same or different thread) while being iteratedMap<String, Something> repository = new ConcurrentHashMap<String, Something>();// same with lists. This one is only available with Java 6List<Something> list = new CopyOnWriteArrayList<Something>();Hardly a large toolbox, is it? If your favourite library is missing, feel free to add it :)I WILL BE AT DEVOXX!! I have a 15 minute talk about the browser cache on December 10th. I am really excited about this, this will be my first JavaPolis (now Devoxx) experience.It will be fun, I plan to talk about 304, Expires / Cache-control, and maybe have a small demo of MD5-based cache if time permits. If you happen to be there, please drop by and say hi!
The blog that shares the passion about the how and why of our projects.