@Override public boolean add(Counter<?> e) { if (null == e) { return false; } return counters.putIfAbsent(e.getUniqueName(), e) == null; }
public void merge(CounterSet that) { for (Counter<?> theirCounter : that) { Counter<?> myCounter = counters.get(theirCounter.getUniqueName()); if (myCounter != null) { mergeCounters(myCounter, theirCounter); } else { addCounter(theirCounter); } } }
/** * 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; }