
package com.zygon.htm.core;

import com.google.common.util.concurrent.AbstractScheduledService;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author zygon
 */
public abstract class AbstractScheduledServiceImpl extends AbstractScheduledService {
    
    public static final class Settings {
        
        private long period = 0;
        private TimeUnit timeUnit = TimeUnit.SECONDS;

        public long getPeriod() {
            return period;
        }

        public TimeUnit getTimeUnit() {
            return timeUnit;
        }

        public void setPeriod(long period) {
            this.period = period;
        }

        public void setTimeUnit(TimeUnit timeUnit) {
            this.timeUnit = timeUnit;
        }
    }
    
    private final Scheduler scheduler;
    
    protected AbstractScheduledServiceImpl(Settings settings) {
        super();
        
        if (settings == null) {
            // defaults
            settings = new Settings();
        }
        
        this.scheduler = Scheduler.newFixedRateSchedule(0, settings.getPeriod(), settings.getTimeUnit());
    }
    
    protected abstract void doRun() throws Exception;
    
    @Override
    protected final void runOneIteration() throws Exception {
        try {
            this.doRun();
        } catch (Exception e) {
            // TODO: log?
            e.printStackTrace();
        }
    }
    
    @Override
    protected final Scheduler scheduler() {
        return this.scheduler;
    }
}

@@ -243,21 +243,17 @@
 rvice %7B%0A
-    
 %0A
+
     publ
@@ -285,24 +285,25 @@
 tings %7B%0A
+%0A
         
 %0A       
@@ -286,32 +286,239 @@
 ings %7B%0A%0A        
+public static Settings create(long period, TimeUnit timeUnit) %7B%0A            Settings s = new Settings();%0A            s.setPeriod(period);%0A            s.setTimeUnit(timeUnit);%0A            return s;%0A        %7D%0A
 %0A        private
@@ -935,29 +935,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     private 
@@ -979,21 +979,17 @@
 eduler;%0A
-    
 %0A
+
     prot
@@ -1061,25 +1061,17 @@
 uper();%0A
-        
 %0A
+
         
@@ -1171,17 +1171,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -1276,29 +1276,25 @@
 t());%0A    %7D%0A
-    
 %0A
+
     protecte
@@ -1335,21 +1335,17 @@
 eption;%0A
-    
 %0A
+
     @Ove
@@ -1559,20 +1559,16 @@
 %7D%0A    %7D%0A
-    
 %0A    @Ov

Add static factory method for settings.