CloudWatchMetricObserver extends BaseMetricObserver { Double truncate(Number numberValue) { double doubleValue = numberValue.doubleValue(); if (truncateEnabled) { final int exponent = Math.getExponent(doubleValue); if (Double.isNaN(doubleValue)) { doubleValue = 0.0; } else if (exponent >= MAX_EXPONENT) { doubleValue = (doubleValue < 0.0) ? -MAX_VALUE : MAX_VALUE; } else if (exponent <= MIN_EXPONENT) { doubleValue = 0.0; } } return doubleValue; } @Deprecated  CloudWatchMetricObserver(String name, String namespace, AWSCredentials credentials); @Deprecated  CloudWatchMetricObserver(String name, String namespace, AWSCredentials credentials,
                                  int batchSize);  CloudWatchMetricObserver(String name, String namespace,
                                  AWSCredentialsProvider provider);  CloudWatchMetricObserver(String name, String namespace,
                                  AWSCredentialsProvider provider, int batchSize);  CloudWatchMetricObserver(String name, String namespace, AmazonCloudWatch cloudWatch);  CloudWatchMetricObserver(String name, String namespace, AmazonCloudWatch cloudWatch,
                                  int batchSize); @Override void updateImpl(List<Metric> metrics); CloudWatchMetricObserver withTruncateEnabled(boolean truncateEnabled);  }
@Test public void testTruncate() throws Exception { observer.withTruncateEnabled(true); Assert.assertEquals(CloudWatchMetricObserver.MAX_VALUE, observer.truncate(Double.POSITIVE_INFINITY)); Assert.assertEquals(-CloudWatchMetricObserver.MAX_VALUE, observer.truncate(Double.NEGATIVE_INFINITY)); Assert.assertEquals(CloudWatchMetricObserver.MAX_VALUE, observer.truncate(Double.MAX_VALUE)); Assert.assertEquals(-CloudWatchMetricObserver.MAX_VALUE, observer.truncate(-Double.MAX_VALUE)); Assert.assertEquals(0.0, observer.truncate(Double.MIN_VALUE)); Assert.assertEquals(0.0, observer.truncate(-Double.MIN_VALUE)); Assert.assertEquals(1.0, observer.truncate(1.0)); Assert.assertEquals(10000.0, observer.truncate(10000.0)); Assert.assertEquals(0.0, observer.truncate(0.0)); Assert.assertEquals(0.0, observer.truncate(Double.NaN)); observer.withTruncateEnabled(false); }