package com.cookpad.android.loghouse;

import android.os.Handler;

public class CuckooClock {
    private final String TAG = CuckooClock.class.getSimpleName();

    private Handler handler;
    private boolean hasAlreadySet;
    private Runnable callback;
    private int callMeAfter;
    private int retryCount = 1;
    private int buckOffTime;

    public CuckooClock(final OnAlarmListener onAlarmListener, int callMeAfter) {
        handler = new Handler();
        this.callMeAfter = callMeAfter;
        hasAlreadySet = false;

        callback = new Runnable() {
            @Override
            public void run() {
                hasAlreadySet = false;
                retryCount = 1;
                onAlarmListener.onAlarm();
            }
        };
    }

    public interface OnAlarmListener {
        public void onAlarm();
    }

    public synchronized void setAlarm() {
        if (hasAlreadySet && retryCount == 1) {
            return;
        }
        retryCount = 1;
        forceSetAlarm();
    }

    private synchronized void forceSetAlarm() {
        buckOffTime = callMeAfter * retryCount;
        handler.removeCallbacks(callback);
        handler.postDelayed(callback, buckOffTime);
        hasAlreadySet = true;
    }

    public synchronized void retryLater() {
        retryCount++;
        forceSetAlarm();
    }
}

@@ -91,75 +91,8 @@
 k %7B%0A
-    private final String TAG = CuckooClock.class.getSimpleName();%0A%0A
     
@@ -198,27 +198,24 @@
 ate int 
-callMeAfter
+interval
 ;%0A    pr
@@ -243,37 +243,8 @@
 = 1;
-%0A    private int buckOffTime;
 %0A%0A  
@@ -307,27 +307,24 @@
 er, int 
-callMeAfter
+interval
 ) %7B%0A    
@@ -369,33 +369,27 @@
 his.
-callMeAfter = callMeAfter
+interval = interval
 ;%0A  
@@ -967,89 +967,91 @@
     
-buckOffTime = callMeAfter * retryCount;%0A        handler.removeCallbacks(callback)
+handler.removeCallbacks(callback);%0A%0A        int buckOffTime = interval * retryCount
 ;%0A  
@@ -1100,16 +1100,17 @@
 fTime);%0A
+%0A
         

Refactor CuckooClock