1 23 24 package org.hammurapi; 25 26 import java.util.Collection ; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.Map ; 32 33 import com.pavelvlasov.review.Signed; 34 import com.pavelvlasov.review.SourceMarker; 35 36 40 public class WaiverSet { 41 44 private static final long serialVersionUID = 4873150699959326186L; 45 private Map signatureMap=new HashMap (); 46 private Collection rejectedRequests=new LinkedList (); 47 48 51 public Collection getWaivers() { 52 Collection ret=new LinkedList (); 53 Iterator it=signatureMap.values().iterator(); 54 while (it.hasNext()) { 55 ret.addAll(((Map ) it.next()).values()); 56 } 57 return ret; 58 } 59 60 public int size() { 61 int ret=0; 62 Iterator it=signatureMap.values().iterator(); 63 while (it.hasNext()) { 64 ret+=((Map ) it.next()).size(); 65 } 66 return ret; 67 } 68 69 73 public Collection getRejectedRequests() { 74 return rejectedRequests; 75 } 76 77 84 public Waiver requestWaiver(Violation violation, boolean peek) { 85 if (Boolean.TRUE.equals(violation.getDescriptor().isWaivable())) { 86 SourceMarker sm=violation.getSource(); 87 String signature= sm instanceof Signed ? ((Signed) sm).getSignature() : null; 88 if (signature!=null) { 89 Map nameMap=(Map ) 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 nameMap=(Map ) 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 now) { 123 if (waiver.getExpirationDate()==null || now.before(waiver.getExpirationDate())) { 124 Collection signatures = waiver.getSignatures(); 125 if (signatures!=null && !signatures.isEmpty()) { 126 Iterator it=signatures.iterator(); 127 while (it.hasNext()) { 128 String signature=(String ) it.next(); 129 getNameMap(signature).put(waiver.getInspectorName(), waiver); 130 } 131 } else { 132 getNameMap(null).put(waiver.getInspectorName(), waiver); 133 } 134 } 135 } 136 137 141 private Map getNameMap(String signature) { 142 Map nameMap=(Map ) signatureMap.get(signature); 143 if (nameMap==null) { 144 nameMap=new HashMap (); 145 signatureMap.put(signature, nameMap); 146 } 147 return nameMap; 148 } 149 } 150 | Popular Tags |