From 94f3a46f67f1fd2da96805e1f893c1043a6d576a Mon Sep 17 00:00:00 2001 From: Greg Edwards Date: Mon, 13 Apr 2015 20:15:19 -0400 Subject: [PATCH] Impose correctness on LazyKeyValuePair --- library/Support/LazyKeyValuePair.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/Support/LazyKeyValuePair.cs b/library/Support/LazyKeyValuePair.cs index 1af2a0d5..f44e1359 100644 --- a/library/Support/LazyKeyValuePair.cs +++ b/library/Support/LazyKeyValuePair.cs @@ -28,6 +28,7 @@ namespace PkmnFoundations.Support set { m_key = value; + m_has_key = true; m_has_value = false; } } @@ -45,29 +46,44 @@ namespace PkmnFoundations.Support set { m_value = value; + m_has_value = true; m_has_key = false; } } public void EvaluateKey() { +#if DEBUG + AssertHelper.Assert(m_has_key || m_has_value); +#endif if (!m_has_key) m_key = m_key_evaluator(m_value); m_has_key = true; } public void Evaluate() { +#if DEBUG + AssertHelper.Assert(m_has_key || m_has_value); +#endif if (!m_has_value) m_value = m_evaluator(m_key); m_has_value = true; } + /// + /// Causes the Key to be lost so it will be recomputed from the Value. + /// public void InvalidateKey() { + if (!m_has_value) throw new InvalidOperationException(); m_has_key = false; } + /// + /// Causes the Value to be lost so it will be recomputed from the Key. + /// public void Invalidate() { + if (!m_has_key) throw new InvalidOperationException(); m_has_value = false; } }