JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Issue 172: optimize map hasKey() and hasEntry()

Open brownian-motion opened this issue 4 years ago • 8 comments

Implements the enhancement in hamcrest/JavaHamcrest#172

Instead of a linear search over all entries, this change leverages Map.containsKey(K key) to check that a map hasKey() or hasEntry().

brownian-motion avatar Apr 13 '20 04:04 brownian-motion

Thank you for your feedback, @nibsi . I'm afraid I created the branch from my local master branch, which included a different change, instead of origin/master; I'm embarrassed I missed that before pushing. I'll rebase and push a PR without the unrelated change.

brownian-motion avatar Apr 13 '20 12:04 brownian-motion

I've added some tests that cover the relevant edge cases covered by the code addition. Thank you very much for your help, @nibsi! I believe this commit is ready for final review + merging if approved.

@tumbarumba , would you or a colleague in this repo please consider reviewing this PR when convenient?

brownian-motion avatar Apr 16 '20 02:04 brownian-motion

Interesting question, the null thing is not something I thought of. The question is what is the right behaviour when using null with an inappropriate collection. Personally, I would have expected it to fail, rather than mismatch, as that's what would happen in the production code. I fear that we'll be hiding failure modes.

Also, is that a copy'n'paste in the comment?

sf105 avatar Apr 19 '20 14:04 sf105

@sf105 There's one case I can think of where the safe API is better: asserting that a null key is absent:

assertThat(new Hashtable<Object, Object>(), not(hasKey(null)));

I would expect this to pass and continue to the next assertion; allowing a null pointer exception would force you to avoid touching particular types/code paths or start to put conditional logic and try/catches in your tests. Since it seems reasonable to assert that "this map type which prohibits null keys does not contain a null key", I support keeping this API that allows expressing that.

Also, sorry, where do you mean? I'm not sure what copy-and-paste you mean.

brownian-motion avatar Apr 20 '20 01:04 brownian-motion

@sf105 I would be happy to make whichever further changes you think would benefit the project the best. When convenient, please let me know what further adjustments might be appropriate here.

brownian-motion avatar May 15 '20 03:05 brownian-motion

@brownian-motion looks good, please can you rebase from master, as hamcrest-core and hamcrest-library have been refactored a lot and also deprecated, so that everything is just in hamcrest.

nhojpatrick avatar Jun 29 '20 22:06 nhojpatrick

Hello again! I know it's been two years, but I've rebased this PR and would be happy to help with any further steps to get it ready for merging.

@nhojpatrick please feel free to look through this again when convenient :)

brownian-motion avatar Feb 01 '22 00:02 brownian-motion

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates. Still trying to understand how has permissions to perform a release.

nhojpatrick avatar Feb 13 '22 12:02 nhojpatrick