001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023:
024: package org.hammurapi;
025:
026: import java.util.Collection;
027: import java.util.Date;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import java.util.Map;
032:
033: import com.pavelvlasov.review.Signed;
034: import com.pavelvlasov.review.SourceMarker;
035:
036: /**
037: * @author Pavel Vlasov
038: * @version $Revision: 1.8 $
039: */
040: public class WaiverSet {
041: /**
042: * Comment for <code>serialVersionUID</code>
043: */
044: private static final long serialVersionUID = 4873150699959326186L;
045: private Map signatureMap = new HashMap();
046: private Collection rejectedRequests = new LinkedList();
047:
048: /**
049: * @return Collection of waivers remaining in the set
050: */
051: public Collection getWaivers() {
052: Collection ret = new LinkedList();
053: Iterator it = signatureMap.values().iterator();
054: while (it.hasNext()) {
055: ret.addAll(((Map) it.next()).values());
056: }
057: return ret;
058: }
059:
060: public int size() {
061: int ret = 0;
062: Iterator it = signatureMap.values().iterator();
063: while (it.hasNext()) {
064: ret += ((Map) it.next()).size();
065: }
066: return ret;
067: }
068:
069: /**
070: * @return Collection of violations which requested waivers, but
071: * weren't granted ones.
072: */
073: public Collection getRejectedRequests() {
074: return rejectedRequests;
075: }
076:
077: /**
078: * Requests and removes a waiver from the set. Waivers with signature==null
079: * aren't removed as they apply to multiple violations.
080: * @param violation
081: * @param peek Just peek for waiver.
082: * @return Waiver if there is one for the violation, null otherwise.
083: */
084: public Waiver requestWaiver(Violation violation, boolean peek) {
085: if (Boolean.TRUE.equals(violation.getDescriptor().isWaivable())) {
086: SourceMarker sm = violation.getSource();
087: String signature = sm instanceof Signed ? ((Signed) sm)
088: .getSignature() : null;
089: if (signature != null) {
090: Map nameMap = (Map) signatureMap.get(signature);
091: Waiver ret = nameMap == null ? null : (Waiver) nameMap
092: .get(violation.getDescriptor().getName());
093:
094: if (ret != null) {
095: if (!peek) {
096: nameMap.remove(violation.getDescriptor()
097: .getName());
098: }
099:
100: if (ret.waive(violation, peek)) {
101: return ret;
102: }
103: }
104: }
105:
106: Map nameMap = (Map) signatureMap.get(null);
107: Waiver ret = nameMap == null ? null : (Waiver) nameMap
108: .get(violation.getDescriptor().getName());
109: boolean waived = ret != null && ret.waive(violation, peek);
110:
111: if (!waived) {
112: rejectedRequests.add(violation);
113: }
114:
115: if (ret != null && !ret.isActive()) {
116: nameMap.remove(ret.getInspectorName());
117: }
118:
119: return waived ? ret : null;
120: }
121:
122: return null;
123: }
124:
125: public void addWaiver(Waiver waiver, Date now) {
126: if (waiver.getExpirationDate() == null
127: || now.before(waiver.getExpirationDate())) {
128: Collection signatures = waiver.getSignatures();
129: if (signatures != null && !signatures.isEmpty()) {
130: Iterator it = signatures.iterator();
131: while (it.hasNext()) {
132: String signature = (String) it.next();
133: getNameMap(signature).put(
134: waiver.getInspectorName(), waiver);
135: }
136: } else {
137: getNameMap(null).put(waiver.getInspectorName(), waiver);
138: }
139: }
140: }
141:
142: /**
143: * @param signature
144: * @return name map
145: */
146: private Map getNameMap(String signature) {
147: Map nameMap = (Map) signatureMap.get(signature);
148: if (nameMap == null) {
149: nameMap = new HashMap();
150: signatureMap.put(signature, nameMap);
151: }
152: return nameMap;
153: }
154: }
|