/*
 *
 *  Copyright 2012 Netflix, Inc.
 *
 *     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.netflix.simianarmy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Monkey {
    private static final Logger LOGGER = LoggerFactory.getLogger(Monkey.class);

    public interface Context {
        MonkeyScheduler scheduler();

        MonkeyCalendar calendar();

        CloudClient cloudClient();

        MonkeyRecorder recorder();
    }

    private Context ctx;

    public Monkey(Context ctx) {
        this.ctx = ctx;
    }

    public abstract Enum type();

    public abstract void doMonkeyBusiness();

    public Context context() {
        return ctx;
    }

    public void start() {
        final Monkey me = this;
        ctx.scheduler().start(type().name(), new Runnable() {
            public void run() {
                try {
                    if (ctx.calendar().isMonkeyTime(me)) {
                        me.doMonkeyBusiness();
                    } else {
                        LOGGER.info("Not Time for " + me.type().name() + " Monkey");
                    }
                } catch (Exception e) {
                    LOGGER.error(me.type().name() + " Monkey Error: ", e);
                }
            }
        });
    }

    public void stop() {
        ctx.scheduler().stop(type().name());
    }

}

@@ -1263,21 +1263,19 @@
 ic void 
-start
+run
 () %7B%0A   
@@ -1283,102 +1283,195 @@
     
-final Monkey me = this;%0A        ctx.scheduler().start(type().name(), new Runnable() %7B%0A
+if (ctx.calendar().isMonkeyTime(this)) %7B%0A            this.doMonkeyBusiness();%0A        %7D else %7B%0A            LOGGER.info(%22Not Time for %22 + this.type().name() + %22 Monkey%22);%0A 
        
+%7D%0A
  
+   %7D%0A%0A
     
@@ -1474,35 +1474,37 @@
     public void 
-run
+start
 () %7B%0A           
@@ -1504,127 +1504,125 @@
     
-        try %7B%0A                    if (ctx.calendar().isMonkeyTime(me)) %7B%0A                        me.doMonkeyBusiness();
+final Monkey me = this;%0A        ctx.scheduler().start(type().name(), new Runnable() %7B%0A            public void run() %7B
 %0A   
@@ -1630,34 +1630,27 @@
             
-    %7D else
+try
  %7B%0A         
@@ -1664,94 +1664,17 @@
     
-    LOGGER.info(%22Not Time for %22 + me.type().name() + %22 Monkey%22);%0A                    %7D
+me.run();
 %0A   

split Monkey.start adding Monkey.run for subclassing