3 import java.util.Collection;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.ScheduledFuture;
6 import java.util.concurrent.TimeUnit;
9 * @author Antti Villberg
11 public class DiskWarningSystem {
13 public interface DiskWarningListener {
15 * @param available in bytes
17 void diskLow(long available);
20 private final Collection<DiskWarningListener> listeners = new CopyOnWriteArrayList<DiskWarningListener>();
23 private long limit = Long.MAX_VALUE;
25 private boolean disposed = false;
27 private ScheduledFuture<?> future;
30 * @param limit in megabytes
32 public void setLimit(long limit) {
33 this.limit = limit*1024*1024;
37 return Simantics.getDiskBytes();
40 public boolean check() {
45 public DiskWarningSystem(int amount, TimeUnit unit) {
47 future = Simantics.scheduleAtFixedRate(new Runnable() {
51 if(!disposed && !check()) {
53 for (DiskWarningListener listener : listeners) {
54 listener.diskLow(bytes);
63 public boolean addListener(DiskWarningListener listener) {
64 return listeners.add(listener);
67 public boolean removeListener(DiskWarningListener listener) {
68 return listeners.remove(listener);
71 public void dispose() {