A sneek peak on the features of the new release of Java(unofficial). How about you, what features would you like to be included in the new release?
http://www.javabeat.net/articles/120-new-features-in-java-70-part-1-1.htmls
Introduction
This article discusses the new features that are available in Java 7.0. Java 7.0 comes with bunch of new features – language level changes as well as API level changes and this article concentrates on the new API features such as Cache API, New Date and Time API and New IO 2 as well as the language level changes in the form of Super packages. The readers are assumed that they are more familiar with the Java 5.0 language level changes such as Generics and Annotations. This article covers few of the new features in Java 7.0 and the remaining set of new features will be covered in the next article. It is likely that the APIs may undergo changes before the formal release of Java 7.0 happens. You can read Java 7.0 Articles.
New features in Java 7.0
The following new features will be discussed in detail in this article.
- Cache API
- Date and Time API
- New IO2
- Super Packages
Cache API
The Cache API provides the mechanism for storing and retrieving the objects. The API also provides a way to plug-in user defined Cache objects. The Cache API is relatively small and this section discusses the core classes and the interfaces available in the Cache API.
Cache Manager
The Cache Manager stands as the entry point for using Cache API. An instance of the CacheManager can be obtained using the following code,
CacheManager manager = CacheManager.getInstance();
This method will return a singleton object of type CacheManager. The cache manager also provides methods for registering and deregistering cache objects. Have a look at the following code,
Cache permanentCache = .... // an implementation of a permanent cache
manager.registerCache("PERM_CACHE", permanentCache);
Cache and CacheFactory
The API provides plug-in capabilities of custom implementation through the help of Cache and the CacheFactory interfaces. The Cache interface extends the Map interface so the put, get and iteration methods that are available in the map can be directly used. It is also possible to remove the objects in the cache that are no longer needed through the Cache.evict() method. CacheFactory is a service provider API and it provides a factory method for creating a Cache object from a map of properties.
CacheManager manager = CacheManager.getInstance();
CacheFactory factory = manager.getCacheFactory();
Map cacheProperties = ....;
Cache cacheObject = factory.createCache(cacheProperties);
An object is no longer needed in the cache if the expiration time set for the object expires and that’s where the CacheEntry object comes in.
CacheEntry
The interface provides certain useful information and operations that are applicable for an object in a cache such as the last access time, last update time, creation time, expiration time etc. Note that this interface directly extends Map.Entry interface. It is possible to obtain an instance of this object from the object’s key,
Cache permanentCache = .... // an implementation of a permanent cache
permanentCache.put("MYSTR", someStrObject);
CacheEntry entry = cache.getCacheEntry("MYSTR");
Cache Events
It is also possible to attach listeners to cache object when objects are stored, removed, loaded and evicted from the cache. An implementation of a cache listener could be,
SimpleCacheListener.java
class SimpleCacheListener implements CacheListener
{
void onClear() {}
void onEvict(Object key) {}
void onLoad(Object key) {}
void onPut(Object key) {}
void onRemove(Object key) {}
}
This cache listener can be added to cache for receiving any events notification through the following code,
Cache cacheObject = ...;
cacheObject.addListener(new SimpleCacheListener());