1 package hudson.model; 2 3 import java.io.File ; 4 import java.io.FilenameFilter ; 5 import java.io.IOException ; 6 import java.util.AbstractMap ; 7 import java.util.Collections ; 8 import java.util.Comparator ; 9 import java.util.Map ; 10 import java.util.Set ; 11 import java.util.SortedMap ; 12 import java.util.TreeMap ; 13 14 24 public final class RunMap<R extends Run<?,R>> extends AbstractMap <Integer ,R> implements SortedMap <Integer ,R> { 25 private transient volatile SortedMap <Integer ,R> builds = 27 new TreeMap <Integer ,R>(COMPARATOR); 28 29 32 private final SortedMap <Integer ,R> view = Collections.unmodifiableSortedMap(this); 33 34 public Set <Entry<Integer ,R>> entrySet() { 35 return Collections.unmodifiableSet(builds.entrySet()); 37 } 38 39 public synchronized R put(R value) { 40 return put(value.getNumber(),value); 41 } 42 43 public synchronized R put(Integer key, R value) { 44 TreeMap <Integer ,R> m = new TreeMap <Integer ,R>(builds); 46 47 R r = update(m, key, value); 48 49 this.builds = m; 50 return r; 51 } 52 53 public synchronized void putAll(Map <? extends Integer ,? extends R> rhs) { 54 TreeMap <Integer ,R> m = new TreeMap <Integer ,R>(builds); 56 57 for (Map.Entry <? extends Integer ,? extends R> e : rhs.entrySet()) 58 update(m, e.getKey(), e.getValue()); 59 60 this.builds = m; 61 } 62 63 private R update(TreeMap <Integer , R> m, Integer key, R value) { 64 R first = m.isEmpty() ? null : m.get(m.firstKey()); 67 R r = m.put(key, value); 68 SortedMap <Integer ,R> head = m.headMap(key); 69 if(!head.isEmpty()) { 70 R prev = m.get(head.lastKey()); 71 value.previousBuild = prev.previousBuild; 72 value.nextBuild = prev; 73 if(value.previousBuild!=null) 74 value.previousBuild.nextBuild = value; 75 prev.previousBuild=value; 76 } else { 77 value.previousBuild = first; 78 value.nextBuild = null; 79 if(first!=null) 80 first.nextBuild = value; 81 } 82 return r; 83 } 84 85 public synchronized boolean remove(R run) { 86 if(run.nextBuild!=null) 87 run.nextBuild.previousBuild = run.previousBuild; 88 if(run.previousBuild!=null) 89 run.previousBuild.nextBuild = run.nextBuild; 90 91 TreeMap <Integer ,R> m = new TreeMap <Integer ,R>(builds); 93 R r = m.remove(run.getNumber()); 94 this.builds = m; 95 96 return r!=null; 97 } 98 99 public synchronized void reset(TreeMap <Integer ,R> builds) { 100 this.builds = new TreeMap <Integer ,R>(COMPARATOR); 101 putAll(builds); 102 } 103 104 107 public SortedMap <Integer ,R> getView() { 108 return view; 109 } 110 111 public Comparator <? super Integer > comparator() { 115 return builds.comparator(); 116 } 117 118 public SortedMap <Integer , R> subMap(Integer fromKey, Integer toKey) { 119 return builds.subMap(fromKey, toKey); 120 } 121 122 public SortedMap <Integer , R> headMap(Integer toKey) { 123 return builds.headMap(toKey); 124 } 125 126 public SortedMap <Integer , R> tailMap(Integer fromKey) { 127 return builds.tailMap(fromKey); 128 } 129 130 public Integer firstKey() { 131 return builds.firstKey(); 132 } 133 134 public Integer lastKey() { 135 return builds.lastKey(); 136 } 137 138 public static final Comparator <Comparable > COMPARATOR = new Comparator <Comparable >() { 139 public int compare(Comparable o1, Comparable o2) { 140 return -o1.compareTo(o2); 141 } 142 }; 143 144 147 public interface Constructor<R extends Run<?,R>> { 148 R create(File dir) throws IOException ; 149 } 150 151 159 public synchronized void load(Job job, Constructor<R> cons) { 160 TreeMap <Integer ,R> builds = new TreeMap <Integer ,R>(RunMap.COMPARATOR); 161 File buildDir = job.getBuildDir(); 162 buildDir.mkdirs(); 163 String [] buildDirs = buildDir.list(new FilenameFilter () { 164 public boolean accept(File dir, String name) { 165 return new File (dir,name).isDirectory(); 166 } 167 }); 168 169 for( String build : buildDirs ) { 170 File d = new File (buildDir,build); 171 if(new File (d,"build.xml").exists()) { 172 try { 174 R b = cons.create(d); 175 builds.put( b.getNumber(), b ); 176 } catch (IOException e) { 177 e.printStackTrace(); 178 } 179 } 180 } 181 182 reset(builds); 183 } 184 } 185 | Popular Tags |