Commits

Meikel Brandmeyer  committed 5faf5aa

Moved LazyMapEntry to reify

  • Participants
  • Parent commits 1633d40
  • Branches new

Comments (0)

Files changed (3)

                 <fileset dir="${lib.dir}" includes="**/*.jar"/>
             </classpath>
             <sysproperty key="clojure.compile.path" value="${classes.dir}"/>
-            <arg value="de.kotka.lazymap.LazyMapEntry"/>
             <arg value="de.kotka.lazymap"/>
         </java>
     </target>

File src/de/kotka/lazymap.clj

      clojure.lang.IMapEntry
      clojure.lang.IPersistentVector
      clojure.lang.ISeq
-     clojure.lang.SeqIterator
-     de.kotka.lazymap.LazyMapEntry))
+     clojure.lang.SeqIterator))
+
+(gen-interface
+  :name    de.kotka.lazymap.ILazyMapEntry
+  :extends [clojure.lang.IMapEntry]
+  :methods [[getRawValue [] clojure.lang.Delay]])
 
 (gen-interface
   :name    de.kotka.lazymap.ILazyMap
   :extends [clojure.lang.IPersistentMap]
   :methods [[lazyAssoc [Object Object] de.kotka.lazymap.ILazyMap]])
 
-(import 'de.kotka.lazymap.ILazyMap)
+(import
+  'de.kotka.lazymap.ILazyMapEntry
+  'de.kotka.lazymap.ILazyMap)
+
+(defn create-lazy-map-entry
+  [k v]
+  (reify this [ILazyMapEntry]
+    ; ILazyMapEntry
+    (.getRawValue
+      []
+      v)
+    ; IMapEntry
+    (.key
+      []
+      (.getKey this))
+    (.getKey
+      []
+      k)
+    (.val
+      []
+      (.getValue this))
+    (.getValue
+      []
+      (force v))
+    ; Object
+    (.toString
+      []
+      (str \[ (.getKey this) \space (.getValue this) \]))))
+
+(defmethod print-method ILazyMapEntry
+  [#^ILazyMapEntry this #^java.io.Writer writer]
+  (.write writer (.toString this)))
 
 (defn create-lazy-map-seq
   ([inner-seq]
      (.first
        []
        (let [first-val (first inner-seq)]
-         (LazyMapEntry. (key first-val) (val first-val))))
+         (create-lazy-map-entry (key first-val) (val first-val))))
      (.next
        []
        (when-let [inner-rest (next inner-seq)]
        (contains? base k))
      (.entryAt
        [k]
-       (LazyMapEntry. k (base k)))
+       (create-lazy-map-entry k (base k)))
      ; IPersistentCollection
      (.count
        []
      (.cons
        [o]
        (condp instance? o
-         LazyMapEntry      (let [#^LazyMapEntry o o]
+         ILazyMapEntry     (let [#^ILazyMapEntry o o]
                              (let [k (.getKey o)
                                    v (.getRawValue o)]
                                (.lazyAssoc this k v)))

File src/de/kotka/lazymap/LazyMapEntry.clj

-;-
-; Copyright 2008,2009 (c) 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.
-
-(clojure.core/ns de.kotka.lazymap.LazyMapEntry
-  (:gen-class
-     :init         init
-     :state        theState
-     :methods      [[getRawValue [] clojure.lang.Delay]]
-     :implements   [clojure.lang.IMapEntry]
-     :constructors {[Object Object] []}))
-
-(defstruct
-  #^{:private true}
-  entry-state
-  :theKey :theValue)
-
-(defn- -init
-  [k v]
-  [[] (struct entry-state k v)])
-
-(defn- -getKey
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (-> this .theState :theKey))
-
-(defn- -key
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (.getKey this))
-
-(defn- -getValue
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (-> this .getRawValue force))
-
-(defn- -val
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (.getValue this))
-
-(defn- -getRawValue
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (-> this .theState :theValue))
-
-(defn- -toString
-  [#^de.kotka.lazymap.LazyMapEntry this]
-  (str \[ (.key this) \space (.val this) \]))
-
-(defmethod print-method de.kotka.lazymap.LazyMapEntry
-  [#^de.kotka.lazymap.LazyMapEntry this #^java.io.Writer w]
-  (.write w (.toString this)))