-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypedMap.Key is unintuitive #7
Comments
Yes this is intentional. See javadoc on the TypedMap. "* Uses of the map are expected to define the keys they use up-front for their
In your example, you would have a key for StartTime defined once, and accessible to whatever needs to know about StartTime in the map. The unit test would then reference the key object, not try to create a new one. Uniqueness of keys are defined by key objects, not by string names. |
So if I want new keys, should I be creating a new implementation of TypedMap? When I want to check that my map contains something, does that mean the Key needs to be accessible in some global registry of keys, a la
I guess what I'm driving at is why doesn't |
If you want new keys, you define new keys. But yes they have to be accessible from everywhere you need to reference the values stored under the key. Usually there will be some set of predefined keys, similar to an enum definition. You shouldn't need new implementations of TypedMap unless there is some unique implementation type you want (similar to any other interface).
Because TypedMap is not the same as the Map collection. The key is meant to be stored in a TypedMap, not in a Map. Again, see the javadoc - it's not quite the same contract as Map. In the case of TypedMap keys, equals is identity equals, which is the same as default object equals. You can think of a TypedMap similar to an EnumMap or IdentityMap, but where the key also contains information on the type of object stored under the key. |
Apologies, I didn't express myself clearly. The test case was a small subset of the problem I'm trying to solve, How can I write a library that uses the contract exposed by Let's say I have certain constraints I wish to enforce on my
If I write an application that listens on a Socket for strings and tries to reconstruct values from a format like I have no access to the classpath of the calling code, nor mechanism for specifying a precise object reference. I may try to construct the map with something like the following:
Even if I passed along a class name in the payload, so I wasn't guessing at If In another scenario, say I'm writing a caching library, and I want to be able to write metrics by accepting an instance of If I want to check How can I add new elements into the Are these scenarios outside of the intended usage of the API? How would you solve these problems? |
I found a way out of the corner I was in with a couple of utility methods:
Since the right primitives are exposed to basically check the key's If this comes up often in the future, it may be worth considering adding these or a variant to |
Consider the following code
with a test:
That test fails. The assertion is false. Looking at the JavaDoc for TypedMap.Key seems to imply this was intentional.
Why would anyone find that usable? What would the intended usage look like here?
The text was updated successfully, but these errors were encountered: