For all the time spent writing Java code, I have developed a specific set of useful third-party libraries that are firmly entrenched in the classpath, and without which not a single development day can be done, whether writing something “on one knee” or working on a serious the project. This is not about “monsters” like Spring, Struts, Hibernate (that's another story), but rather about utilities that fill in the gaps in the Java SE API and save you a dozen or two extra lines of code / minutes here and there. I would like to share this information with the community - I hope it will be useful especially for those who are just starting to storm Java, and will allow a little, but increase productivity.
So, a list of the most frequently used classes and methods with comments: Apache Commons: Collections Very good library, in many respects supplementing Java SE Collections Framework. Very useful especially for those who are stuck in Java 2 SE 1.4.2 (sometimes it happens) because of “client requirements” and still dream of LinkedHashMap :)
CollectionUtils . Perhaps the most frequently used class with a bunch of "tasty":
filter / find (Collection, Predicate) - filtering and searching by predicate.
forAllDo (Collection, Closure) - Closes for each element (will they ever add Closure to Java?)
is (Not) Empty (Collection) - allows you not to check for null before calling (small change, but nice, compare with java.util.Collection.isEmpty ())
isEqualCollection (Collection, Collection) - never had to compare two collections manually?
A few more dozens of useful methods.
ClosureUtils - for those who have long dreamed of the appearance of full Closure in Java.
Many other classes of different levels of utility (hereinafter I list the most frequently used in my case).
closeQuietly (Connection, Statement, ResultSet) - do you work directly with JDBC? Tired of constantly writing nested if / try / catch / finally to properly close resources with a null check? Then to you here :) Typical code "from the textbook"
try {
// do some db work
}
catch (Exception e) {
e.printStackTrace ();
}
finally {
try {
if (rs! = null)
rs.close ();
}
catch (Exception e) {
e.printStackTrace ();
}
try {
if (st! = null)
st.close ();
}
catch (Exception e) {
e.printStackTrace ();
}
try {
if (conn! = null)
conn.close ();
}
catch (Exception e) {
e.printStackTrace ();
}
}
takes a more elegant form:
try {
// do some db work
}
catch (Exception e) {
e.printStackTrace ();
}
finally {
DbUtils.closeQuietly (conn, st, rs);
}
Apache Commons: IO Working with files is not the strongest side of the platform that Java critics like to use (sometimes, deservedly). For example, a request to implement java.io.File.copy () has beenhanging for 10 years. Commons IO partially rescues:
closeQuietly (Reader / Writer / InputStream / OutputStream) - as well as DbUtils, really helps to avoid spaghetti-like code, which is often obtained when piled if / try / catch / finally to just close the I / O resource.
copy (InputStream, OutputStream) is a classic.
Apache Commons: Lang Tools for working with strings, reflection, serialization, objects and system. Perhaps the most frequently used library from Apache Commons.
StringUtils - a huge number of methods for manipulating strings.
is (Not) Blank / Empty (String) - about if (s!=null && s.trim().length()>0) time to forget.
reflectionToString (Object) is a toString () implementation based on reflection. That is, the result of the method will automatically change when adding or removing object fields. For the lazy, like me :)
reflectionEquals / HashCode (Object). Methods, of course, should be used together. Suitable when the equality of two objects means the equality of the values ​​of all fields. Eclipse users will argue that the “generate hashCode () and equals ()” menu option has long been available. However, these two Builders automatically take into account the structural changes of an object during its evolution, such as adding fields.