|
Project Information
|
A collection of utility classes for Java APIs Nano TimerOften you need to time a block of code and display the length of time it took. The standard approach is to use either System.currentTimeMillis() or System.nanoTime(). However the display of the time requires formatting to ensure it is readable regardless of how long the timing took. This simple class provides nanosecond accuracy on the timing, coupled with a human readable format for printing the time. NanoTimer timer = new NanoTimer();
timer.start();
// Code to time
timer.stop();
System.out.println("Operation took " + timer);Example output: Operation took 52.1 millis Operation took 1.8 hours Operation took 22.0 seconds Count MapA Count Map provides a quick thread-safe mechanism for counting objects. It is particularly useful for recording statistics, number of times an event has occurred, number of active objects, etc... CountMap<String> map = new CountMap<String>();
map.increment("LoginPage");
map.increment("LoginPage");
map.increment("LoginPage");
map.increment("ActiveUsers", 6);
map.decrement("ActiveUsers", 2);
System.out.println("Logins: " + map.get("LoginPage"));
System.out.println("Active Users: " + map.get("ActiveUsers"));Output: Logins: 3 Active Users: 4 Positive Number StreamsThe Java DataOutput and DataInput interfaces define methods for reading and writing primitives. Often though there are situations where the data needs to be compressed as much as possible without applying an intense compression algorithm. The positive number streams provide methods to read and write positive whole numbers, but using the minimum number of bytes possible. For example a long is 8 bytes, however using these methods it will be between 1 and 9 bytes instead. The algorithm is very simple and uses the first bit from each byte as a flag to indicate whether the following byte is part of the same number. long value = 100;
ByteArrayOutputStream output = new ByteArrayOutputStream();
PositiveNumberOutputStream stream = new PositiveNumberOutputStream(output);
stream.writeLong(value);
System.out.println("Bytes Written: " + output.toByteArray().length);Output: Bytes Written: 1 EnumerationsThe library includes the following useful enumerations. MonthJANUARY, FEBRUARY, etc... LanguageENGLISH, GERMAN, FRENCH, etc... LanguageCountryENGLISH_UNITED_KINGDOM, ENGLISH_UNITED_STATES, CHINESE_CHINA, CHINESE_HONG_KONG, etc... WorldRegionEUROPE, NORTH_AMERICA, ASIA, etc ... |