| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
package ca.uhn.hl7v2.validation.impl; |
| 27 | |
|
| 28 | |
import java.io.Serializable; |
| 29 | |
import java.util.*; |
| 30 | |
|
| 31 | |
import ca.uhn.hl7v2.model.Primitive; |
| 32 | |
import ca.uhn.hl7v2.validation.EncodingRule; |
| 33 | |
import ca.uhn.hl7v2.validation.MessageRule; |
| 34 | |
import ca.uhn.hl7v2.validation.PrimitiveTypeRule; |
| 35 | |
import ca.uhn.hl7v2.validation.Rule; |
| 36 | |
import ca.uhn.hl7v2.validation.ValidationContext; |
| 37 | |
import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
@SuppressWarnings("serial") |
| 46 | |
public class ValidationContextImpl implements ValidationContext, Serializable { |
| 47 | |
|
| 48 | |
private List<RuleBinding<PrimitiveTypeRule>> myPrimitiveRuleBindings; |
| 49 | |
private List<RuleBinding<MessageRule>> myMessageRuleBindings; |
| 50 | |
private List<RuleBinding<EncodingRule>> myEncodingRuleBindings; |
| 51 | |
|
| 52 | |
protected Map<String, Collection<PrimitiveTypeRule>> primitiveRuleCache; |
| 53 | |
protected Map<String, Collection<MessageRule>> messageRuleCache; |
| 54 | |
protected Map<String, Collection<EncodingRule>> encodingRuleCache; |
| 55 | |
|
| 56 | 5800 | public ValidationContextImpl() { |
| 57 | 5800 | myPrimitiveRuleBindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>(); |
| 58 | 5800 | myMessageRuleBindings = new ArrayList<RuleBinding<MessageRule>>(); |
| 59 | 5800 | myEncodingRuleBindings = new ArrayList<RuleBinding<EncodingRule>>(); |
| 60 | 5800 | initCaches(); |
| 61 | 5800 | } |
| 62 | |
|
| 63 | |
ValidationContextImpl(ValidationRuleBuilder builder) { |
| 64 | 5740 | this(); |
| 65 | 5740 | for (RuleBinding<? extends Rule<?>> ruleBinding : builder.initialize()) { |
| 66 | 174200 | if (ruleBinding instanceof MessageRuleBinding) |
| 67 | 10870 | myMessageRuleBindings.add((MessageRuleBinding)ruleBinding); |
| 68 | 163330 | else if (ruleBinding instanceof EncodingRuleBinding) |
| 69 | 20 | myEncodingRuleBindings.add((EncodingRuleBinding)ruleBinding); |
| 70 | 163310 | else if (ruleBinding instanceof PrimitiveTypeRuleBinding) |
| 71 | 163310 | myPrimitiveRuleBindings.add((PrimitiveTypeRuleBinding)ruleBinding); |
| 72 | 174200 | } |
| 73 | 5740 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
protected void initCaches() { |
| 85 | 5800 | primitiveRuleCache = newRuleCache(100); |
| 86 | 5800 | messageRuleCache = newRuleCache(100); |
| 87 | 5800 | encodingRuleCache = newRuleCache(10); |
| 88 | 5800 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public Collection<PrimitiveTypeRule> getPrimitiveRules(String theVersion, String theTypeName, Primitive theType) { |
| 95 | 134458 | Collection<PrimitiveTypeRule> rules = primitiveRuleCache.get(theVersion + theTypeName); |
| 96 | 134458 | if (rules == null) { |
| 97 | 8996 | rules = getRules(myPrimitiveRuleBindings, theVersion, theTypeName); |
| 98 | 8996 | primitiveRuleCache.put(theVersion + theTypeName, rules); |
| 99 | |
} |
| 100 | 134458 | return rules; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public List<RuleBinding<PrimitiveTypeRule>> getPrimitiveRuleBindings() { |
| 108 | 10 | return myPrimitiveRuleBindings; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public Collection<MessageRule> getMessageRules(String theVersion, String theMessageType, String theTriggerEvent) { |
| 116 | 5633 | Collection<MessageRule> rules = messageRuleCache.get(theVersion + theMessageType + theTriggerEvent); |
| 117 | 5633 | if (rules == null) { |
| 118 | 1606 | rules = getRules(myMessageRuleBindings, theVersion, theMessageType + "^" + theTriggerEvent); |
| 119 | 1606 | messageRuleCache.put(theVersion + theMessageType + theTriggerEvent, rules); |
| 120 | |
} |
| 121 | 5633 | return rules; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public List<RuleBinding<MessageRule>> getMessageRuleBindings() { |
| 128 | 15 | return myMessageRuleBindings; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public Collection<EncodingRule> getEncodingRules(String theVersion, String theEncoding) { |
| 136 | 5645 | Collection<EncodingRule> rules = encodingRuleCache.get(theVersion + theEncoding); |
| 137 | 5645 | if (rules == null) { |
| 138 | 1420 | rules = getRules(myEncodingRuleBindings, theVersion, theEncoding); |
| 139 | 1420 | encodingRuleCache.put(theVersion + theEncoding, rules); |
| 140 | |
} |
| 141 | 5645 | return rules; |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public List<RuleBinding<EncodingRule>> getEncodingRuleBindings() { |
| 148 | 10 | return myEncodingRuleBindings; |
| 149 | |
} |
| 150 | |
|
| 151 | |
private <T extends Rule<?>> Collection<T> getRules(List<RuleBinding<T>> bindings, String version, String scope) { |
| 152 | 12022 | List<T> active = new ArrayList<T>(bindings.size()); |
| 153 | 12022 | for (RuleBinding<T> binding : bindings) { |
| 154 | 238402 | if (applies(binding, version, scope)) |
| 155 | 9978 | active.add(binding.getRule()); |
| 156 | 238402 | } |
| 157 | 12022 | return active; |
| 158 | |
} |
| 159 | |
|
| 160 | |
private boolean applies(RuleBinding<?> binding, String version, String scope) { |
| 161 | 238402 | return (binding.getActive() && binding.appliesToVersion(version) && binding.appliesToScope(scope)); |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | 17400 | private static class RuleCache<T extends Rule<?>> extends LinkedHashMap<String, Collection<T>> { |
| 171 | |
|
| 172 | |
private final int size; |
| 173 | |
|
| 174 | |
private RuleCache(int size) { |
| 175 | 17400 | super(size); |
| 176 | 17400 | this.size = size; |
| 177 | 17400 | } |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
protected boolean removeEldestEntry(Map.Entry<String, Collection<T>> eldest) { |
| 181 | 12010 | return size() > size; |
| 182 | |
} |
| 183 | |
|
| 184 | |
} |
| 185 | |
|
| 186 | |
protected static <T extends Rule<?>> Map<String, Collection<T>> newRuleCache(int size) { |
| 187 | 17400 | Map<String, Collection<T>> cache = new RuleCache<T>(size); |
| 188 | 17400 | return Collections.synchronizedMap(cache); |
| 189 | |
} |
| 190 | |
} |