private <T> void mergeCounters(Counter<T> mine, Counter<?> theirCounter) { checkArgument( mine.isCompatibleWith(theirCounter), "Can't merge CounterSets containing incompatible counters with the same name: " + "%s (existing) and %s (merged)", mine, theirCounter); @SuppressWarnings("unchecked") Counter<T> theirs = (Counter<T>) theirCounter; mine.merge(theirs); }
/** * Adds the given Counter to this CounterSet. * * <p>If a counter with the same name already exists, it will be reused, as long as it is * compatible. * * @return the Counter that was reused, or added * @throws IllegalArgumentException if a counter with the same name but an incompatible kind had * already been added */ public <T> Counter<T> addOrReuseCounter(Counter<T> counter) { Counter<?> oldCounter = counters.putIfAbsent(counter.getUniqueName(), counter); if (oldCounter != null) { checkArgument( counter.isCompatibleWith(oldCounter), "Counter %s duplicates incompatible counter %s in %s", counter, oldCounter, this); // Return the counter to reuse. @SuppressWarnings("unchecked") Counter<T> compatibleCounter = (Counter<T>) oldCounter; return compatibleCounter; } return counter; }