/*
 * Copyright (c) 2014-2015 Neil Ellis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sillelien.dollar.api.time;

import com.sillelien.dollar.api.Pipeable;
import com.sillelien.dollar.api.types.DollarFactory;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;
import java.util.concurrent.*;

import static com.sillelien.dollar.api.DollarStatic.$;

public class Scheduler {

    private static final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(3);
    private static final ConcurrentHashMap<String, ScheduledFuture> scheduledTasks = new ConcurrentHashMap<>();

    @NotNull public static String schedule(@NotNull Pipeable task, long duration) {

        String id = UUID.randomUUID().toString();
        scheduledTasks.put(id, scheduler.scheduleAtFixedRate(() -> {
            try {
                task.pipe($(id));
            } catch (Exception e) {
                e.printStackTrace();
                DollarFactory.failure(e);
            }
        }, duration, duration, TimeUnit.MILLISECONDS));
        return id;
    }

    public static void cancel(@NotNull String id) {
        ScheduledFuture scheduledFuture = scheduledTasks.get(id);
        if (scheduledFuture != null) {
            scheduledFuture.cancel(false);
        }
    }
}

@@ -1,13 +1,16 @@
 /*%0A *
+   
  Copyrig
@@ -28,9 +28,9 @@
 -201
-5
+7
  Nei
@@ -42,16 +42,19 @@
 is%0A *%0A *
+   
  License
@@ -112,16 +112,19 @@
 se%22);%0A *
+   
  you may
@@ -183,16 +183,19 @@
 ense.%0A *
+   
  You may
@@ -228,24 +228,27 @@
 nse at%0A *%0A *
+   
        http:
@@ -290,16 +290,19 @@
 .0%0A *%0A *
+   
  Unless 
@@ -364,16 +364,19 @@
 tware%0A *
+   
  distrib
@@ -436,16 +436,19 @@
 ASIS,%0A *
+   
  WITHOUT
@@ -515,16 +515,19 @@
 lied.%0A *
+   
  See the
@@ -589,16 +589,19 @@
 s and%0A *
+   
  limitat
@@ -747,306 +747,902 @@
 api.
-types.DollarFactory;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.UUID;%0Aimport java.util.concurrent.*;%0A%0Aimport static com.sillelien.dollar.api.DollarStatic.$;%0A%0Apublic class Scheduler %7B%0A%0A    private static final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(3);
+execution.DollarExecutor;%0Aimport com.sillelien.dollar.api.plugin.Plugins;%0Aimport com.sillelien.dollar.api.types.DollarFactory;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.util.UUID;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.ScheduledThreadPoolExecutor;%0A%0Aimport static com.sillelien.dollar.api.DollarStatic.$;%0A%0Apublic class Scheduler %7B%0A%0A    @NotNull%0A    private static final Logger log = LoggerFactory.getLogger(%22Scheduler%22);%0A%0A    @NotNull%0A    private static final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(3);%0A    @Nullable%0A    private static final DollarExecutor executor = Plugins.sharedInstance(DollarExecutor.class);%0A    @NotNull
 %0A   
@@ -1689,25 +1689,16 @@
 String, 
-Scheduled
 Future%3E 
@@ -1754,16 +1754,20 @@
 @NotNull
+%0A   
  public 
@@ -1912,24 +1912,23 @@
 put(id, 
-schedule
+executo
 r.schedu
@@ -1933,20 +1933,23 @@
 dule
-AtFixedRate(
+Every(duration,
 () -
@@ -1970,16 +1970,57 @@
   try %7B%0A
+                assert executor != null;%0A
         
@@ -2101,26 +2101,34 @@
     
-e.printStackTrace(
+log.debug(e.getMessage(),e
 );%0A 
@@ -2195,51 +2195,8 @@
    %7D
-, duration, duration, TimeUnit.MILLISECONDS
 ));%0A
@@ -2285,34 +2285,16 @@
     
-ScheduledFuture scheduledF
+Future f
 utur
@@ -2333,26 +2333,17 @@
     if (
-scheduledF
+f
 uture !=
@@ -2367,18 +2367,9 @@
     
-scheduledF
+f
 utur

Work on new scope system.