KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > RunList


1 package hudson.util;
2
3 import hudson.model.Job;
4 import hudson.model.Result;
5 import hudson.model.Run;
6 import hudson.model.View;
7 import hudson.model.Item;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Calendar JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.GregorianCalendar JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * {@link List} of {@link Run}s.
19  *
20  * @author Kohsuke Kawaguchi
21  */

22 public class RunList extends ArrayList JavaDoc<Run> {
23
24     public RunList() {
25     }
26
27     public RunList(Job<?,?> j) {
28         addAll(j.getBuilds());
29     }
30
31     public RunList(View view) {
32         for (Item item : view.getItems())
33             for (Job<?,?> j : item.getAllJobs())
34                 addAll(j.getBuilds());
35         Collections.sort(this,Run.ORDER_BY_DATE);
36     }
37
38     public RunList(Collection JavaDoc<? extends Job> jobs) {
39         for (Job<?,?> j : jobs)
40             addAll(j.getBuilds());
41         Collections.sort(this,Run.ORDER_BY_DATE);
42     }
43
44     public static RunList fromRuns(Collection JavaDoc<? extends Run> runs) {
45         RunList r = new RunList();
46         r.addAll(runs);
47         return r;
48     }
49
50     /**
51      * Filter the list to non-successful builds only.
52      */

53     public RunList failureOnly() {
54         for (Iterator JavaDoc<Run> itr = iterator(); itr.hasNext();) {
55             Run r = itr.next();
56             if(r.getResult()==Result.SUCCESS)
57                 itr.remove();
58         }
59         return this;
60     }
61
62     /**
63      * Filter the list to regression builds only.
64      */

65     public RunList regressionOnly() {
66         for (Iterator JavaDoc<Run> itr = iterator(); itr.hasNext();) {
67             Run r = itr.next();
68             if(!r.getBuildStatusSummary().isWorse)
69                 itr.remove();
70         }
71         return this;
72     }
73
74     /**
75      * Reduce the size of the list by only leaving relatively new ones.
76      * This also removed on-going builds, as RSS cannot be used to publish information
77      * if it changes.
78      */

79     public RunList newBuilds() {
80         GregorianCalendar JavaDoc threshold = new GregorianCalendar JavaDoc();
81         threshold.add(Calendar.DAY_OF_YEAR,-7);
82
83         int count=0;
84
85         for (Iterator JavaDoc<Run> itr = iterator(); itr.hasNext();) {
86             Run r = itr.next();
87             if(r.isBuilding()) {
88                 // can't publish on-going builds
89
itr.remove();
90                 continue;
91             }
92             // at least put 10 items
93
if(count<10) {
94                 count++;
95                 continue;
96             }
97             // anything older than 7 days will be ignored
98
if(r.getTimestamp().before(threshold))
99                 itr.remove();
100         }
101         return this;
102     }
103 }
104
Popular Tags