SampledSetImpl implements SampledSet<T> { @Override public boolean add(T element) { boolean returnValue = false; long elementDigest = digestFunction.computeDigest(element); int sampleRateSnapshot = currentSampleRate; if (inSample(elementDigest, sampleRateSnapshot)) { if (proposedSize.incrementAndGet() > maxSetSize) { downSampleLock.writeLock().lock(); try { if (inSample(elementDigest, currentSampleRate) && !baseSet.contains(element)) { downSample(); if (inSample(elementDigest, currentSampleRate)) { returnValue = baseSet.add(element); } } } finally { if (!returnValue) { proposedSize.decrementAndGet(); } downSampleLock.writeLock().unlock(); } } else { downSampleLock.readLock().lock(); try { if (currentSampleRate == sampleRateSnapshot || inSample(elementDigest, currentSampleRate)) { returnValue = baseSet.add(element); } } finally { if (!returnValue) { proposedSize.decrementAndGet(); } downSampleLock.readLock().unlock(); } } } if (returnValue) { dirty.set(true); } return returnValue; } @Deprecated  SampledSetImpl(
      int maxSetSize,
      DigestFunction<T> digestFunction,
      SnapshotableSet<T> baseSet,
      SetFactory<T, SnapshotableSet<T>> setFactory,
      int currentSampleRate);  SampledSetImpl(
      int maxSetSize,
      DigestFunction<T> digestFunction,
      SetFactory<T, SnapshotableSet<T>> setFactory); @Override boolean add(T element); @Override int getMaxSetSize(); @Override int getScaledSize(); @Override int getSampleRate(); @Override int getSize(); @Override Set<T> getEntries(); @Override SampledSetSnapshot<T> sampleAt(int rate); @Override SampledSet<T> merge(SampledSet<T> sampledSet); @Override boolean mergeInPlaceWith(SampledSet<T> sampledSet); @Override boolean hasChanged(); @Override Iterator<T> iterator(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); @Override V[] toArray(V[] a); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean retainAll(Collection<?> c); @Override boolean removeAll(Collection<?> c); @Override void clear(); @Override SampledSet<T> makeSnapshot(); @Override SampledSet<T> makeTransientSnapshot(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test(groups = "fast") public void testMaxNeverExceeded() throws Exception { int numElements = 10 * maxSetSize; for (int i = 0; i < numElements; i++) { integerSet.add((long) i); assertValidMaxSize(); } }