<NME> VariableTimer.swift
<BEF> //
//  VariableTimer.swift
//  Chronos
//
//  Copyright (c) 2015 Andrew Chun, Comyar Zaheri. 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.


// MARK:- Imports

import Foundation


// MARK:- Constants and Functions

private let queuePrefix = "com.chronos.variableTimer"


// MARK:- VariableTimer Implementation

/**
A VariableTimer allows you to create a Grand Central Dispatch-based timer object
that allows for variable time intervals between each firing. Each successive
time interval is obtained by executing the given interval closure.

A timer has limited accuracy when determining the exact moment to fire; the
actual time at which a timer fires can potentially be a significant period of
time after the scheduled firing time. However, successive fires are guarenteed
to occur in order.
*/
@objc
@availability (iOS, introduced=8.0)
@availability (OSX, introduced=10.10)
public class VariableTimer : NSObject, RepeatingTimer {
    private var valid                   = State.invalid
    private var running                 = State.paused
    fileprivate var running                 = State.paused
    fileprivate var isExecuting             = false
    fileprivate var shouldFireImmediately   = false
    fileprivate var timer: DispatchSourceTimer
    
    // MARK: Type Aliases
    /**
    The closure to execute the next interval length.
    
    :param: timer   The timer that fired.
    :param: count   The next invocation count. The first count is 0.
    */
    public typealias IntervalClosure = ((timer: VariableTimer, count: Int) -> Double)
    
    
    // MARK: Properties
    
    /**
    The timer's execution queue.
    */
    open let queue: DispatchQueue!
    
    /**
    The timer's execution closure.
    */
    open let closure: ExecutionClosure!
    
    /**
    The timer's interval closure.
    */
    open let intervalProvider: IntervalClosure!
    
    /**
    The number of times the execution closure has been executed.
    */
    open fileprivate(set) var count: Int = 0
    
    /**
    true, if the timer is valid; otherwise, false.
    
    A timer is considered valid if it has not been canceled.
    */
    open var isValid: Bool {
        return valid == State.valid
    }
    
    /**
    true, if the timer is currently running; otherwise, false.
    */
    open var isRunning: Bool {
        return running == State.running
    }
    
    // MARK: NSObject
    
    override fileprivate init() { fatalError("Cannot create timer with init.") }
    deinit { cancel() }
    
    // MARK: Creating a Variable Timer
    /**
    Creates a VariableTimer object.
    
    :param: executionClosure    The closure to execute at a variable interval.
    :param: intervalClosure     The closure to execute to obtain a variable
    interval.
    
    :returns: A newly created VariableTimer object.
    */
    convenience public init(closure: ExecutionClosure, intervalProvider: IntervalClosure) {
        let name    = "\(queuePrefix).\(NSUUID().UUIDString)"
        let name    = "\(queuePrefix).\(UUID().uuidString)"
        let queue   = DispatchQueue(label: name, attributes: [])
        self.init(closure: closure, intervalProvider: intervalProvider, queue: queue)
    }
    /**
    Creates a VariableTimer object.
    
    :param: executionClosure    The closure to execute at a variable interval.
    :param: intervalClosure     The closure that provides time intervals.
    :param: queue               The queue that should execute the given closure.
    
    :returns: A newly created VariableTimer object.
    */
    convenience public init(closure: ExecutionClosure, intervalProvider: IntervalClosure, queue: dispatch_queue_t) {
        self.init(closure: closure, intervalProvider: intervalProvider, queue: queue, failureClosure: nil)
      self.timer = DispatchSource.makeTimerSource(queue: queue)
      self.valid = State.valid
    /**
    Creates a VariableTimer object.
    
    :param: executionClosure    The closure to execute at a variable interval.
    :param: intervalClosure     The closure that provides time intervals.
    :param: queue               The queue that should execute the given closure.
    :param: failureClosure      The closure to execute if creation fails.
    
    :returns: A newly created VariableTimer object.
    */
    public init(closure: ExecutionClosure, intervalProvider: IntervalClosure, queue: dispatch_queue_t, failureClosure: FailureClosure) {
        if let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue) {
              strongSelf.closure(strongSelf, strongSelf.count)
              strongSelf.count += 1
        } else if let failureClosure = failureClosure {
            failureClosure()
        } else {
            println("Failed to create dispatch source for timer.")
        }
        
        self.queue              = queue
    
    /**
    Schedules the next execution closure
    
    - parameter now: true, if the execution closure should be scheduled immediately;
    false, otherwise
    */
    func schedule(_ now: Bool) {
        if isValid {
          let interval: Double = intervalProvider(self, count)
          timer.scheduleRepeating(deadline: startTime(interval, now: now), interval: DispatchTimeInterval.nanoseconds(Int(interval.multiplied(by: Double(NSEC_PER_SEC)))))
        }
    }
    
    // MARK: Using a Variable Timer
    
    /**
    Starts the timer.
    
    - parameter now:     true, if the timer should fire immediately.
    */
    /**
    Schedules the next execution closure
    
    :param: now true, if the execution closure should be scheduled immediately;
    false, otherwise
    */
    func schedule(now: Bool) {
        if isValid {
            let interval = intervalProvider(timer: self, count: count)
            dispatch_source_set_timer(timer, startTime(interval, now), UInt64(interval * Double(NSEC_PER_SEC)), 0)
        }
    }
    
    /**
    Pauses the timer and does not reset the count.
    /**
    Starts the timer.
    
    :param: now     true, if the timer should fire immediately.
    */
    public func start(now: Bool) {
        validate()
    
    /**
    Permanently cancels the timer.
    
    Attempting to start or pause an invalid timer is considered an error and
    will throw an exception.
    */
    open func cancel() {
        if OSAtomicCompareAndSwap32Barrier(State.valid, State.invalid, &valid) {
          if running == State.paused {
            timer.resume()
          }
          
          running = State.paused
          timer.cancel()
        }
    }
    
    fileprivate func validate() {
        if valid != State.valid {
            NSException(name: NSExceptionName.internalInconsistencyException,
                reason: "Attempting to use invalid DispatchTimer",
                userInfo: nil).raise()
        }
    }
}

<MSG> Auto convert by Xcode7

<DFF> @@ -46,8 +46,8 @@ time after the scheduled firing time. However, successive fires are guarenteed
 to occur in order.
 */
 @objc
-@availability (iOS, introduced=8.0)
-@availability (OSX, introduced=10.10)
+@available (iOS, introduced=8.0)
+@available (OSX, introduced=10.10)
 public class VariableTimer : NSObject, RepeatingTimer {
     private var valid                   = State.invalid
     private var running                 = State.paused
@@ -60,8 +60,8 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     The closure to execute the next interval length.
     
-    :param: timer   The timer that fired.
-    :param: count   The next invocation count. The first count is 0.
+    - parameter timer:   The timer that fired.
+    - parameter count:   The next invocation count. The first count is 0.
     */
     public typealias IntervalClosure = ((timer: VariableTimer, count: Int) -> Double)
     
@@ -113,11 +113,11 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     Creates a VariableTimer object.
     
-    :param: executionClosure    The closure to execute at a variable interval.
-    :param: intervalClosure     The closure to execute to obtain a variable
+    - parameter executionClosure:    The closure to execute at a variable interval.
+    - parameter intervalClosure:     The closure to execute to obtain a variable
     interval.
     
-    :returns: A newly created VariableTimer object.
+    - returns: A newly created VariableTimer object.
     */
     convenience public init(closure: ExecutionClosure, intervalProvider: IntervalClosure) {
         let name    = "\(queuePrefix).\(NSUUID().UUIDString)"
@@ -128,11 +128,11 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     Creates a VariableTimer object.
     
-    :param: executionClosure    The closure to execute at a variable interval.
-    :param: intervalClosure     The closure that provides time intervals.
-    :param: queue               The queue that should execute the given closure.
+    - parameter executionClosure:    The closure to execute at a variable interval.
+    - parameter intervalClosure:     The closure that provides time intervals.
+    - parameter queue:               The queue that should execute the given closure.
     
-    :returns: A newly created VariableTimer object.
+    - returns: A newly created VariableTimer object.
     */
     convenience public init(closure: ExecutionClosure, intervalProvider: IntervalClosure, queue: dispatch_queue_t) {
         self.init(closure: closure, intervalProvider: intervalProvider, queue: queue, failureClosure: nil)
@@ -141,12 +141,12 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     Creates a VariableTimer object.
     
-    :param: executionClosure    The closure to execute at a variable interval.
-    :param: intervalClosure     The closure that provides time intervals.
-    :param: queue               The queue that should execute the given closure.
-    :param: failureClosure      The closure to execute if creation fails.
+    - parameter executionClosure:    The closure to execute at a variable interval.
+    - parameter intervalClosure:     The closure that provides time intervals.
+    - parameter queue:               The queue that should execute the given closure.
+    - parameter failureClosure:      The closure to execute if creation fails.
     
-    :returns: A newly created VariableTimer object.
+    - returns: A newly created VariableTimer object.
     */
     public init(closure: ExecutionClosure, intervalProvider: IntervalClosure, queue: dispatch_queue_t, failureClosure: FailureClosure) {
         if let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue) {
@@ -155,7 +155,7 @@ public class VariableTimer : NSObject, RepeatingTimer {
         } else if let failureClosure = failureClosure {
             failureClosure()
         } else {
-            println("Failed to create dispatch source for timer.")
+            print("Failed to create dispatch source for timer.")
         }
         
         self.queue              = queue
@@ -183,13 +183,13 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     Schedules the next execution closure
     
-    :param: now true, if the execution closure should be scheduled immediately;
+    - parameter now: true, if the execution closure should be scheduled immediately;
     false, otherwise
     */
     func schedule(now: Bool) {
         if isValid {
             let interval = intervalProvider(timer: self, count: count)
-            dispatch_source_set_timer(timer, startTime(interval, now), UInt64(interval * Double(NSEC_PER_SEC)), 0)
+            dispatch_source_set_timer(timer, startTime(interval, now: now), UInt64(interval * Double(NSEC_PER_SEC)), 0)
         }
     }
     
@@ -198,7 +198,7 @@ public class VariableTimer : NSObject, RepeatingTimer {
     /**
     Starts the timer.
     
-    :param: now     true, if the timer should fire immediately.
+    - parameter now:     true, if the timer should fire immediately.
     */
     public func start(now: Bool) {
         validate()

Auto convert by Xcode7