aspose file tools*
The moose likes Java in General and the fly likes Heterogeneous map and generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Heterogeneous map and generics" Watch "Heterogeneous map and generics" New topic
Author

Heterogeneous map and generics

Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
All,

I am trying to write a heterogeneous map where I can get compile time type safety.

The goal is to have a map of attributes where a producer can put data in and consumer can retrieve it without casting.
I am not using any messaging API. This will be used in pure JAVA app without any fancy frameworks.
What do you think about this approach:

Some of my colleges say that type safety if overrated and we should use regular Object->Object dictionary. This approach will assume that users know that source of information and can appropriately cast it at run time. Do you think it is batter to keep it simple or enforce type safety?


Attribute class (I can not use enum. I want to use this as an API and do not want anyone to stick new attributes in the jar once it was released. Unfortunately, enum does not support inheritance. User of the API can extend the attribute to create new keys.):


AttributeList(not using collection):


Simple test:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

As far as I can see, this is just a complicated way of writing a container which can optionally contain a String, a Date, a Double, and an Integer. It would be simpler to do this:


Also, you're using a static Map to store the data, which means you've sort of written a Singleton object but don't have the proper controls, and you've created that map in the Attribute class and not in the AttributeList class where I think it belongs. So creating two of those objects (whichever class has the static variable) is going to lead you into trouble.

Also I think it's a bad practice to name something "SomethingList" when it uses a Map to store things. Calling it a "List" leads the user to believe he/she can add as many Dates as he/she likes, when it doesn't actually work that way.

I don't see anything about type safety in your question, really. You'll notice that in my equivalent class, the question of type safety doesn't arise.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Paul Clapham wrote:
Also, you're using a static Map to store the data, which means you've sort of written a Singleton object but don't have the proper controls, and you've created that map in the Attribute class and not in the AttributeList class where I think it belongs. So creating two of those objects (whichever class has the static variable) is going to lead you into trouble.


I tried to simulate enum. I want to use this class in different layers of the app which will communicate via simple messaging interface.
This is not singleton since it is expendable. However, I enforce "singleton property" for each attribute. I use combination of readResolve(), protected constructor and builder to enforce single instance of the attribute of a particular id.

Since I want users to extend it before use, it should be a class and not part of the AttributeList.


Also I think it's a bad practice to name something "SomethingList" when it uses a Map to store things. Calling it a "List" leads the user to believe he/she can add as many Dates as he/she likes, when it doesn't actually work that way.


I totally agree to that. Will rename ASAP.

My sample may have been misleading. I will have a message that will contain attributes. The messages will be exchanged through a message broker between different components of the app. I want complete decoupling and have similar system to NotificationCenter of Objective C.

Your container will work but has some limitations. I have to store this container in some sort of the collection.
I may have two containers with the same type. For example start Date and end Date. How will consumer distinguish them without extra information (some tag may be)?


On the other hand, we can create a class per type (similar to what I do with attribute). This may lead to a maintenance issue. Also, I will need some construct for this containers to make all of them immutable/mutable. I would like to keep it simple and use a dictionary where anyone can put/get data. This concept is easy to understand and use but will lead to type safety issue. For example, if I use a map and decide to use String for data while consumer was expecting another object, the issue will arise at run time only. I want to know about it at compile time and not hear about it from production env.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Heterogeneous map and generics