Commits

Meikel Brandmeyer  committed fde44a1

Add Delay for deferred configuration

  • Participants
  • Parent commits 2e9c95f

Comments (0)

Files changed (2)

File clojuresque/src/main/groovy/clojuresque/Delay.groovy

+/*-
+ * Copyright 2013 © Meikel Brandmeyer.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package clojuresque
+
+class Delay {
+    def Closure fn
+    def value
+
+    Delay(Closure f) {
+        fn = f
+    }
+
+    Delay(Object o) {
+        value = o
+    }
+
+    def getValue() {
+        if (fn != null) {
+            value = fn()
+            fn = null
+        }
+
+        this.value
+    }
+
+    def static force(Delay d) {
+        d.value
+    }
+
+    def static force(Object o) {
+        o
+    }
+}

File clojuresque/src/test/groovy/clojuresque/TestDelay.groovy

+package clojuresque
+
+import spock.lang.Specification
+
+class TestDelay extends Specification {
+    def "a delay returns the delayed value"() {
+        given: "a value"
+        def x = "x"
+
+        when: "delaying it"
+        def d = new Delay({-> x + "y"})
+
+        then: "the delay will return it"
+        d.value == "xy"
+    }
+
+    def "delaying a none Closure is just the identity"() {
+        given: "a value"
+        def x = "x"
+
+        when: "delaying the value without a Closure"
+        def d = new Delay(x)
+
+        and: "changing the value"
+        x = "y"
+
+        then: "then the old value is kept"
+        d.value == "x"
+    }
+
+    def "forcing a delay will get the delayed value"() {
+        given: "a value"
+        def x = "x"
+
+        when: "delaying it"
+        def d = new Delay({-> x})
+
+        and: "changing the original value"
+        x = "y"
+
+        then: "the change will be picked up"
+        Delay.force(d) == "y"
+
+        when: "changing the original value again"
+        x = "z"
+
+        then: "the delay will keep the force value"
+        Delay.force(d) == "y"
+    }
+
+    def "forcing any other object is the identity"() {
+        given: "a value"
+        def x = "x"
+
+        expect: "forcing it to be the identity"
+        Delay.force(x) == x
+    }
+}