Java Developer Corner: Everyday Libraries
For all the time spent writing Java code, I have formed a certain set of useful third-party libraries that are firmly embedded in the classpath, and without which not a single day of development can be done, whether it's writing something on the knee or working on serious project. This is not about “monsters” like Spring, Struts, Hibernate (this is another story), but rather about utilities that fill in the gaps in the Java SE API and save a dozen or two extra lines of code / minutes here and there. I would like to share this information with the habrasociety - I hope it will be useful especially to those who are just starting to storm Java, and will allow a little, but increase productivity.
So, a list of the classes and methods I often use with comments:
Apache Commons: Collections
A very good library that greatly complements the Java SE Collections Framework. Very useful especially for those who are stuck on Java 2 SE 1.4.2 (sometimes it happens) and still dream of LinkedHashMap :)
File management is not the strongest side of the platform that Java critics like to use (sometimes, well-deserved). For example, a request to implement java.io.File.copy () has been hanging for 10 years. Commons IO partially helps out:
Utilities for working with strings, reflection, serialization, objects and the system. Perhaps the most used library from Apache Commons.
So, a list of the classes and methods I often use with comments:
Apache Commons: Collections
A very good library that greatly complements the Java SE Collections Framework. Very useful especially for those who are stuck on Java 2 SE 1.4.2 (sometimes it happens) and still dream of LinkedHashMap :)
- CollectionUtils . Perhaps the most commonly used class with a bunch of "tasty":
- filter / find (Collection, Predicate) - filtering and searching by predicate.
- forAllDo (Collection, Closure) - Performs Closure for each element (will they ever add Closure to Java?)
- is (Not) Empty (Collection) - allows not checking for null before calling (a trifle, but nice, compare with java.util.Collection.isEmpty ())
- isEqualCollection (Collection, Collection) - never had to compare two collections manually?
- A few dozen more useful methods.
- filter / find (Collection, Predicate) - filtering and searching by predicate.
- ClosureUtils - for those who have long dreamed of the emergence of full-fledged Closure in Java.
- Many other classes of different levels of utility (hereinafter I list the most often used in my case).
- Dbutils
- closeQuietly (Connection, Statement, ResultSet) - working with JDBC directly? Tired of constantly writing nested if / try / catch / finally to properly close resources with null checking? 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 shape:try { // do some db work } catch (Exception e) { e.printStackTrace (); } finally { DbUtils.closeQuietly (conn, st, rs); }
- closeQuietly (Connection, Statement, ResultSet) - working with JDBC directly? Tired of constantly writing nested if / try / catch / finally to properly close resources with null checking? Then to you here :) Typical code "from the textbook"
File management is not the strongest side of the platform that Java critics like to use (sometimes, well-deserved). For example, a request to implement java.io.File.copy () has been hanging for 10 years. Commons IO partially helps out:
- Fileutils
- copyDirectory (File, File) - copy directories
- copyFile (File, File) - copy files
- listFiles (File, String [], boolean) - a list of files by extension and recursively. What java.io.File never learned.
- readFileToString (File, String)
- writeStringToFile (File, String)
- copyDirectory (File, File) - copy directories
- IOUtils
- closeQuietly (Reader / Writer / InputStream / OutputStream) - like DbUtils, it really helps to avoid spaghetti-like code, which often happens when if / try / catch / finally is piled up to just close the I / O resource.
- copy (InputStream, OutputStream) is a classic.
- closeQuietly (Reader / Writer / InputStream / OutputStream) - like DbUtils, it really helps to avoid spaghetti-like code, which often happens when if / try / catch / finally is piled up to just close the I / O resource.
Utilities for working with strings, reflection, serialization, objects and the system. Perhaps the most used library from Apache Commons.
- StringUtils - a huge number of methods for manipulating strings.
- is (Not) Blank / Empty (String) - it's
if (s!=null && s.trim().length()>0)
time to forget about it.
- is (Not) Blank / Empty (String) - it's
- StringEscapeUtils
- (un) escapeSql (String) - for those who do not use PreparedStatement :)
- (un) escapeHtml (String) - where are the Web developers without it.
- (un) escapeSql (String) - for those who do not use PreparedStatement :)
- ToStringBuilder
- reflectionToString (Object) is a reflection-based implementation of toString (). That is, the result of the method will automatically change when adding or deleting the fields of the object. For the lazy, like me :)
- reflectionToString (Object) is a reflection-based implementation of toString (). That is, the result of the method will automatically change when adding or deleting the fields of the object. For the lazy, like me :)
- EqualsBuilder & HashCodeBuilder
- 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 object that there has long been a menu option “generate hashCode () and equals ()”. However, these two Builders automatically take into account structural changes of an object during its evolution, such as adding fields.
- 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 object that there has long been a menu option “generate hashCode () and equals ()”. However, these two Builders automatically take into account structural changes of an object during its evolution, such as adding fields.
- ExceptionUtils
- getFullStrackTrace (Throwable). For 13 years, java.lang.Throwable did not have a simple but necessary method that would produce a trace in the form of String ...
- getFullStrackTrace (Throwable). For 13 years, java.lang.Throwable did not have a simple but necessary method that would produce a trace in the form of String ...