Permalink
Browse files

Move init code from cljain.

  • Loading branch information...
Ruiyun committed Jul 16, 2012
0 parents commit f7c670527e2e1dbbe7e62c1ba03afd01479f8e38
Showing with 144 additions and 0 deletions.
  1. +12 −0 .gitignore
  2. +49 −0 README.md
  3. +5 −0 project.clj
  4. +72 −0 src/ruiyun/tools/timer/core.clj
  5. +6 −0 test/ruiyun/tools/timer/test/core.clj
@@ -0,0 +1,12 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
+/.idea/
+*.iml
+/autodoc
+autodoc/**
@@ -0,0 +1,49 @@
+ruiyun.tools.timer
+=========
+
+The ruiyun.tools.timer is a Java [Timer] and [TimerTask] wrapper for [Clojure].
+
+Installation
+============
+
+Add the following to your **project.clj** or pom.xml:
+
+Lein artifact:
+
+ [ruiyun/tools.timer "1.0.0-SNAPSHOT"]
+
+Maven:
+
+ <dependency>
+ <groupId>ruiyun</groupId>
+ <artifactId>tools.timer</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </dependency>
+
+Then execute
+
+ lein deps
+
+And here is an example to show how to work with ruiyun.tools.timer.
+
+```clojure
+ (use 'ruiyun.tools.timer)
+ TBD..
+```
+
+Documentation
+=============
+
+For more detailed information on **ruiyun.tools.timer**, please refer to the [documentation].
+
+License
+=======
+
+Copyright (C) 2012 Ruiyun Wen
+
+Distributed under the Eclipse Public License, the same as Clojure.
+
+[Timer]: http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
+[TimerTask]: http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html
+[Clojure]: http://clojure.org/
+[documentation]: http://ruiyun.github.com/tools.timer/
@@ -0,0 +1,5 @@
+(defproject ruiyun/tools.timer "1.0.0-SNAPSHOT"
+ :description "An easy to use Java Timer wrapper for clojure."
+ :dependencies [[org.clojure/clojure "1.4.0"]]
+ :dev-dependencies [[lein-autodoc "0.9.0"]]
+ :autodoc {:name "ruiyun.tools.timer", :page-title "ruiyun.tools.timer API Documentation"})
@@ -0,0 +1,72 @@
+(ns ^{:doc "place doc string here"
+ :author "ruiyun"}
+ ruiyun.tools.timer.core
+ (:use cljain.tools.predicate)
+ (:import [java.util Timer TimerTask Date]))
+
+(defn timer
+ "Create a new java.util.Timer object."
+ {:added "0.2.0"}
+ ([] (Timer.))
+ ([name] (Timer. name)))
+
+(defn deamon-timer
+ "Create a new java.util.Timer object with deamon option."
+ {:added "0.2.0"}
+ []
+ (Timer. true))
+
+(defmacro task
+ "Create a java.util.TimerTask object with some code."
+ {:arglists '([body*])
+ :added "0.2.0"}
+ [& body]
+ `(proxy [TimerTask] []
+ (run []
+ ~@body)))
+
+(defmulti run!
+ "Execute a timer task, then return the timer user passed or created auto.
+ User must set one of the two options:
+ :at <time>
+ :delay <milliseconds>
+
+ Optional, user can set
+ :period <milliseconds>"
+ {:arglists '([timer? task & options])
+ :added "0.2.0"}
+ (fn [a & more] (class a)))
+
+(defn- run-task!
+ "The 'run!' functions internal implement."
+ {:added "0.2.0"}
+ [timer task & {:keys [at delay period]}]
+ {:pre [(or (instance? Date at) (>= delay 0))
+ (or (nil? period) (> period 0))]
+ :post [(instance? Timer %)]}
+ (let [start-time at]
+ (cond
+ (not (nil? start-time))
+ (if (nil? period)
+ (.schedule timer task start-time)
+ (.schedule timer task start-time period))
+
+ (not (nil? delay))
+ (if (nil? period)
+ (.schedule timer task delay)
+ (.schedule timer task delay period)))
+ timer))
+
+(defmethod run! Timer
+ [timer task & options]
+ (apply run-task! timer task options))
+
+(defmethod run! TimerTask
+ [task & options]
+ (apply run-task! (timer) task options))
+
+(defn cancel!
+ "Terminates a timer, discarding any currently scheduled tasks."
+ {:added "0.2.0"}
+ [timer]
+ (.cancel timer))
@@ -0,0 +1,6 @@
+(ns ruiyun.tools.timer.test.core
+ (:use [ruiyun.tools.timer.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit f7c6705

Please sign in to comment.