Over some time in different jobs I came across a need for several utilities that I couldn't find available at the time. And I saw that I needed them several times over and over again. So I wrote my own small library that I found very useful. So I just published it as an open source java library. Here is the link
Also this library is available on Maven Central. Here are the Maven artifacts (the version 1.03 is the latest at the time of writing of this article but might change to check for the latest version search for artifact "MgntUtils" at http://search.maven.org/):
Below is just a short explanation what is there. The library comes with a nicely written (I hope) javadoc with detailed description. So here are the list of features:
Stacktrace noise filter In my experience this feature was the most generic and useful for me. Stacktrace is a life saver when debugging or trying to figure out what went wrong in your application. However, When working with logs on the server side you can come across huge stacktrace that contains the longest useless tail of various frameworks and Application Server related packages. And somewhere in this pile there are several lines of a relevant trace and they may be in different segments separated by useless information. It becomes a nightmare to search for a relevant stuff. Here is a link that describes the same problem with real life examples (not for the fainthearted ) https://dzone.com/articles/filtering-stack-trace-hell. So In my library There is class is called TextUtils and it has method getStacktrace() with several overridden signatures. It takes a Throwable instance and allows to set a package prefix of the packages that are relevant. Let's say your company's code always resides in packages that start with "com.plain.*" So you set such a prefix and do this
this will filter out very smartly all the useless parts of the trace leaving you with very concise stacktrace. Also I found it very convinient to pre-set the prefix and then just use the convinience method
It will do the same. To preset the prefix just use method
Also if you use Spring environment you can add the following segment to your Spring configuration and then you all set:
Javadoc explains everything in detail. But here is a little teaser: you will get a following stacktrace:
Silent String parsing into Integer, Long etc The same class TextUtils provides methods to parse String silently (without exception ever being thrown) into types of Double, Long, Integer, Short and Byte. The methods are called parseStringToLong()parseStringToInteger() etc. They all take 4 arguments:
A String to be parsed
An implementation of Number (Double, Long, Integer, Short or Byte) to serve as default value if any parsing occurred
A String to be printed as an error message into log if the first argument is null (may be null, and then no error is printed)
A String to be printed as an error message if NumberFormatException occurred (may be null, and then no error is printed)
Compare Versions This utility allows to convert String to version and vise-versa and to compare versions and version ranges properly. Often if you need to compare versions you just compare Strings. So lets say version "1.5.3" will be greater then version "1.3.1". However if you will compare Strings "1.4.2" and "1.12.3" the String "1.4.2" will erroneously be greater. So this Utility takes care of such problem and in addition introduces VersionRange class and allows operations on version ranges. (See methods compareVersions in class TextUtils and class VersionRange)
String Unicode converter Class StringUnicodeEncoderDecoder has methods that can convert a String (in any language) into a sequence of Unicode characters and vise-versa. For example a String "Hello World" will be converted into
Web Utils Class WebUtils provides a method to read a binary information from http request. This method may be useful in particular if a client is uploading a large size of binary information and the server side reading is faster then client writing. This Utility allows server to throttle down and adjust server to client speed. Also may be convenient to read a client input as byte array in one go.
File Utils This utility allows to read file as a String with or without specifying character set or just read it as byte array. This utility uses nio package and Buffers for faster performance. Max file size for this Utility is 2G.
Time Util This is just a single convenience method that provides the same functionality as Thread.sleep() but it is silient. I.e. it "swallows" IterruptedException. Also it takes 2 arguments: time to sleep and measurement unit. So instead of
You can just write:
I thought it was cute
So this is it. Feel free to download this library. It comes with MIT licence - one of the most permissive licences I know of. Source code, Javadoc and binary version is available at the link above. The code was compiled by jdk 8 but has no features of version 8 in it. It should work with java 7 most definitely, I suspect it will work with java 6 and even 5, but haven't tested it with those versions. Feel free to use it as you see fit, tell others about it if you like it and feel free to drop me a note at email@example.com if you have any feedback.