/** * Constructor. * * @param copy QueryContext to gather sequence number and total queries from * @param contextMap Map of context values to override with */ protected QueryContext(QueryContext copy, Map<Param, Object> contextMap) { // Ensure that all contained context fields are of the correct type for (Map.Entry<Param, Object> entry: contextMap.entrySet()) { validateField(entry.getKey(), entry.getValue()); } // Store an immutable copy this.contextMap = ImmutableMap.copyOf(contextMap); this.totalQueries = copy.totalQueries; this.sequenceNumber = copy.sequenceNumber; }
/** * Constructor. * * @param contextMap Map of context values * @param totalQueries Number of queries that this context is attached to */ public QueryContext(Map<Param, Object> contextMap, AtomicLong totalQueries) { // Ensure that all contained context fields are of the correct type for (Map.Entry<Param, Object> entry: contextMap.entrySet()) { validateField(entry.getKey(), entry.getValue()); } // Store an immutable copy this.contextMap = ImmutableMap.copyOf(contextMap); this.totalQueries = totalQueries == null ? new AtomicLong(0) : totalQueries; this.sequenceNumber = this.totalQueries.incrementAndGet(); }
/** * Copy-mutate the context on the given field with the given value. * * @param param Field to set in the clone * @param value Value to set in the clone * * @return a clone of this context with the new value set */ protected QueryContext withValue(Param param, Object value) { validateField(param, value); Map<Param, Object> values = new HashMap<>(contextMap); if (value == null) { values.remove(param); } else { values.put(param, value); } return new QueryContext(this, values); }