OutputObjectAndByteCounter implements ElementCounter { @Override public void update(Object elem) throws Exception { if (objectCount != null) { objectCount.addValue(1L); } if ((byteCountObserver != null || meanByteCountObserver != null) && (sampleElement() || elementByteSizeObservable.isRegisterByteSizeObserverCheap(elem))) { if (byteCountObserver != null) { byteCountObserver.setScalingFactor( Math.max(samplingToken, SAMPLING_CUTOFF) / (double) SAMPLING_CUTOFF); elementByteSizeObservable.registerByteSizeObserver(elem, byteCountObserver); } if (meanByteCountObserver != null) { elementByteSizeObservable.registerByteSizeObserver(elem, meanByteCountObserver); } if (byteCountObserver != null && !byteCountObserver.getIsLazy()) { byteCountObserver.advance(); } if (meanByteCountObserver != null && !meanByteCountObserver.getIsLazy()) { meanByteCountObserver.advance(); } } } @SuppressWarnings("unchecked")  OutputObjectAndByteCounter(
      ElementByteSizeObservable<?> elementByteSizeObservable, CounterFactory counterFactory); @SuppressWarnings("unchecked")  OutputObjectAndByteCounter(
      ElementByteSizeObservable<?> elementByteSizeObservable,
      CounterFactory counterFactory,
      @Nullable NameContext nameContext); OutputObjectAndByteCounter countObject(String objectCounterName); OutputObjectAndByteCounter countBytes(String bytesCounterName); OutputObjectAndByteCounter countMeanByte(String meanByteCounterName); OutputObjectAndByteCounter setSamplingPeriod(int period); Counter<Long, ?> getObjectCount(); Counter<Long, Long> getByteCount(); Counter<Long, CounterMean<Long>> getMeanByteCount(); @Override void update(Object elem); @Override void finishLazyUpdate(Object elem); OutputObjectAndByteCounter setRandom(Random random);  }
@Test public void testUpdate() throws Exception { TestOutputCounter outputCounter = new TestOutputCounter(NameContextsForTests.nameContextForTest()); outputCounter.update("hi"); outputCounter.finishLazyUpdate("hi"); outputCounter.update("bob"); outputCounter.finishLazyUpdate("bob"); CounterMean<Long> meanByteCount = outputCounter.getMeanByteCount().getAggregate(); assertEquals(7, (long) meanByteCount.getAggregate()); assertEquals(2, meanByteCount.getCount()); }
@Test public void testIncorrectType() throws Exception { TestOutputCounter outputCounter = new TestOutputCounter(NameContextsForTests.nameContextForTest()); thrown.expect(ClassCastException.class); outputCounter.update(5); }
@Test public void testNullArgument() throws Exception { TestOutputCounter outputCounter = new TestOutputCounter(NameContextsForTests.nameContextForTest()); thrown.expect(CoderException.class); outputCounter.update(null); }