Skip to main content

Java Concurrent Set and Lifo Queue - Problem with java.util.Collections factory methods

Today I got to know about java.util.Collections.newSetFromMap() method, added in Java 6 (aka Java 1.6), through [JavaSpecialists] newsletter.

Notwithstanding its utility, I was a little disturbed by its name. By and large, the naming in Java has been done considerable effort with focus on affordance. The semantics of the elements used to match quite closely with the names.

This one have too vague a name, and the other I found, asLifoQueue.

Apart from this, a much bigger problem these methods in API pose is the semantic override they provide on the returned interface. In newSetFromMap method a Set interface is returned but is heavily dependent on the underlying Map.

As a general rule of generalization is that one should not assume any specialization, for example, if a reference of type Map has ConcurrentHashMap object, the user should not assume that it is concurrent. The only way to use it concurrently should be to downcast it and then test it as ConcurrentHashMap.

Set<String> names = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()
The above example creates an issue; how does a user of names verify if the set is concurrent or not. This problem is more than just an academic issue, if the scope of the object is more than a few lines of code, much real issue if this object is passed across methods.

Similarly, java.util.Collections.asLifoQueue returns a Queue Interface. The user of the returned reference cannot infer if the queue is LIFO.

It would have been much better if the API added additional wrapper instead of wrapper factory methods. The semantics of the operations of these collections could be appropriately documented where they could be useful for the consumer of the objects returned from these methods.
For the users, I would recommend NOT to use the methods directly, instead declare classes and use the wrapper methods in those classes.

So, to make a concurrent set use this:
public class ConcurrentSet<E> implements Set<E> {
 private Set<E> underlyingSet;
 public ConcurrentSet(){
  this(new ConcurrentHashMap<E, Boolean>());
 }
 public ConcurrentSet(ConcurrentHashMap<E,Boolean> concurrentHashMap){
  underlyingSet = Collections.newSetFromMap(concurrentHashMap);
 }
 // …
 // Delegate all methods of Set interface to underlyingSet,
 // you can use Eclipse->Source->Generate Delegate Methods.
}
Download Source Code for ConcurrentSet.

Similarly for a FIFOQueue, use this:

public class FIFOQueue<E> implements Queue<E> {
 private Queue<E> underlyingQueue;
 public FIFOQueue(){
  this(new LinkedList<E>());
 }
 public FIFOQueue(Deque<E> deque){
  this.underlyingQueue = Collections.asLifoQueue(deque);
 }
 // …
 // Delegate all methods of Queue interface to underlyingQueue,
 // you can use Eclipse->Source->Generate Delegate Methods.
}

Download Source Code for FIFIQueue.

Remember the best practice is to:Program for other programmers; any dummy can program for the computer.

Comments

Popular posts from this blog

بر بحر "گلس ہنس دیئے نقاب الٹ دی بہار نے"

ماہِ صیام آنکھ کھولی ہے بہار نے
مولا کو جانشین دیا کردگار نے

خوش ہیں علی مسکرا رہیں ہیں فاطمہ
"گلس ہنس دیئے نقاب الٹ دی بہار نے"

شبیہِ رسول اور حاملِ عظمتِ نبی
تحفہ دیا ہے سیدہ پروردگار نے

جھکایا سر کو بارگہِ امامت میں دست بند
حکمت و حلم اور عظمت و کردار نے

امامِ حسن کے صفین میں لپکنے پر
روکا بحوالہ نبی انہیں کرار نے

پیشِ نظر تحفظِ دینِ رسول تھا
قلم کی پیروی کرلی جبھی تلوار نے

ڈوبتے نے لیا نامِ علی مشکل میں
مارے ساحل کے لیئے ہاتھ منجدھار نے

دورانِ امن ضبط سکھایا تھا حسن نے
اور ضبط دکھایا جنگ میں علمدار نے

اس ہمتِ قلم پر سفر کو معافی مولا
بڑی کی ہے یہ جرِءت اس خاکسار نے

Putting Ideas in the public

I have a gift (or a curse); I can think on many different fronts at the same time, often too many.

One of those fronts is technical / business ideas. There is a ever growing list of ideas that come to my mind. And I want to make use of each of those.

However realizing that I have been not so productive with these ideas. I spend too much time in generating and enhancing these Ideas and contemplating on them and do not focus enough to get through with them to convert them into value. And I know Ideas do not have any value unless they get used.

Lately I have been thinking about this and I thought instead of keeping the ideas to myself in hope that I will get time and focus to implement them why don't I put those Ideas on the web to the public.

This got me thinking....

However, some part of me does not want to let go of the ideas, I see business potentials in many of the these ideas and being a normal person, I wish I could get some benefit out of it. However, I know that stinginess …

Integrating Blogger with Google Sites

I wanted to integrate my blogger site (this is the first post on blogger) to be shown on my site hosted by google sites.

Though both Google Sites and Blogspot are owned by Google, apparently there isn't enough market base for google to have them integrated.

On  Google Forum some people suggest to integrate them using RSS Feed, blogpost being the rss provider and placing a widget for RSS consumer, but that would only show the recent blogs (depending upon settings) and the other method is to use iFrame, page within a page.

I went ahead with tsinn implementation, downloading his xml template for blogspot, chose not to delete the widgets (I was not sure whether I needed them or not). Hid the widgets using css.
header, .Profile, .Followers, .Attribution,.blog-feeds {
  display: none;
}[Note: I could not identify a rational of the structure, naming and capitalization, and in absense of this understanding did not like the inconsistency in Template XML for blogspot]

Added the iFrame with hei…