]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/DiskWarningSystem.java
StructuralUtils.isImmutable now takes L0.readOnly of input into account
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / DiskWarningSystem.java
1 package org.simantics;
2
3 import java.util.Collection;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.ScheduledFuture;
6 import java.util.concurrent.TimeUnit;
7
8 /**
9  * @author Antti Villberg
10  */
11 public class DiskWarningSystem {
12
13     public interface DiskWarningListener {
14         /*
15          * @param available in bytes
16          */
17         void diskLow(long available);
18     }
19
20     private final Collection<DiskWarningListener> listeners = new CopyOnWriteArrayList<DiskWarningListener>();
21
22     // In bytes
23     private long limit = Long.MAX_VALUE;
24
25     private boolean disposed = false;
26
27     private ScheduledFuture<?> future;
28
29     /*
30      * @param limit in megabytes
31      */
32     public void setLimit(long limit) {
33         this.limit = limit*1024*1024;
34     }
35
36     public long get() {
37         return Simantics.getDiskBytes();
38     }
39     
40     public boolean check() {
41                 return get() > limit;
42     }
43         
44     
45     public DiskWarningSystem(int amount, TimeUnit unit) {
46         
47         future = Simantics.scheduleAtFixedRate(new Runnable() {
48
49                 @Override
50                 public void run() {
51                         if(!disposed && !check()) {
52                                 long bytes = get();
53                                 for (DiskWarningListener listener : listeners) {
54                                         listener.diskLow(bytes);
55                                 }
56                         }
57                 }
58
59         }, 0, amount, unit);
60         
61     }
62
63     public boolean addListener(DiskWarningListener listener) {
64         return listeners.add(listener);
65     }
66
67     public boolean removeListener(DiskWarningListener listener) {
68         return listeners.remove(listener);
69     }
70
71     public void dispose() {
72         if (!disposed) {
73             disposed = true;
74             future.cancel(false);
75             listeners.clear();
76         }
77     }
78
79 }