Skip to content

Latest commit

 

History

History
155 lines (127 loc) · 12.3 KB

File metadata and controls

155 lines (127 loc) · 12.3 KB

Overview

  1. How to build a proper utility class in Java 8+

Item-0480: Don't reinvent the wheel

  1. You are probably reinventing the wheel :-)
  2. Verify Core Java, Apache Commons, Guava & Spring lack what you're building

Core Java

  1. Arrays
  2. Base64
  3. Collections
  4. Executors
  5. Files
  6. FileSystems
  7. Math
  8. Objects
  9. Paths
  10. Spliterators
  11. TimeUnit

Apache Commons

  1. ArithmeticUtils
  2. BagUtils
  3. BeanUtils
  4. BooleanUtils
  5. CaseUtils
  6. ClassUtils
  7. CollectionUtils
  8. ComparatorUtils
  9. ConvertUtils
  10. DbUtils
  11. DigestUtils
  12. FileUtils
  13. IOUtils
  14. KeyManagerUtils
  15. ListUtils
  16. MapUtils
  17. MathUtils
  18. MethodUtils
  19. MimeMessageUtils
  20. ObjectUtils
  21. PropertyUtils
  22. SSLContextUtils
  23. StringEscapeUtils
  24. StringUtils
  25. SystemUtils
  26. TrieUtils
  27. TrustManagerUtils
  28. WordUtils

Guava

  1. Atomics
  2. Booleans
  3. ByteStreams
  4. Chars
  5. Closeables
  6. Doubles
  7. Escapers
  8. Files
  9. Flushables
  10. Graphs
  11. InetAddresses
  12. Ints
  13. Iterables
  14. Iterators
  15. Lists
  16. Longs
  17. Maps
  18. MoreExecutors
  19. MoreFiles
  20. Multimaps
  21. Multisets
  22. ObjectArrays
  23. Preconditions
  24. Primitives
  25. Queues
  26. Runnables
  27. Sets
  28. Stats
  29. Strings
  30. Tables
  31. Uninterruptibles
  32. UrlEscapers

Spring (Prefer Apache Commons & Guava over Spring utils)

  1. AnnotationUtils
  2. Base64Utils
  3. BeanUtils
  4. ClassUtils
  5. DataSourceUtils
  6. DomUtils
  7. FileSystemUtils
  8. MimeTypeUtils
  9. ReflectionTestUtils
  10. SystemPropertyUtils
  11. UriUtils
  12. ValidationUtils

Item-0482: Javadoc

  1. Add brief javadoc, with a summary of purpose
  2. Document who is responsible for closing resources (like IO Streams)
    1. Caller is responsible or callee is responsible

Item-0483: Final

  1. Mark the utility class final

Item-0484: Stateless

  1. No instance properties on the utility class
    1. static final constants are acceptable (at top of file)

Item-0485: Naming

  1. Package name: ends with .util; (Eg. com.abc.util)
  2. Class name: ends with Utils (eg. see Apache commons examples above)
    1. Core Java & Guava tend to name utils after a Type (eg. Foos contains methods to simplify using Foo)
    2. Apache Commons and Spring tend to name utilities with the Utils suffix
    3. Avoid naming collisions by using the Utils suffix (and not reinventing the wheel)

Item-0486: Constructor

  1. private constructor
  2. Exactly one constructor
  3. constructor accepts zero arguments

Item-0487: Methods

  1. Length: Less than 75-lines per method
  2. Args: Add Preconditions on method args (unless they are nullable)
  3. All methods must be static
  4. Use method signatures consistent with other popular utilities (Apache Commons, Guava, ...)
  5. Ensure all methods are Stateless
  6. Ensure all methods are threadsafe
  7. Three arguments max. Accept a POJO if you need mor args.
  8. Never mutate method Arguments

Item-0488: Tests

  1. Add "enough" tests to exercise both positive and negative cases

TODO: Find a home for these

  • Lenient in args, Strict on output
  • Use modern types
  • no hard coded dates, pass clock