Rarely available and hard-to-write Java utilities, written correctly, and thoroughly tested (> 98% code coverage via JUnit tests).
To include in your project:
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>1.22.0</version>
</dependency>
Like java-util and find it useful? Tip bitcoin: 1MeozsfDpUALpnu3DntHWXxoPJXvSAXmQA
Also, check out json-io at https://github.com/jdereg/json-io
YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications.
Including in java-util:
- ArrayUtilities - Useful utilities for working with Java's arrays [ ]
- ByteUtilities - Useful routines for converting byte[] to HEX character [] and visa-versa.
- CaseInsensitiveMap - When Strings are used as keys, they are compared without case. Can be used as regular Map with any Java object as keys, just specially handles Strings.
- CaseInsensitiveSet - Set implementation that ignores String case for contains() calls, yet can have any object added to it (does not limit you to adding only Strings to it).
- Converter - Convert from once instance to another. For example, convert("45.3", BigDecimal.class) will convert the String to a BigDecimal. Works for all primitives, primitive wrappers, Date, java.sql.Date, String, BigDecimal, BigInteger, AtomicBoolean, AtomicLong, etc. The method is very generous on what it allows to be converted. For example, a Calendar instance can be input for a Date or Long. Examine source to see all possibilities.
- DateUtilities - Robust date String parser that handles date/time, date, time, time/date, string name months or numeric months, skips comma, etc. English month names only (plus common month name abbreviations), time with/without seconds or milliseconds, y/m/d and m/d/y ordering as well.
- DeepEquals - Compare two object graphs and return 'true' if they are equivalent, 'false' otherwise. This will handle cycles in the graph, and will call an equals() method on an object if it has one, otherwise it will do a field-by-field equivalency check for non-transient fields.
- EncryptionUtilities - Makes it easy to compute MD5 checksums for Strings, byte[], as well as making it easy to AES-128 encrypt Strings and byte[]'s.
- Executor - One line call to execute operating system commands. Executor executor = new Executor(); executor.exec('ls -l'); Call executor.getOut() to fetch the output, executor.getError() retrieve error output. If a -1 is returned, there was an error.
- GraphComparator - Compare any two Java object graphs. It generates a
List
ofDelta
objects which describe the difference between the two Object graphs. This Delta list can be played back, such thatList deltas = GraphComparator.delta(source, target); GraphComparator.applyDelta(source, deltas)
will bring source up to match target. See JUnit test cases for example usage. This is a completely thorough graph difference (and apply delta), including support forArray
,Collection
,Map
, and object field differences. - IOUtilities - Handy methods for simplifying I/O including such niceties as properly setting up the input stream for HttpUrlConnections based on their specified encoding. Single line .close() method that handles exceptions for you.
- MathUtilities - Handy mathematical algorithms to make your code smaller. For example, minimum of array of values.
- ReflectionUtils - Simple one-liners for many common reflection tasks.
- SafeSimpleDateFormat - Instances of this class can be stored as member variables and reused without any worry about thread safety. Fixing the problems with the JDK's SimpleDateFormat and thread safety (no reentrancy support).
- StringUtilities - Helpful methods that make simple work of common String related tasks.
- SystemUtilities - A Helpful utility methods for working with external entities like the OS, environment variables, and system properties.
- TrackingMap - Map class that tracks when the keys are accessed via .get() or .containsKey(). Provided by @seankellner
- Traverser - Pass any Java object to this Utility class, it will call your passed in anonymous method for each object it encounters while traversing the complete graph. It handles cycles within the graph. Permits you to perform generalized actions on all objects within an object graph.
- UniqueIdGenerator - Generates a Java long unique id, that is unique across up to 100 servers in a cluster, never hands out the same value, has massive entropy, and runs very quickly.
- UrlUtitilies - Fetch cookies from headers, getUrlConnections(), HTTP Response error handler, and more.
- UrlInvocationHandler - Use to easily communicate with RESTful JSON servers, especially ones that implement a Java interface that you have access to.
Version History
- 1.22.0
- Added
GraphComparator
which is used to compute the difference (delta) between two object graphs. The generatedList
of Delta objects can be 'played' against the source to bring it up to match the target. Very useful in transaction processing systems. - 1.21.0
- Added
Executor
which is used to execute Operating System commands. For example,Executor exector = new Executor(); executor.exec("echo This is handy"); assertEquals("This is handy", executor.getOut().trim());
- bug fix:
CaseInsensitiveMap
, when passed aLinkedHashMap
, was inadvertently using a HashMap instead. - 1.20.5
CaseInsensitiveMap
intentionally does not retain 'not modifiability'.CaseInsensitiveSet
intentionally does not retain 'not modifiability'.- 1.20.4
- Failed release. Do not use.
- 1.20.3
TrackingMap
changed so thatget(anyKey)
always marks it as keyRead. Same forcontainsKey(anyKey)
.CaseInsensitiveMap
has a constructor that takes aMap
, which allows it to take on the nature of theMap
, allowing for case-insensitiveConcurrentHashMap
, sortedCaseInsensitiveMap
, etc. The 'Unmodifiable'Map
nature is intentionally not taken on. The passed inMap
is not mutated.CaseInsensitiveSet
has a constructor that takes aCollection
, nwhich allows it to take on the nature of theCollection
, allowing for sortedCaseInsensitiveSets
. The 'unmodifiable'Collection
nature is intentionally not taken on. The passed inSet
is not mutated.- 1.20.2
TrackingMap
changed so that an existing key associated to null counts as accessed. It is valid for many Map types to allow null values to be associated to the key.TrackingMap.getWrappedMap()
added so that you can fetch the wrapped Map.- 1.20.1
- TrackingMap changed so that .put() does not mark the key as accessed.
- 1.20.0
TrackingMap
added. Create this map around any type of Map, and it will track which keys are accessed via .get(), .containsKey(), or .put() (when put overwrites a value already associated to the key). Provided by @seankellner.- 1.19.3
- Bug fix:
CaseInsensitiveMap.entrySet()
- callingentry.setValue(k, v)
while iterating the entry set, was not updating the underlying value. This has been fixed and test case added. - 1.19.2
- The order in which system properties are read versus environment variables via the
SystemUtilities.getExternalVariable()
method has changed. System properties are checked first, then environment variables. - 1.19.1
- Fixed issue in
DeepEquals.deepEquals()
where a Container type (Map
orCollection
) was being compared to a non-container - the result of this comparison was inconsistent. It is always false if a Container is compared to a non-container type (anywhere within the object graph), regardless of the comparison order A, B versus comparing B, A. - 1.19.0
StringUtilities.createUtf8String(byte[])
API added which is used to easily create UTF-8 strings without exception handling code.StringUtilities.getUtf8Bytes(String s)
API added which returns a byte[] of UTF-8 bytes from the passed in Java String without any exception handling code required.ByteUtilities.isGzipped(bytes[])
API added which returns true if thebyte[]
represents gzipped data.IOUtilities.compressBytes(byte[])
API added which returns the gzipped version of the passed inbyte[]
as abyte[]
IOUtilities.uncompressBytes(byte[])
API added which returns the original byte[] from the passed in gzippedbyte[]
.- JavaDoc issues correct to support Java 1.8 stricter JavaDoc compilation.
- 1.18.1
UrlUtilities
now allows for per-threaduserAgent
andreferrer
as well as maintains backward compatibility for setting these values globally.StringUtilities
getBytes()
andcreateString()
now allow null as input, and return null for output for null input.- Javadoc updated to remove errors flagged by more stringent Javadoc 1.8 generator.
- 1.18.0
- Support added for
Timestamp
inConverter.convert()
null
can be passed intoConverter.convert()
for primitive types, and it will return their logical 0 value (0.0f, 0.0d, etc.). For primitive wrappers, atomics, etc, null will be returned.- "" can be passed into
Converter.convert()
and it will set primitives to 0, and the object types (primitive wrappers, dates, atomics) to null.String
will be set to "". - 1.17.1
- Added full support for
AtomicBoolean
,AtomicInteger
, andAtomicLong
toConverter.convert(value, AtomicXXX)
. Any reasonable value can be converted to/from these, including Strings, Dates (AtomicLong
), allNumber
types. IOUtilities.flush()
now supportsXMLStreamWriter
- 1.17.0
UIUtilities.close()
now supportsXMLStreamReader
andXMLStreamWriter
in addition toCloseable
.Converter.convert(value, type)
- a value of null is supported, and returns null. A null type, however, throws anIllegalArgumentException
.- 1.16.1
- In
Converter.convert(value, type)
, the value is trimmed of leading / trailing white-space if it is a String and the type is aNumber
. - 1.16.0
- Added
Converter.convert()
API. Allows converting instances of one type to another. Handles all primitives, primitive wrappers,Date
,java.sql.Date
,String
,BigDecimal
, andBigInteger
. Additionally, input (from) argument acceptsCalendar
. - Added static
getDateFormat()
toSafeSimpleDateFormat
for quick access to thread local formatter (per formatString
). - 1.15.0
- Switched to use Log4J2 () for logging.
- 1.14.1
- bug fix:
CaseInsensitiveMa.keySet()
was only initializing the iterator once. IfkeySet()
was called a 2nd time, it would no longer work. - 1.14.0
- bug fix:
CaseInsensitiveSet()
, the return value foraddAll()
,returnAll()
, andretainAll()
was wrong in some cases. - 1.13.3
EncryptionUtilities
- Added byte[] APIs. Makes it easy to encrypt/decryptbyte[]
data.pom.xml
had extraneous characters inadvertently added to the file - these are removed.- 1.13.1 & 13.12 - issues with sonatype
- 1.13.0
DateUtilities
- Day of week allowed (properly ignored).DateUtilities
- First (st), second (nd), third (rd), and fourth (th) ... supported.DateUtilities
- The default toString() standard date / time displayed by the JVM is now supported as a parseable format.DateUtilities
- Extra whitespace can exist within the date string.DateUtilities
- Full time zone support added.DateUtilities
- The date (or date time) is expected to be in isolation. Whitespace on either end is fine, however, once the date time is parsed from the string, no other content can be left (prevents accidently parsing dates from dates embedded in text).UrlUtilities
- Removed proxy from calls toURLUtilities
. These are now done through the JVM.- 1.12.0
UniqueIdGenerator
uses 99 as the cluster id when the JAVA_UTIL_CLUSTERID environment variable or System property is not available. This speeds up execution on developer's environments when they do not specifyJAVA_UTIL_CLUSTERID
.- All the 1.11.x features rolled up.
- 1.11.3
UrlUtilities
- separated out call that resolvesres://
to a public API to allow for wider use.- 1.11.2
- Updated so headers can be set individually by the strategy (
UrlInvocationHandler
) InvocationHandler
set to always usesPOST
method to allow additionalHTTP
headers.- 1.11.1
- Better IPv6 support (
UniqueIdGenerator
) - Fixed
UrlUtilities.getContentFromUrl()
(byte[]
) no longer setting upSSLFactory
whenHTTP
protocol used. - 1.11.0
UrlInvocationHandler
,UrlInvocationStrategy
- Updated to allow more generalized usage. Pass in your implementation ofUrlInvocationStrategy
which allows you to set the number of retry attempts, fill out the URL pattern, set up the POST data, and optionally set/get cookies.- Removed dependency on json-io. Only remaining dependency is Apache commons-logging.
- 1.10.0
- Issue #3 fixed:
DeepEquals.deepEquals()
allows similarMap
(orCollection
) types to be compared without returning 'not equals' (false). Example,HashMap
andLinkedHashMap
are compared on contents only. However, compare aSortedSet
(likeTreeMap
) toHashMap
would fail unless the Map keys are in the same iterative order. - Tests added for
UrlUtilities
- Tests added for
Traverser
- 1.9.2
- Added wildcard to regex pattern to
StringUtilities
. This API turns a DOS-like wildcard pattern (where * matches anything and ? matches a single character) into a regex pattern useful inString.matches()
API. - 1.9.1
- Floating-point allow difference by epsilon value (currently hard-coded on
DeepEquals
. Will likely be optional parameter in future version). - 1.9.0
MathUtilities
added. Currently, variable lengthminimum(arg0, arg1, ... argn)
andmaximum()
functions added. Available forlong
,double
,BigInteger
, andBigDecimal
. These cover the smaller types.CaseInsensitiveMap
andCaseInsensitiveSet
keySet()
andentrySet()
are faster as they do not make a copy of the entries. Internally,CaseInsensitiveString
caches it's hash, speeding up repeated access.StringUtilities levenshtein()
anddamerauLevenshtein()
added to compute edit length. See Wikipedia for understand of the difference. Currently recommend usinglevenshtein()
as it uses less memory.- The Set returned from the
CaseInsensitiveMap.entrySet()
now contains mutable entry's (value-side). It had been using an immutable entry, which disallowed modification of the value-side during entry walk. - 1.8.4
UrlUtilities
, fixed issue where the default settings for the connection were changed, not the settings on the actual connection.- 1.8.3
ReflectionUtilities
has newgetClassAnnotation(classToCheck, annotation)
API which will return the annotation if it exists within the classes super class hierarchy or interface hierarchy. Similarly, thegetMethodAnnotation()
API does the same thing for method annotations (allow inheritance - class or interface).- 1.8.2
CaseInsensitiveMap
methodskeySet()
andentrySet()
return Sets that are identical to how the JDK returns 'view' Sets on the underlying storage. This means that all operations, besidesadd()
andaddAll()
, are supported.CaseInsensitiveMap.keySet()
returns aSet
that is case insensitive (not aCaseInsensitiveSet
, just aSet
that ignores case). Iterating thisSet
properly returns each originally stored item.- 1.8.1
- Fixed
CaseInsensitiveMap() removeAll()
was not removing when accessed viakeySet()
- 1.8.0
- Added
DateUtilities
. See description above. - 1.7.4
- Added "res" protocol (resource) to
UrlUtilities
to allow files from classpath to easily be loaded. Useful for testing. - 1.7.2
UrlUtilities.getContentFromUrl() / getContentFromUrlAsString()
- removed hard-coded proxy server name- 1.7.1
UrlUtilities.getContentFromUrl() / getContentFromUrlAsString()
- allow content to be fetched asString
or binary (byte[]
).- 1.7.0
SystemUtilities
added. New API to fetch value from environment or System propertyUniqueIdGenerator
- checks for environment variable (or System property) JAVA_UTIL_CLUSTERID (0-99). Will use this if set, otherwise last IP octet mod 100.- 1.6.1
- Added:
UrlUtilities.getContentFromUrl()
- 1.6.0
- Added
CaseInsensitiveSet
. - 1.5.0
- Fixed:
CaseInsensitiveMap's iterator.remove()
method, it did not remove items. - Fixed:
CaseInsensitiveMap's equals()
method, it required case to match on keys. - 1.4.0
- Initial version
By: John DeRegnaucourt and Ken Partlow