zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package kernel.util;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@SuppressWarnings("unchecked")
public class TimeWindow implements Runnable {
    
    private static final Logger logger = LoggerFactory.getLogger(TimeWindow.class);
 
    private TimeWindow instance = null;
 
    private boolean stop = false;
 
    protected int timeSize = 600; // 600s
 
    protected HashMap[] cache = null;
 
    protected byte[] cacheLock = new byte[1];
 
    private String name = "TimeWindow";
 
    private void init() {
        cache = new HashMap[timeSize];
        for (int i = 0; i < cache.length; i++) {
            cache[i] = new HashMap();
        }
    }
 
    public void start() {
        if (instance == null) {
            instance = this;
            this.init();
            if (name == null || name.trim().length() == 0)
                name = "TimeWindow";
            new Thread(this, name).start();
        }
    }
 
    public void stop() {
        if (instance != null) {
            instance = null;
            cache = null;
            this.setStop(true);
        }
    }
 
    public void run() {
        while (!stop) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
 
            synchronized (cacheLock) { // 030316 cacheLock
                if (cache != null) {
                    for (int i = 0; i < timeSize - 1; i++) {
                        cache[i] = cache[i + 1];
                    }
 
                    HashMap m = new HashMap();
                    cache[timeSize - 1] = m;
                }
            }
        }
 
        logger.warn("Time window stop.");
    }
 
    public int find(Object key) {
        synchronized (cacheLock) {
            for (int i = 0; cache != null && i < timeSize; i++) {
                HashMap m = (HashMap) cache[i];
                if (m.containsKey(key))
                    return i;
            }
        }
 
        return -1;
    }
 
    public Object findObject(Object key) {
        synchronized (cacheLock) {
            for (int i = 0; cache != null && i < timeSize; i++) {
                HashMap m = (HashMap) cache[i];
                if (m.containsKey(key)) {
                    Object object = m.get(key);
                    return object;
                }
            }
        }
        return null;
    }
 
    public Map getObjectAll() {
        HashMap map = new HashMap();
        synchronized (cacheLock) {
            for (int i = 0; cache != null && i < timeSize; i++) {
                HashMap m = (HashMap) cache[i];
                map.putAll(m);
            }
        }
 
        return map;
    }
 
    public boolean add(Object key, Object o) {
        this.remove(key);
        return add(key, o, new Date());
    }
 
    public void remove(Object key) {
        synchronized (cacheLock) {
            for (int i = 0; cache != null && i < timeSize; i++) {
                HashMap m = (HashMap) cache[i];
                if (m.containsKey(key)) {
                    m.remove(key);
                }
            }
        }
    }
 
    public void remove(Object key, int index) {
        synchronized (cacheLock) {
            if (cache != null)
                cache[index].remove(key);
        }
    }
 
    public boolean remove(Object key, Date d) {
 
        if (d == null)
            d = new Date();
 
        int index = (int) ((d.getTime() / 1000) % timeSize);
 
        synchronized (cacheLock) {
            if (cache != null)
                cache[index].remove(key);
        }
 
        return true;
 
    }
 
    public boolean add(Object key, Object o, Date d) {
        if (d == null)
            d = new Date();
        // 随机
        // int index = (int) ((d.getTime() / 1000) % timeSize);
        
        int index = timeSize - 1;
        if(d.before(new Date())) {//d<当前时间
            index = index - (int) ((new Date().getTime()-d.getTime() )/ 1000);
            if(index<=0) return false;
        }
        synchronized (cacheLock) {
            if (cache != null)
                cache[index].put(key, o);
        }
 
        return true;
    }
 
    public boolean isStop() {
        return stop;
    }
 
    public void setStop(boolean stop) {
        this.stop = stop;
    }
 
    public int getTimeSize() {
        return timeSize;
    }
 
    public void setTimeSize(int timeSize) {
        this.timeSize = timeSize;
    }
 
    public int size() {
        int size = 0;
 
        synchronized (cacheLock) {
            for (int i = 0; cache != null && i < timeSize; i++) {
                HashMap m = cache[i];
                if (m != null)
                    size += m.size();
            }
        }
 
        return size;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}