Skip to content

Interesting behavior when extending AbstractMap #293

@jonahaapala

Description

@jonahaapala

Me again!

I wanted to do some custom validation while I am deserializing part of my json payload that is a Map<String, Object>, so I had the idea to create my own Map object so I could override put() as an interception point. As an aside, I initially tried using reader.readMap() which takes in key and value readers, but I want to be able to quickly see if my Map experienced any errors during parsing as well as have access to the original values for reporting/diagnostics purposes. So my setup became something like this:

public class MyMap extends HashMap<String, Object> {
        ValidationError error = NONE;
        String sampleKey;

        @Override
        public Object put(String key, Object value) {
                if (/*key validation*/) { error = KEY_ERROR; sampleKey = key; }
                if (/*value validation*/) { error = VALUE_ERROR; sampleKey = key; }
                else { return super.put(key, value); }
        }
}

And this works! But I wanted to use the fast-utils Object2ObjectOpenHashMap instead, but switching to that did not work. I was able to work-around it by have MapMap extend AbstractMap<String, Object> and have MyMap have a delegate map, but I was wondering if there is a reason that you can't extend any valid Map implementation rather than just the ones provided by the Java runtime? This is not a huge deal, but I figured I'd see if it was something possible to add.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions