• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Heterogeneous map and generics

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
Sheriff
Posts: 28329
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic