KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > WaiverSet


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23
24 package org.hammurapi;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import com.pavelvlasov.review.Signed;
34 import com.pavelvlasov.review.SourceMarker;
35
36 /**
37  * @author Pavel Vlasov
38  * @version $Revision: 1.8 $
39  */

40 public class WaiverSet {
41     /**
42      * Comment for <code>serialVersionUID</code>
43      */

44     private static final long serialVersionUID = 4873150699959326186L;
45     private Map JavaDoc signatureMap=new HashMap JavaDoc();
46     private Collection JavaDoc rejectedRequests=new LinkedList JavaDoc();
47
48     /**
49      * @return Collection of waivers remaining in the set
50      */

51     public Collection JavaDoc getWaivers() {
52         Collection JavaDoc ret=new LinkedList JavaDoc();
53         Iterator JavaDoc it=signatureMap.values().iterator();
54         while (it.hasNext()) {
55             ret.addAll(((Map JavaDoc) it.next()).values());
56         }
57         return ret;
58     }
59     
60     public int size() {
61         int ret=0;
62         Iterator JavaDoc it=signatureMap.values().iterator();
63         while (it.hasNext()) {
64             ret+=((Map JavaDoc) it.next()).size();
65         }
66         return ret;
67     }
68     
69     /**
70      * @return Collection of violations which requested waivers, but
71      * weren't granted ones.
72      */

73     public Collection JavaDoc getRejectedRequests() {
74         return rejectedRequests;
75     }
76
77     /**
78      * Requests and removes a waiver from the set. Waivers with signature==null
79      * aren't removed as they apply to multiple violations.
80      * @param violation
81      * @param peek Just peek for waiver.
82      * @return Waiver if there is one for the violation, null otherwise.
83      */

84     public Waiver requestWaiver(Violation violation, boolean peek) {
85         if (Boolean.TRUE.equals(violation.getDescriptor().isWaivable())) {
86             SourceMarker sm=violation.getSource();
87             String JavaDoc signature= sm instanceof Signed ? ((Signed) sm).getSignature() : null;
88             if (signature!=null) {
89                 Map JavaDoc nameMap=(Map JavaDoc) signatureMap.get(signature);
90                 Waiver ret = nameMap==null ? null : (Waiver) nameMap.get(violation.getDescriptor().getName());
91                                 
92                 if (ret!=null) {
93                     if (!peek) {
94                         nameMap.remove(violation.getDescriptor().getName());
95                     }
96                     
97                     if (ret.waive(violation, peek)) {
98                         return ret;
99                     }
100                 }
101             }
102
103             
104             Map JavaDoc nameMap=(Map JavaDoc) signatureMap.get(null);
105             Waiver ret = nameMap==null ? null : (Waiver) nameMap.get(violation.getDescriptor().getName());
106             boolean waived = ret!=null && ret.waive(violation, peek);
107             
108             if (!waived) {
109                 rejectedRequests.add(violation);
110             }
111             
112             if (ret!=null && !ret.isActive()) {
113                 nameMap.remove(ret.getInspectorName());
114             }
115             
116             return waived ? ret : null;
117         }
118         
119         return null;
120     }
121     
122     public void addWaiver(Waiver waiver, Date JavaDoc now) {
123         if (waiver.getExpirationDate()==null || now.before(waiver.getExpirationDate())) {
124             Collection JavaDoc signatures = waiver.getSignatures();
125             if (signatures!=null && !signatures.isEmpty()) {
126                 Iterator JavaDoc it=signatures.iterator();
127                 while (it.hasNext()) {
128                     String JavaDoc signature=(String JavaDoc) it.next();
129                     getNameMap(signature).put(waiver.getInspectorName(), waiver);
130                 }
131             } else {
132                 getNameMap(null).put(waiver.getInspectorName(), waiver);
133             }
134         }
135     }
136
137     /**
138      * @param signature
139      * @return name map
140      */

141     private Map JavaDoc getNameMap(String JavaDoc signature) {
142         Map JavaDoc nameMap=(Map JavaDoc) signatureMap.get(signature);
143         if (nameMap==null) {
144             nameMap=new HashMap JavaDoc();
145             signatureMap.put(signature, nameMap);
146         }
147         return nameMap;
148     }
149 }
150
Popular Tags