Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 452acc6582b805c9b33210d2ec8589f1602a987c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.structures;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.linuxtools.systemtap.structures.listeners.IUpdateListener;



public class UpdateManager {
    public UpdateManager(int delay) {
        updateListeners = new ArrayList<>();
        stopped = false;
        disposed = false;
        restart(delay);
    }

    /**
     * Restart with the given delay.
     * @param delay The milliseconds to delay execution.
     * @since 3.0
     */
    public void restart(int delay) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer("Update Manager", true); //$NON-NLS-1$
        timer.scheduleAtFixedRate(new Notify(), delay, delay);
    }

    /**
     * Terminates the timer and removes all update listeners.
     */
    public void stop() {
        if(!stopped) {
            stopped = true;
            timer.cancel();
            synchronized (updateListeners) {
                for(int i=0; i<updateListeners.size(); i++) {
                    removeUpdateListener(updateListeners.get(i));
                }
            }
        }
    }

    public void addUpdateListener(IUpdateListener l) {
        if(!updateListeners.contains(l)) {
            updateListeners.add(l);
        }
    }
    public void removeUpdateListener(IUpdateListener l) {
        if(updateListeners.contains(l)) {
            updateListeners.remove(l);
        }
    }

    public boolean isRunning() {
        return !stopped;
    }

    public void dispose() {
        if(!disposed) {
            disposed = true;
            stop();
            timer = null;
            updateListeners = null;
        }
    }

    /**
     * Handle any events that are timed to occur.
     */
    private class Notify extends TimerTask {
        @Override
        public void run() {
            if(!stopped) {
                synchronized (updateListeners) {
                    for(int i = 0; i < updateListeners.size(); i++) {
                        (updateListeners.get(i)).handleUpdateEvent();
                    }
                }
            }
        }
    }

    private Timer timer;
    private List<IUpdateListener> updateListeners;
    private boolean stopped;
    private boolean disposed;
}

Back to the top