Skip to main content
summaryrefslogtreecommitdiffstats
blob: cfe429938d295f3111f1dc2325fd7d92e0550c44 (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
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.markers;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Andrew M. Finkbeiner
 */
public class FileWatchList {

   private class FileWatchItem {
      IFile file;
      List<IMarker> markers;
      long timeUpdated;

      FileWatchItem(IFile file, List<IMarker> markers) {
         this.file = file;
         this.markers = markers;
         timeUpdated = System.currentTimeMillis();
      }
   }

   private final List<FileWatchItem> fileWatchItems;

   public FileWatchList() {
      fileWatchItems = new CopyOnWriteArrayList<>();
   }

   public void put(IFile file, List<IMarker> markers) {
      FileWatchItem item = findWatchItem(file);
      if (item == null) {
         if (isListTooBig()) {
            removeOldestWatchItem();
         }
         fileWatchItems.add(new FileWatchItem(file, markers));
      } else {
         item.markers = markers;
         item.timeUpdated = System.currentTimeMillis();
      }
   }

   private void removeOldestWatchItem() {
      FileWatchItem oldest = null;
      for (FileWatchItem item : fileWatchItems) {
         if (oldest == null) {
            oldest = item;
         } else {
            if (oldest.timeUpdated > item.timeUpdated) {
               oldest = item;
            }
         }
      }
      if (oldest != null) {
         OseeLog.logf(FileWatchList.class, Level.INFO, 
            "Removing markers from [%s] because maximium marker watch list size has been reached.",
            oldest.file.getName());
         fileWatchItems.remove(oldest);
         if (oldest.markers != null) {
            for (IMarker marker : oldest.markers) {
               try {
                  marker.delete();
               } catch (CoreException ex) {
                  OseeLog.log(FileWatchList.class, Level.SEVERE, ex);
               }
            }
         }
      }
   }

   private boolean isListTooBig() {
      return fileWatchItems.size() > 20;
   }

   public List<IMarker> get(IFile file) {
      FileWatchItem item = findWatchItem(file);
      if (item != null) {
         return item.markers;
      } else {
         return null;
      }
   }

   private FileWatchItem findWatchItem(IFile file) {
      for (FileWatchItem item : fileWatchItems) {
         if (item.file.equals(file)) {
            return item;
         }
      }
      return null;
   }

}

Back to the top