package com.alibaba.mtc;

import java.util.Map;
import java.util.TimerTask;

/**
 * {@link MtContextTimerTask} decorate {@link Runnable}, so as to get @{@link MtContext}
 * and transmit it to the time of {@link Runnable} execution, needed when use {@link Runnable} to thread pool.
 * <p/>
 * Use factory method {@link #get(TimerTask)} to create instance.
 * <p/>
 * Use {@link java.util.concurrent.ScheduledThreadPoolExecutor} instead {@link java.util.Timer}
 *
 * @author ding.lid
 * @see java.util.Timer
 * @see TimerTask
 * @since 0.9.0
 */
@Deprecated
public final class MtContextTimerTask extends TimerTask {
    private final Map<String, Object> content;
    private final TimerTask timerTask;

    private MtContextTimerTask(TimerTask timerTask) {
        content = MtContext.getContext().getWithCopy();
        this.timerTask = timerTask;
    }

    /**
     * wrap method {@link TimerTask#run()}.
     */
    @Override
    public void run() {
        MtContext mtContext = MtContext.getContext();
        final Map<String, Object> old = mtContext.get0();
        try {
            mtContext.set0(content);
            timerTask.run();
        } finally {
            mtContext.set0(old); // restore MtContext
        }
    }

    @Override
    public boolean cancel() {
        return timerTask.cancel();
    }

    @Override
    public long scheduledExecutionTime() {
        return timerTask.scheduledExecutionTime();
    }

    public TimerTask getTimerTask() {
        return timerTask;
    }

    /**
     * Factory method, wrapper input {@link Runnable} to {@link MtContextTimerTask}.
     * <p/>
     * This method is idempotent.
     *
     * @param timerTask input {@link TimerTask}
     * @return Wrapped {@link TimerTask}
     */
    public static MtContextTimerTask get(TimerTask timerTask) {
        if (null == timerTask) {
            throw new NullPointerException("runnable argument is null!");
        }

        if (timerTask instanceof MtContextTimerTask) { // avoid redundant decoration, and ensure idempotency
            return (MtContextTimerTask) timerTask;
        }
        return new MtContextTimerTask(timerTask);
    }
}

@@ -120,24 +120,25 @@
  %7B@link 
-Runnable
+TimerTask
 %7D, so as
@@ -452,16 +452,193 @@
 l.Timer%7D
+.%0A * %3Cp/%3E%0A * %3Cb%3ENOTE:%3C/b%3E%0A * The %7B@link MtContextTimerTask%7D make the the method %7B@link TimerTask#scheduledExecutionTime()%7D of%0A * the origin %7B@link TimerTask%7D lose effectiveness!
 %0A *%0A * @
@@ -709,17 +709,17 @@
 nce 0.9.
-0
+1
 %0A */%0A@De
@@ -1450,39 +1450,32 @@
 cel() %7B%0A        
-return 
 timerTask.cancel
@@ -1486,115 +1486,31 @@
     
-%7D%0A%0A
     
-@Override%0A    public long scheduledExecutionTime() %7B%0A        return timerTask.scheduledExecutionTime
+return super.cancel
 ();%0A

improve javadoc, remove method scheduledExecutionTime