diff --git a/src/flatland/ordered/set.clj b/src/flatland/ordered/set.clj index 21c424c..0257069 100644 --- a/src/flatland/ordered/set.clj +++ b/src/flatland/ordered/set.clj @@ -56,7 +56,7 @@ (toString [this] (str "#{" (clojure.string/join " " (map str this)) "}")) (hashCode [this] - (reduce + (keep #(when % (.hashCode ^Object %)) (.seq this)))) + (reduce + (keep #(when (some? %) (.hashCode ^Object %)) (.seq this)))) (equals [this other] (or (identical? this other) (and (instance? Set other) diff --git a/test/flatland/ordered/set_test.clj b/test/flatland/ordered/set_test.clj index ddae56c..c2fa070 100644 --- a/test/flatland/ordered/set_test.clj +++ b/test/flatland/ordered/set_test.clj @@ -165,6 +165,11 @@ (is (= (.hashCode (ordered-set nil)) (.hashCode (hash-set nil)))) (is (= (.hashCode (ordered-set nil :a {:b nil})) (.hashCode (hash-set nil :a {:b nil})))))) +(deftest nil-and-false-hashes + (is (not= (.hashCode (ordered-set nil)) (.hashCode (hash-set false)))) + (is (not= (.hashCode (ordered-set false)) (.hashCode (hash-set nil)))) + (is (= (.hashCode (ordered-set false nil)) (.hashCode (hash-set nil false))))) + (deftest nil-hash-code-npe ;; No assertions here; just check that it doesn't NPE ;; See: https://github.com/amalloy/ordered/issues/27