diff --git a/.travis.yml b/.travis.yml index 8e0bf36..8671fbe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,7 @@ sudo: required language: java -# This project is aimed to be JRE7-compatible. However, there -# are issues testing against oraclejdk7 in Travis: -# https://github.com/travis-ci/travis-ci/issues/7884 +# This project is JRE8-compatible. jdk: - openjdk8 - oraclejdk8 diff --git a/DEVELOPER.md b/DEVELOPER.md index 8be7c8e..dc597e3 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -6,7 +6,7 @@ This document summarizes information relevant to Java tally contributors. ### Prerequisites In order to build this project, you must have: -- JDK-7 or later +- JDK-8 or later - Apache Thrift 0.9.x -- only if you plan to make changes to Thrift files and recompile (regenerate) source files ### Building and testing diff --git a/core/src/main/java/com/uber/m3/tally/BucketPairImpl.java b/core/src/main/java/com/uber/m3/tally/BucketPairImpl.java index b614fec..1edacf8 100644 --- a/core/src/main/java/com/uber/m3/tally/BucketPairImpl.java +++ b/core/src/main/java/com/uber/m3/tally/BucketPairImpl.java @@ -28,10 +28,10 @@ * Default implementation of a {@link BucketPair} */ public class BucketPairImpl implements BucketPair { - private double lowerBoundValue; - private double upperBoundValue; - private Duration lowerBoundDuration; - private Duration upperBoundDuration; + private final double lowerBoundValue; + private final double upperBoundValue; + private final Duration lowerBoundDuration; + private final Duration upperBoundDuration; public BucketPairImpl( double lowerBoundValue, diff --git a/core/src/main/java/com/uber/m3/tally/CapableOf.java b/core/src/main/java/com/uber/m3/tally/CapableOf.java index cb554c6..acd72af 100644 --- a/core/src/main/java/com/uber/m3/tally/CapableOf.java +++ b/core/src/main/java/com/uber/m3/tally/CapableOf.java @@ -29,8 +29,8 @@ public class CapableOf implements Capabilities { public static final CapableOf REPORTING = new CapableOf(true, false); public static final CapableOf REPORTING_TAGGING = new CapableOf(true, true); - private boolean reporting; - private boolean tagging; + private final boolean reporting; + private final boolean tagging; public CapableOf(boolean reporting, boolean tagging) { this.reporting = reporting; @@ -69,8 +69,8 @@ public boolean equals(Object other) { public int hashCode() { int code = 0; - code = 31 * code + new Boolean(reporting).hashCode(); - code = 31 * code + new Boolean(tagging).hashCode(); + code = 31 * code + Boolean.valueOf(reporting).hashCode(); + code = 31 * code + Boolean.valueOf(tagging).hashCode(); return code; } diff --git a/core/src/main/java/com/uber/m3/tally/CounterImpl.java b/core/src/main/java/com/uber/m3/tally/CounterImpl.java index bb56e96..7d3deff 100644 --- a/core/src/main/java/com/uber/m3/tally/CounterImpl.java +++ b/core/src/main/java/com/uber/m3/tally/CounterImpl.java @@ -28,8 +28,8 @@ * Default implementation of a {@link Counter}. */ class CounterImpl implements Counter { - private AtomicLong prev = new AtomicLong(0); - private AtomicLong curr = new AtomicLong(0); + private final AtomicLong prev = new AtomicLong(0); + private final AtomicLong curr = new AtomicLong(0); @Override public void inc(long delta) { diff --git a/core/src/main/java/com/uber/m3/tally/CounterSnapshotImpl.java b/core/src/main/java/com/uber/m3/tally/CounterSnapshotImpl.java index 6d5a31e..0e93183 100644 --- a/core/src/main/java/com/uber/m3/tally/CounterSnapshotImpl.java +++ b/core/src/main/java/com/uber/m3/tally/CounterSnapshotImpl.java @@ -28,9 +28,9 @@ * Default implementation of a {@link CounterSnapshot}. */ class CounterSnapshotImpl implements CounterSnapshot { - private String name; - private ImmutableMap tags; - private long value; + private final String name; + private final ImmutableMap tags; + private final long value; CounterSnapshotImpl(String name, ImmutableMap tags, long value) { this.name = name; diff --git a/core/src/main/java/com/uber/m3/tally/DurationBuckets.java b/core/src/main/java/com/uber/m3/tally/DurationBuckets.java index 91a9d77..f667da5 100644 --- a/core/src/main/java/com/uber/m3/tally/DurationBuckets.java +++ b/core/src/main/java/com/uber/m3/tally/DurationBuckets.java @@ -47,7 +47,7 @@ public Double[] asValues() { @Override public Duration[] asDurations() { - return buckets.toArray(new Duration[buckets.size()]); + return buckets.toArray(new Duration[0]); } /** @@ -106,7 +106,6 @@ public static DurationBuckets exponential(Duration start, double factor, int num /** * Allows to create bucket with finer bucket creation control - * * @param sortedDurations sorted values (ascending) of upper bound of the buckets * @return {@link DurationBuckets} of the specified parameters */ diff --git a/core/src/main/java/com/uber/m3/tally/GaugeImpl.java b/core/src/main/java/com/uber/m3/tally/GaugeImpl.java index e4b75de..868425d 100644 --- a/core/src/main/java/com/uber/m3/tally/GaugeImpl.java +++ b/core/src/main/java/com/uber/m3/tally/GaugeImpl.java @@ -29,8 +29,8 @@ * Default implementation of a {@link Gauge}. */ class GaugeImpl implements Gauge { - private AtomicBoolean updated = new AtomicBoolean(false); - private AtomicLong curr = new AtomicLong(0); + private final AtomicBoolean updated = new AtomicBoolean(false); + private final AtomicLong curr = new AtomicLong(0); @Override public void update(double value) { diff --git a/core/src/main/java/com/uber/m3/tally/GaugeSnapshotImpl.java b/core/src/main/java/com/uber/m3/tally/GaugeSnapshotImpl.java index e0aad6f..54e31b3 100644 --- a/core/src/main/java/com/uber/m3/tally/GaugeSnapshotImpl.java +++ b/core/src/main/java/com/uber/m3/tally/GaugeSnapshotImpl.java @@ -28,9 +28,9 @@ * Default implementation of a {@link GaugeSnapshot}. */ class GaugeSnapshotImpl implements GaugeSnapshot { - private String name; - private ImmutableMap tags; - private double value; + private final String name; + private final ImmutableMap tags; + private final double value; GaugeSnapshotImpl(String name, ImmutableMap tags, double value) { this.name = name; diff --git a/core/src/main/java/com/uber/m3/tally/HistogramImpl.java b/core/src/main/java/com/uber/m3/tally/HistogramImpl.java index e559733..4910032 100644 --- a/core/src/main/java/com/uber/m3/tally/HistogramImpl.java +++ b/core/src/main/java/com/uber/m3/tally/HistogramImpl.java @@ -33,13 +33,13 @@ * Default implementation of a {@link Histogram}. */ class HistogramImpl implements Histogram, StopwatchRecorder { - private Type type; - private String name; - private ImmutableMap tags; - private Buckets specification; - private List buckets; - private List lookupByValue; - private List lookupByDuration; + private final Type type; + private final String name; + private final ImmutableMap tags; + private final Buckets specification; + private final List buckets; + private final List lookupByValue; + private final List lookupByDuration; HistogramImpl( String name, @@ -186,7 +186,7 @@ public void recordStopwatch(long stopwatchStart) { recordDuration(Duration.between(stopwatchStart, System.nanoTime())); } - class HistogramBucket { + static class HistogramBucket { CounterImpl samples; double valueLowerBound; double valueUpperBound; diff --git a/core/src/main/java/com/uber/m3/tally/HistogramSnapshotImpl.java b/core/src/main/java/com/uber/m3/tally/HistogramSnapshotImpl.java index f0f326d..b10bcb8 100644 --- a/core/src/main/java/com/uber/m3/tally/HistogramSnapshotImpl.java +++ b/core/src/main/java/com/uber/m3/tally/HistogramSnapshotImpl.java @@ -29,10 +29,10 @@ * Default implementation of a {@link HistogramSnapshot}. */ class HistogramSnapshotImpl implements HistogramSnapshot { - private String name; - private ImmutableMap tags; - private Map values; - private Map durations; + private final String name; + private final ImmutableMap tags; + private final Map values; + private final Map durations; HistogramSnapshotImpl( String name, diff --git a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java index fe42e82..a54ee61 100644 --- a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java +++ b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java @@ -33,14 +33,14 @@ * Default {@link Scope} implementation. */ class ScopeImpl implements Scope { - private StatsReporter reporter; - private String prefix; - private String separator; - private ImmutableMap tags; - private Buckets defaultBuckets; + private final StatsReporter reporter; + private final String prefix; + private final String separator; + private final ImmutableMap tags; + private final Buckets defaultBuckets; - private ScheduledExecutorService scheduler; - private Registry registry; + private final ScheduledExecutorService scheduler; + private final Registry registry; // ConcurrentHashMap nearly always allowing read operations seems like a good // performance upside to the consequence of reporting a newly-made metric in @@ -71,15 +71,8 @@ public Counter counter(String name) { return counter; } - synchronized (counters) { - if (!counters.containsKey(name)) { - counters.put(name, new CounterImpl()); - } - - counter = counters.get(name); - } - - return counter; + counters.putIfAbsent(name, new CounterImpl()); + return counters.get(name); } @Override @@ -90,15 +83,8 @@ public Gauge gauge(String name) { return gauge; } - synchronized (gauges) { - if (!gauges.containsKey(name)) { - gauges.put(name, new GaugeImpl()); - } - - gauge = gauges.get(name); - } - - return gauge; + gauges.putIfAbsent(name, new GaugeImpl()); + return gauges.get(name); } @Override @@ -109,15 +95,8 @@ public Timer timer(String name) { return timer; } - synchronized (timers) { - if (!timers.containsKey(name)) { - timers.put(name, new TimerImpl(fullyQualifiedName(name), tags, reporter)); - } - - timer = timers.get(name); - } - - return timer; + timers.putIfAbsent(name, new TimerImpl(fullyQualifiedName(name), tags, reporter)); + return timers.get(name); } @Override @@ -132,15 +111,8 @@ public Histogram histogram(String name, Buckets buckets) { return histogram; } - synchronized (histograms) { - if (!histograms.containsKey(name)) { - histograms.put(name, new HistogramImpl(fullyQualifiedName(name), tags, reporter, buckets)); - } - - histogram = histograms.get(name); - } - - return histogram; + histograms.putIfAbsent(name, new HistogramImpl(fullyQualifiedName(name), tags, reporter, buckets)); + return histograms.get(name); } @Override @@ -208,11 +180,11 @@ static String keyForPrefixedStringMap(String prefix, ImmutableMap) ImmutableMap.EMPTY; } Set keySet = stringMap.keySet(); - String[] sortedKeys = keySet.toArray(new String[keySet.size()]); + String[] sortedKeys = keySet.toArray(new String[0]); Arrays.sort(sortedKeys); StringBuilder keyBuffer = new StringBuilder(prefix.length() + sortedKeys.length * 20); diff --git a/core/src/main/java/com/uber/m3/tally/SnapshotImpl.java b/core/src/main/java/com/uber/m3/tally/SnapshotImpl.java index 3673b35..dab1278 100644 --- a/core/src/main/java/com/uber/m3/tally/SnapshotImpl.java +++ b/core/src/main/java/com/uber/m3/tally/SnapshotImpl.java @@ -27,10 +27,10 @@ * Default implementation of a {@link Snapshot}. */ class SnapshotImpl implements Snapshot { - Map counters = new ConcurrentHashMap<>(); - Map gauges = new ConcurrentHashMap<>(); - Map timers = new ConcurrentHashMap<>(); - Map histograms = new ConcurrentHashMap<>(); + private final Map counters = new ConcurrentHashMap<>(); + private final Map gauges = new ConcurrentHashMap<>(); + private final Map timers = new ConcurrentHashMap<>(); + private final Map histograms = new ConcurrentHashMap<>(); @Override public Map counters() { diff --git a/core/src/main/java/com/uber/m3/tally/StatsReporter.java b/core/src/main/java/com/uber/m3/tally/StatsReporter.java index 678ab04..18105ef 100644 --- a/core/src/main/java/com/uber/m3/tally/StatsReporter.java +++ b/core/src/main/java/com/uber/m3/tally/StatsReporter.java @@ -35,9 +35,9 @@ public interface StatsReporter extends BaseStatsReporter { * @param value value to report */ void reportCounter( - String name, - Map tags, - long value + String name, + Map tags, + long value ); /** @@ -47,9 +47,9 @@ void reportCounter( * @param value value to report */ void reportGauge( - String name, - Map tags, - double value + String name, + Map tags, + double value ); /** @@ -59,9 +59,9 @@ void reportGauge( * @param interval interval to report */ void reportTimer( - String name, - Map tags, - Duration interval + String name, + Map tags, + Duration interval ); /** @@ -74,12 +74,12 @@ void reportTimer( * @param samples samples to report */ void reportHistogramValueSamples( - String name, - Map tags, - Buckets buckets, - double bucketLowerBound, - double bucketUpperBound, - long samples + String name, + Map tags, + Buckets buckets, + double bucketLowerBound, + double bucketUpperBound, + long samples ); /** @@ -92,11 +92,11 @@ void reportHistogramValueSamples( * @param samples samples to report */ void reportHistogramDurationSamples( - String name, - Map tags, - Buckets buckets, - Duration bucketLowerBound, - Duration bucketUpperBound, - long samples + String name, + Map tags, + Buckets buckets, + Duration bucketLowerBound, + Duration bucketUpperBound, + long samples ); } diff --git a/core/src/main/java/com/uber/m3/tally/Stopwatch.java b/core/src/main/java/com/uber/m3/tally/Stopwatch.java index bc4672e..093bf75 100644 --- a/core/src/main/java/com/uber/m3/tally/Stopwatch.java +++ b/core/src/main/java/com/uber/m3/tally/Stopwatch.java @@ -27,8 +27,8 @@ * of the stopwatch are comparable with one another. */ public class Stopwatch { - private long startNanos; - private StopwatchRecorder recorder; + private final long startNanos; + private final StopwatchRecorder recorder; /** * Creates a stopwatch. diff --git a/core/src/main/java/com/uber/m3/tally/TimerImpl.java b/core/src/main/java/com/uber/m3/tally/TimerImpl.java index b31be1c..cd8bf2e 100644 --- a/core/src/main/java/com/uber/m3/tally/TimerImpl.java +++ b/core/src/main/java/com/uber/m3/tally/TimerImpl.java @@ -34,10 +34,10 @@ * Default implementation of a {@link Timer}. */ class TimerImpl implements Timer, StopwatchRecorder { - private String name; - private ImmutableMap tags; - private StatsReporter reporter; - private Values unreported = new Values(); + private final String name; + private final ImmutableMap tags; + private final StatsReporter reporter; + private final Values unreported = new Values(); TimerImpl(String name, ImmutableMap tags, StatsReporter reporter) { this.name = name; @@ -89,7 +89,7 @@ static class Values { // instead as this separation is not needed e.g. we only lock a // ConcurrentHashMap when doing writes and not for reads. private final ReadWriteLock rwlock = new ReentrantReadWriteLock(); - private List values = new ArrayList<>(); + private final List values = new ArrayList<>(); Lock readLock() { return rwlock.readLock(); diff --git a/core/src/main/java/com/uber/m3/tally/TimerSnapshotImpl.java b/core/src/main/java/com/uber/m3/tally/TimerSnapshotImpl.java index c40bfcb..3656757 100644 --- a/core/src/main/java/com/uber/m3/tally/TimerSnapshotImpl.java +++ b/core/src/main/java/com/uber/m3/tally/TimerSnapshotImpl.java @@ -29,9 +29,9 @@ * Default implementation of a {@link TimerSnapshot}. */ class TimerSnapshotImpl implements TimerSnapshot { - private String name; - private ImmutableMap tags; - private Duration[] values; + private final String name; + private final ImmutableMap tags; + private final Duration[] values; TimerSnapshotImpl(String name, ImmutableMap tags, Duration[] values) { this.name = name; diff --git a/core/src/main/java/com/uber/m3/tally/ValueBuckets.java b/core/src/main/java/com/uber/m3/tally/ValueBuckets.java index 387a56a..a23928a 100644 --- a/core/src/main/java/com/uber/m3/tally/ValueBuckets.java +++ b/core/src/main/java/com/uber/m3/tally/ValueBuckets.java @@ -36,7 +36,7 @@ public ValueBuckets() { @Override public Double[] asValues() { - return buckets.toArray(new Double[buckets.size()]); + return buckets.toArray(new Double[0]); } @Override @@ -93,7 +93,7 @@ public static ValueBuckets exponential(double start, double factor, int numBucke Double[] buckets = new Double[numBuckets]; - Double curDuration = start; + double curDuration = start; for (int i = 0; i < numBuckets; i++) { buckets[i] = curDuration; @@ -106,7 +106,6 @@ public static ValueBuckets exponential(double start, double factor, int numBucke /** * Helper function to create {@link ValueBuckets} with custom buckets. - * * @param sortedBucketUpperValues sorted (ascending order) values of bucket's upper bound * @return {@link ValueBuckets} of the specified paramters */ diff --git a/core/src/main/java/com/uber/m3/util/ImmutableList.java b/core/src/main/java/com/uber/m3/util/ImmutableList.java index eed1e10..abdbbd6 100644 --- a/core/src/main/java/com/uber/m3/util/ImmutableList.java +++ b/core/src/main/java/com/uber/m3/util/ImmutableList.java @@ -32,7 +32,7 @@ public class ImmutableList implements List { private final ArrayList collection; public ImmutableList(Collection collection) { - this.collection = new ArrayList(collection); + this.collection = new ArrayList<>(collection); } @Override @@ -162,7 +162,7 @@ public boolean equals(Object other) { return false; } - return collection.equals(((ImmutableList) other).collection); + return collection.equals(((ImmutableList) other).collection); } @Override diff --git a/core/src/main/java/com/uber/m3/util/ImmutableMap.java b/core/src/main/java/com/uber/m3/util/ImmutableMap.java index 1901cf5..2892904 100644 --- a/core/src/main/java/com/uber/m3/util/ImmutableMap.java +++ b/core/src/main/java/com/uber/m3/util/ImmutableMap.java @@ -29,7 +29,7 @@ * @param the value type */ public class ImmutableMap implements Map { - public static final ImmutableMap EMPTY = new ImmutableMap(); + public static final ImmutableMap EMPTY = new ImmutableMap<>(); private final HashMap map; @@ -183,7 +183,7 @@ public boolean equals(Object other) { return false; } - return map.equals(((ImmutableMap) other).map); + return map.equals(((ImmutableMap) other).map); } @Override @@ -202,7 +202,7 @@ public String toString() { * @param the value type */ public static class Builder { - private HashMap map; + private final HashMap map; public Builder() { this(16, 0.75f); diff --git a/core/src/main/java/com/uber/m3/util/ImmutableSet.java b/core/src/main/java/com/uber/m3/util/ImmutableSet.java index 006fa21..53168dc 100644 --- a/core/src/main/java/com/uber/m3/util/ImmutableSet.java +++ b/core/src/main/java/com/uber/m3/util/ImmutableSet.java @@ -112,7 +112,7 @@ public boolean equals(Object other) { return false; } - return set.equals(((ImmutableSet) other).set); + return set.equals(((ImmutableSet) other).set); } @Override @@ -125,7 +125,7 @@ public int hashCode() { * @param the type of elements in this set */ public static class Builder { - private HashSet set; + private final HashSet set; public Builder() { this(16, 0.75f); diff --git a/core/src/main/java/com/uber/m3/util/UnmodifiableIterator.java b/core/src/main/java/com/uber/m3/util/UnmodifiableIterator.java index c1d3ece..ca0ddf7 100644 --- a/core/src/main/java/com/uber/m3/util/UnmodifiableIterator.java +++ b/core/src/main/java/com/uber/m3/util/UnmodifiableIterator.java @@ -26,11 +26,11 @@ * A thin wrapper around an {@code Iterator} to disallow modifications to this * iterator. *

- * Note: Take care in using this class. If the user has a - * reference to constructing iterator, this iterator can still be modified - * from under the hood. This class mainly serves as an iterator over - * {@code Immutable*} classes, where users will not have a reference to the underlying - * iterator. + * Note: Take care in using this class. If the user has a + * reference to constructing iterator, this iterator can still be modified + * from under the hood. This class mainly serves as an iterator over + * {@code Immutable*} classes, where users will not have a reference to the underlying + * iterator. *

* @param the type of element this iterator iterates over */ diff --git a/core/src/main/java/com/uber/m3/util/UnmodifiableListIterator.java b/core/src/main/java/com/uber/m3/util/UnmodifiableListIterator.java index 2a7a3ce..87fedc8 100644 --- a/core/src/main/java/com/uber/m3/util/UnmodifiableListIterator.java +++ b/core/src/main/java/com/uber/m3/util/UnmodifiableListIterator.java @@ -26,11 +26,11 @@ * A thin wrapper around a {@code ListIterator} to disallow modifications to this * list iterator. *

- * Note: Take care in using this class. If the user has a - * reference to constructing iterator, this iterator can still be modified - * from under the hood. This class mainly serves as an iterator over - * {@code Immutable*} classes, where users will not have a reference to the underlying - * iterator. + * Note: Take care in using this class. If the user has a + * reference to constructing iterator, this iterator can still be modified + * from under the hood. This class mainly serves as an iterator over + * {@code Immutable*} classes, where users will not have a reference to the underlying + * iterator. *

* @param the type of element this iterator iterates over */ diff --git a/core/src/test/java/com/uber/m3/tally/BucketPairImplTest.java b/core/src/test/java/com/uber/m3/tally/BucketPairImplTest.java index 5af2760..e8911d4 100644 --- a/core/src/test/java/com/uber/m3/tally/BucketPairImplTest.java +++ b/core/src/test/java/com/uber/m3/tally/BucketPairImplTest.java @@ -25,8 +25,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotEquals; public class BucketPairImplTest { @Test @@ -97,11 +96,11 @@ public void equalsHashCode() { BucketPair bucketPair = BucketPairImpl.bucketPairs(null)[0]; BucketPair sameBucketPair = BucketPairImpl.bucketPairs(null)[0]; - assertTrue(bucketPair.equals(sameBucketPair)); - assertTrue(bucketPair.equals(bucketPair)); + assertEquals(bucketPair, sameBucketPair); + assertEquals(bucketPair, bucketPair); assertEquals(bucketPair.hashCode(), sameBucketPair.hashCode()); - assertFalse(bucketPair.equals(null)); - assertFalse(bucketPair.equals(9)); + assertNotEquals(null, bucketPair); + assertNotEquals(9, bucketPair); } } diff --git a/core/src/test/java/com/uber/m3/tally/CapableOfTest.java b/core/src/test/java/com/uber/m3/tally/CapableOfTest.java index d839055..8914d8b 100644 --- a/core/src/test/java/com/uber/m3/tally/CapableOfTest.java +++ b/core/src/test/java/com/uber/m3/tally/CapableOfTest.java @@ -20,15 +20,16 @@ package com.uber.m3.tally; +import org.junit.Test; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; -import org.junit.Test; - public class CapableOfTest { @Test - public void capabilities() throws Exception { + public void capabilities() { Capabilities capabilities = new CapableOf(false, true); assertFalse(capabilities.reporting()); @@ -45,16 +46,16 @@ public void capabilities() throws Exception { } @Test - public void equalsHashCode() throws Exception { - assertFalse(CapableOf.NONE.equals(null)); - assertFalse(CapableOf.NONE.equals(9)); + public void equalsHashCode() { + assertNotEquals(null, CapableOf.NONE); + assertNotEquals(9, CapableOf.NONE); - assertFalse(CapableOf.NONE.equals(CapableOf.REPORTING)); - assertFalse(new CapableOf(true, false).equals(new CapableOf(false, true))); + assertNotEquals(CapableOf.NONE, CapableOf.REPORTING); + assertNotEquals(new CapableOf(true, false), new CapableOf(false, true)); - assertTrue(CapableOf.REPORTING.equals(CapableOf.REPORTING)); - assertTrue(CapableOf.REPORTING.equals(new CapableOf(true, false))); + assertEquals(CapableOf.REPORTING, CapableOf.REPORTING); + assertEquals(CapableOf.REPORTING, new CapableOf(true, false)); assertEquals(CapableOf.REPORTING.hashCode(), new CapableOf(true, false).hashCode()); - assertTrue(new CapableOf(false, false).equals(new CapableOf(false, false))); + assertEquals(new CapableOf(false, false), new CapableOf(false, false)); } } diff --git a/core/src/test/java/com/uber/m3/tally/DurationBucketsTest.java b/core/src/test/java/com/uber/m3/tally/DurationBucketsTest.java index 1973063..35a4eed 100644 --- a/core/src/test/java/com/uber/m3/tally/DurationBucketsTest.java +++ b/core/src/test/java/com/uber/m3/tally/DurationBucketsTest.java @@ -20,21 +20,21 @@ package com.uber.m3.tally; +import com.uber.m3.util.Duration; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import org.hamcrest.CoreMatchers; -import org.junit.Test; - -import com.uber.m3.util.Duration; - import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class DurationBucketsTest { @Test @@ -44,7 +44,7 @@ public void durationBuckets() { assertFalse(buckets.isEmpty()); assertTrue(buckets.contains(Duration.ofMillis(120))); - for (Iterator iter : new Iterator[]{buckets.iterator(), buckets.listIterator()}) { + for (Iterator iter : new Iterator[]{buckets.iterator(), buckets.listIterator()}) { for (int i = 0; i < buckets.size(); i++) { assertEquals(Duration.ofMillis(100 + 10 * i), iter.next()); } @@ -135,7 +135,7 @@ public void asValues() { Duration.ofNanos(1000) }); - Double[] expectedBuckets = new Double[] { + Double[] expectedBuckets = new Double[]{ 10d / Duration.NANOS_PER_SECOND, 30d / Duration.NANOS_PER_SECOND, 55d / Duration.NANOS_PER_SECOND, @@ -209,26 +209,26 @@ public void exponential() { @Test public void custom() { DurationBuckets expectedBuckets = new DurationBuckets( - new Duration[] { - Duration.ofMillis(1), - Duration.ofMillis(2), - Duration.ofMillis(3), - Duration.ofMillis(5), - Duration.ofMillis(7), - Duration.ofMillis(10), - } + new Duration[]{ + Duration.ofMillis(1), + Duration.ofMillis(2), + Duration.ofMillis(3), + Duration.ofMillis(5), + Duration.ofMillis(7), + Duration.ofMillis(10), + } ); assertThat("custom buckets are created as per our expectations", - DurationBuckets.custom( - Duration.ofMillis(1), - Duration.ofMillis(2), - Duration.ofMillis(3), - Duration.ofMillis(5), - Duration.ofMillis(7), - Duration.ofMillis(10) - ), - CoreMatchers.equalTo(expectedBuckets)); + DurationBuckets.custom( + Duration.ofMillis(1), + Duration.ofMillis(2), + Duration.ofMillis(3), + Duration.ofMillis(5), + Duration.ofMillis(7), + Duration.ofMillis(10) + ), + CoreMatchers.equalTo(expectedBuckets)); } @Test(expected = IllegalArgumentException.class) @@ -239,9 +239,9 @@ public void customFailWithEmptyBuckets() { @Test(expected = IllegalArgumentException.class) public void customFailWithUnsortedBuckets() { DurationBuckets.custom( - Duration.ofMillis(1), - Duration.ofMillis(3), - Duration.ofMillis(2) + Duration.ofMillis(1), + Duration.ofMillis(3), + Duration.ofMillis(2) ); } @@ -262,10 +262,10 @@ public void equalsHashcode() { DurationBuckets buckets = DurationBuckets.linear(Duration.ZERO, Duration.ofSeconds(10), 3); DurationBuckets sameBuckets = DurationBuckets.linear(Duration.ZERO, Duration.ofSeconds(10), 3); - assertTrue(buckets.equals(sameBuckets)); + assertEquals(buckets, sameBuckets); assertEquals(buckets.hashCode(), sameBuckets.hashCode()); - assertFalse(buckets.equals(null)); - assertFalse(buckets.equals(9)); + assertNotEquals(null, buckets); + assertNotEquals(9, buckets); } } diff --git a/core/src/test/java/com/uber/m3/tally/NoopScopeTest.java b/core/src/test/java/com/uber/m3/tally/NoopScopeTest.java index ba7735d..316f63c 100644 --- a/core/src/test/java/com/uber/m3/tally/NoopScopeTest.java +++ b/core/src/test/java/com/uber/m3/tally/NoopScopeTest.java @@ -20,13 +20,13 @@ package com.uber.m3.tally; -import java.util.HashMap; -import java.util.Map; - import org.hamcrest.CoreMatchers; import org.junit.Assert; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + public class NoopScopeTest { @Test @@ -35,14 +35,14 @@ public void testCounter() { Counter noopCounter1 = noopScope.counter("test1"); Counter noopCounter2 = noopScope.counter("test2"); Assert.assertThat( - "same noop counter returned", - noopCounter1 == noopCounter2, - CoreMatchers.equalTo(true) + "same noop counter returned", + noopCounter1 == noopCounter2, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop counter", - noopCounter1 == NoopScope.NOOP_COUNTER, - CoreMatchers.equalTo(true) + "noop counter", + noopCounter1 == NoopScope.NOOP_COUNTER, + CoreMatchers.equalTo(true) ); } @@ -52,14 +52,14 @@ public void testGauge() { Gauge noopGauge1 = noopScope.gauge("test1"); Gauge noopGauge2 = noopScope.gauge("test2"); Assert.assertThat( - "same noop gauge returned", - noopGauge1 == noopGauge2, - CoreMatchers.equalTo(true) + "same noop gauge returned", + noopGauge1 == noopGauge2, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop gauge", - noopGauge1 == NoopScope.NOOP_GAUGE, - CoreMatchers.equalTo(true) + "noop gauge", + noopGauge1 == NoopScope.NOOP_GAUGE, + CoreMatchers.equalTo(true) ); } @@ -69,19 +69,19 @@ public void testTimer() { Timer noopTimer1 = noopScope.timer("test1"); Timer noopTimer2 = noopScope.timer("test2"); Assert.assertThat( - "same noop timer returned", - noopTimer1 == noopTimer2, - CoreMatchers.equalTo(true) + "same noop timer returned", + noopTimer1 == noopTimer2, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop timer", - noopTimer1 == NoopScope.NOOP_TIMER, - CoreMatchers.equalTo(true) + "noop timer", + noopTimer1 == NoopScope.NOOP_TIMER, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop stopwatch", - noopTimer1.start() == NoopScope.NOOP_STOPWATCH, - CoreMatchers.equalTo(true) + "noop stopwatch", + noopTimer1.start() == NoopScope.NOOP_STOPWATCH, + CoreMatchers.equalTo(true) ); } @@ -92,14 +92,14 @@ public void testHistogram() { Histogram noopHistogram2 = noopScope.histogram("test2", ValueBuckets.custom(4, 5, 6)); Assert.assertThat( - "same noop histogram returned", - noopHistogram1 == noopHistogram2, - CoreMatchers.equalTo(true) + "same noop histogram returned", + noopHistogram1 == noopHistogram2, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop histogram", - noopHistogram1 == NoopScope.NOOP_HISTOGRAM, - CoreMatchers.equalTo(true) + "noop histogram", + noopHistogram1 == NoopScope.NOOP_HISTOGRAM, + CoreMatchers.equalTo(true) ); } @@ -109,14 +109,14 @@ public void testCapabilities() { Capabilities noopCapabilities1 = noopScope.capabilities(); Capabilities noopCapabilities2 = noopScope.capabilities(); Assert.assertThat( - "same noop capabilities returned", - noopCapabilities1 == noopCapabilities2, - CoreMatchers.equalTo(true) + "same noop capabilities returned", + noopCapabilities1 == noopCapabilities2, + CoreMatchers.equalTo(true) ); Assert.assertThat( - "noop capabilities", - noopCapabilities1 == NoopScope.NOOP_CAPABILITIES, - CoreMatchers.equalTo(true) + "noop capabilities", + noopCapabilities1 == NoopScope.NOOP_CAPABILITIES, + CoreMatchers.equalTo(true) ); } @@ -126,12 +126,12 @@ public void testSubscope() { Scope noopScope1 = noopScope.subScope("test1"); Scope noopScope2 = noopScope.subScope("test2"); Assert.assertThat("same noop scope returned", - noopScope1 == noopScope2, - CoreMatchers.equalTo(true) + noopScope1 == noopScope2, + CoreMatchers.equalTo(true) ); Assert.assertThat("noop scope returned", - noopScope1 == noopScope, - CoreMatchers.equalTo(true) + noopScope1 == noopScope, + CoreMatchers.equalTo(true) ); } @@ -149,12 +149,12 @@ public void testTagged() { Scope noopScope1 = noopScope.tagged(tagMap1); Scope noopScope2 = noopScope.tagged(tagMap2); Assert.assertThat("same noop scope returned", - noopScope1 == noopScope2, - CoreMatchers.equalTo(true) + noopScope1 == noopScope2, + CoreMatchers.equalTo(true) ); Assert.assertThat("noop scope returned", - noopScope1 == noopScope, - CoreMatchers.equalTo(true) + noopScope1 == noopScope, + CoreMatchers.equalTo(true) ); } } diff --git a/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java b/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java index 89de860..c9897fd 100644 --- a/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java +++ b/core/src/test/java/com/uber/m3/tally/ScopeImplTest.java @@ -20,19 +20,18 @@ package com.uber.m3.tally; +import com.uber.m3.util.Duration; +import com.uber.m3.util.ImmutableMap; +import org.junit.Test; + import java.lang.Thread.UncaughtExceptionHandler; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; - -import com.uber.m3.util.Duration; -import com.uber.m3.util.ImmutableMap; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; public class ScopeImplTest { private static final double EPSILON = 1e-10; @@ -54,21 +53,21 @@ public void metricCreation() { Counter sameCounter = scope.counter("new-counter"); // Should be the same Counter object and not a new instance - assertTrue(counter == sameCounter); + assertEquals(counter, sameCounter); Gauge gauge = scope.gauge("new-gauge"); assertNotNull(gauge); Gauge sameGauge = scope.gauge("new-gauge"); // Should be the same Gauge object and not a new instance - assertTrue(gauge == sameGauge); + assertEquals(gauge, sameGauge); Timer timer = scope.timer("new-timer"); assertNotNull(timer); Timer sameTimer = scope.timer("new-timer"); // Should be the same Timer object and not a new instance - assertTrue(timer == sameTimer); + assertEquals(timer, sameTimer); Histogram histogram = scope.histogram( "new-histogram", @@ -78,7 +77,7 @@ public void metricCreation() { Histogram sameHistogram = scope.histogram("new-histogram", null); // Should be the same Histogram object and not a new instance - assertTrue(histogram == sameHistogram); + assertEquals(histogram, sameHistogram); } @Test @@ -151,9 +150,9 @@ public void subscopes() { ImmutableMap additionalTags = new ImmutableMap.Builder(2) - .put("new_key", "new_val") - .put("baz", "quz") - .build(); + .put("new_key", "new_val") + .put("baz", "quz") + .build(); Scope taggedSubscope = rootScope.tagged(additionalTags); Timer taggedTimer = taggedSubscope.timer("tagged_timer"); taggedTimer.record(Duration.ofSeconds(6)); @@ -180,9 +179,9 @@ public void subscopes() { assertEquals("tagged_timer", timer.getName()); ImmutableMap expectedTags = new ImmutableMap.Builder(4) - .putAll(tags) - .putAll(additionalTags) - .build(); + .putAll(tags) + .putAll(additionalTags) + .build(); assertEquals(expectedTags, timer.getTags()); } @@ -222,24 +221,24 @@ public void snapshot() { Map counters = snapshot.counters(); assertEquals(1, counters.size()); assertEquals("snapshot-counter", counters.get("snapshot-counter+").name()); - assertEquals(null, counters.get("snapshot-counter+").tags()); + assertNull(counters.get("snapshot-counter+").tags()); Map gauges = snapshot.gauges(); assertEquals(3, gauges.size()); assertEquals("snapshot-gauge", gauges.get("snapshot-gauge+").name()); - assertEquals(null, gauges.get("snapshot-gauge+").tags()); + assertNull(gauges.get("snapshot-gauge+").tags()); assertEquals(120, gauges.get("snapshot-gauge+").value(), EPSILON); assertEquals("snapshot-gauge2", gauges.get("snapshot-gauge2+").name()); - assertEquals(null, gauges.get("snapshot-gauge2+").tags()); + assertNull(gauges.get("snapshot-gauge2+").tags()); assertEquals(220, gauges.get("snapshot-gauge2+").value(), EPSILON); assertEquals("snapshot-gauge3", gauges.get("snapshot-gauge3+").name()); - assertEquals(null, gauges.get("snapshot-gauge3+").tags()); + assertNull(gauges.get("snapshot-gauge3+").tags()); assertEquals(320, gauges.get("snapshot-gauge3+").value(), EPSILON); Map timers = snapshot.timers(); assertEquals(1, timers.size()); assertEquals("snapshot-timer", timers.get("snapshot-timer+").name()); - assertEquals(null, timers.get("snapshot-timer+").tags()); + assertNull(timers.get("snapshot-timer+").tags()); } @Test(expected = IllegalArgumentException.class) @@ -251,19 +250,12 @@ public void nonPositiveReportInterval() { public void exceptionInReportLoop() throws ScopeCloseException, InterruptedException { final AtomicInteger uncaghtExceptionReported = new AtomicInteger(); ThrowingStatsReporter reporter = new ThrowingStatsReporter(); - final UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread t, Throwable e) { - uncaghtExceptionReported.incrementAndGet(); - } - }; + final UncaughtExceptionHandler uncaughtExceptionHandler = (t, e) -> uncaghtExceptionReported.incrementAndGet(); - Scope scope = new RootScopeBuilder() + try (Scope scope = new RootScopeBuilder() .reporter(reporter) .reportEvery(Duration.ofMillis(REPORT_INTERVAL_MILLIS), - uncaughtExceptionHandler); - - try { + uncaughtExceptionHandler)) { scope.counter("hi").inc(1); Thread.sleep(SLEEP_MILLIS); @@ -276,15 +268,13 @@ public void uncaughtException(Thread t, Throwable e) { assertEquals(2, uncaghtExceptionReported.get()); assertEquals(2, reporter.getNumberOfReportedMetrics()); - } finally { - scope.close(); } } private static class ThrowingStatsReporter implements StatsReporter { private final AtomicInteger reported = new AtomicInteger(); - public int getNumberOfReportedMetrics() { + int getNumberOfReportedMetrics() { return reported.get(); } diff --git a/core/src/test/java/com/uber/m3/tally/TestStatsReporter.java b/core/src/test/java/com/uber/m3/tally/TestStatsReporter.java index 7252987..9a0db46 100644 --- a/core/src/test/java/com/uber/m3/tally/TestStatsReporter.java +++ b/core/src/test/java/com/uber/m3/tally/TestStatsReporter.java @@ -28,12 +28,12 @@ import java.util.concurrent.LinkedBlockingDeque; public class TestStatsReporter implements StatsReporter { - private Queue> counters = new LinkedBlockingDeque<>(); - private Queue> gauges = new LinkedBlockingDeque<>(); - private Queue> timers = new LinkedBlockingDeque<>(); + private final Queue> counters = new LinkedBlockingDeque<>(); + private final Queue> gauges = new LinkedBlockingDeque<>(); + private final Queue> timers = new LinkedBlockingDeque<>(); private Buckets buckets; - private Map valueSamples = new HashMap<>(); - private Map durationSamples = new HashMap<>(); + private final Map valueSamples = new HashMap<>(); + private final Map durationSamples = new HashMap<>(); @Override public void reportCounter(String name, Map tags, long value) { @@ -127,9 +127,9 @@ public Buckets getBuckets() { } static class MetricStruct { - private String name; - private Map tags; - private T value; + private final String name; + private final Map tags; + private final T value; MetricStruct(String name, Map tags, T value) { this.name = name; diff --git a/core/src/test/java/com/uber/m3/tally/ValueBucketsTest.java b/core/src/test/java/com/uber/m3/tally/ValueBucketsTest.java index 27c9f7b..fe126ef 100644 --- a/core/src/test/java/com/uber/m3/tally/ValueBucketsTest.java +++ b/core/src/test/java/com/uber/m3/tally/ValueBucketsTest.java @@ -20,15 +20,13 @@ package com.uber.m3.tally; +import com.uber.m3.util.Duration; import org.hamcrest.CoreMatchers; import org.junit.Test; -import com.uber.m3.util.Duration; - import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; public class ValueBucketsTest { @@ -42,7 +40,7 @@ public void asValues() { 1000d }); - Double[] expectedBuckets = new Double[] { + Double[] expectedBuckets = new Double[]{ 10d, 30d, 55d, @@ -115,7 +113,7 @@ public void exponential() { @Test public void custom() { - ValueBuckets expectedBuckets = new ValueBuckets(new Double[] { + ValueBuckets expectedBuckets = new ValueBuckets(new Double[]{ 1d, 2d, 3d, @@ -123,8 +121,8 @@ public void custom() { 7d }); assertThat("Buckets are created as per our expectations", - ValueBuckets.custom(1D, 2D, 3D, 5D, 7D), - CoreMatchers.equalTo(expectedBuckets)); + ValueBuckets.custom(1D, 2D, 3D, 5D, 7D), + CoreMatchers.equalTo(expectedBuckets)); } @Test(expected = IllegalArgumentException.class) @@ -154,10 +152,10 @@ public void equalsHashcode() { ValueBuckets buckets = ValueBuckets.linear(0, 10, 3); ValueBuckets sameBuckets = ValueBuckets.linear(0, 10, 3); - assertTrue(buckets.equals(sameBuckets)); + assertEquals(buckets, sameBuckets); assertEquals(buckets.hashCode(), sameBuckets.hashCode()); - assertFalse(buckets.equals(null)); - assertFalse(buckets.equals(9)); + assertNotEquals(null, buckets); + assertNotEquals(9, buckets); } } diff --git a/core/src/test/java/com/uber/m3/util/DurationTest.java b/core/src/test/java/com/uber/m3/util/DurationTest.java index 5cacee0..5052462 100644 --- a/core/src/test/java/com/uber/m3/util/DurationTest.java +++ b/core/src/test/java/com/uber/m3/util/DurationTest.java @@ -23,8 +23,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotEquals; public class DurationTest { private static final double EPSILON = 1e-10; @@ -102,15 +101,15 @@ public void compareTo() { @Test public void equals() { - assertTrue(Duration.ZERO.equals(Duration.ZERO)); - assertTrue(Duration.ZERO.equals(Duration.ofNanos(0))); - assertTrue(Duration.ofMillis(6000).equals(Duration.ofSeconds(6))); - assertTrue(Duration.ofHours(1).equals(Duration.ofNanos(3_600_000_000_000L))); + assertEquals(Duration.ZERO, Duration.ZERO); + assertEquals(Duration.ZERO, Duration.ofNanos(0)); + assertEquals(Duration.ofMillis(6000), Duration.ofSeconds(6)); + assertEquals(Duration.ofHours(1), Duration.ofNanos(3_600_000_000_000L)); assertEquals(Duration.ofHours(1).hashCode(), Duration.ofNanos(3_600_000_000_000L).hashCode()); - assertFalse(Duration.ofMillis(6001).equals(Duration.ofSeconds(6))); - assertFalse(Duration.ZERO.equals(null)); - assertFalse(Duration.ZERO.equals(1)); + assertNotEquals(Duration.ofMillis(6001), Duration.ofSeconds(6)); + assertNotEquals(null, Duration.ZERO); + assertNotEquals(1, Duration.ZERO); } @Test diff --git a/core/src/test/java/com/uber/m3/util/ImmutableListTest.java b/core/src/test/java/com/uber/m3/util/ImmutableListTest.java index f5d82be..440ea27 100644 --- a/core/src/test/java/com/uber/m3/util/ImmutableListTest.java +++ b/core/src/test/java/com/uber/m3/util/ImmutableListTest.java @@ -48,7 +48,7 @@ public void setUp() { public void size() { assertEquals(2, list.size()); - list = new ImmutableList<>(new ArrayList(0)); + list = new ImmutableList<>(new ArrayList<>(0)); assertEquals(0, list.size()); } @@ -57,7 +57,7 @@ public void size() { public void isEmpty() { assertFalse(list.isEmpty()); - list = new ImmutableList<>(new ArrayList(0)); + list = new ImmutableList<>(new ArrayList<>(0)); assertTrue(list.isEmpty()); } @@ -202,13 +202,13 @@ public void equals() { helperList.add("zz"); ImmutableList differentList = new ImmutableList<>(helperList); - assertTrue(list.equals(sameList)); + assertEquals(list, sameList); assertEquals(list.hashCode(), sameList.hashCode()); - assertFalse(list.equals(differentList)); + assertNotEquals(list, differentList); assertNotEquals(list.hashCode(), differentList.hashCode()); - assertFalse(list.equals(null)); - assertTrue(list.equals(list)); - assertFalse(list.equals(1)); + assertNotEquals(null, list); + assertEquals(list, list); + assertNotEquals(1, list); } } diff --git a/core/src/test/java/com/uber/m3/util/ImmutableMapTest.java b/core/src/test/java/com/uber/m3/util/ImmutableMapTest.java index 2e0a6c1..67ed093 100644 --- a/core/src/test/java/com/uber/m3/util/ImmutableMapTest.java +++ b/core/src/test/java/com/uber/m3/util/ImmutableMapTest.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class ImmutableMapTest { @@ -66,18 +67,18 @@ public void of() { public void size() { assertEquals(3, map.size()); - map = new ImmutableMap<>(new HashMap(0, 1)); + map = new ImmutableMap<>(new HashMap<>(0, 1)); assertEquals(0, map.size()); } @Test public void isEmpty() { - assertEquals(false, map.isEmpty()); + assertFalse(map.isEmpty()); - map = new ImmutableMap<>(new HashMap(0, 1)); + map = new ImmutableMap<>(new HashMap<>(0, 1)); - assertEquals(true, map.isEmpty()); + assertTrue(map.isEmpty()); } @Test @@ -102,7 +103,7 @@ public void containsValue() { public void get() { assertEquals("val1", map.get("key1")); - assertEquals(null, map.get("key9")); + assertNull(map.get("key9")); } @Test(expected = UnsupportedOperationException.class) @@ -145,27 +146,27 @@ public void builderPutAll() { ImmutableMap sameMap = new ImmutableMap.Builder() .putAll(helperMap).build(); - assertTrue(map.equals(sameMap)); + assertEquals(map, sameMap); } @Test public void equals() { - assertFalse(map.equals(null)); - assertFalse(map.equals(1)); - assertTrue(map.equals(map)); + assertNotEquals(null, map); + assertNotEquals(1, map); + assertEquals(map, map); ImmutableMap sameMap = new ImmutableMap<>(helperMap); - assertTrue(map.equals(sameMap)); + assertEquals(map, sameMap); assertEquals(map.hashCode(), sameMap.hashCode()); helperMap.put("key7", "val7"); ImmutableMap differentMap = new ImmutableMap<>(helperMap); - assertFalse(map.equals(differentMap)); + assertNotEquals(map, differentMap); assertNotEquals(map.hashCode(), differentMap.hashCode()); - assertTrue(ImmutableMap.EMPTY.equals(new ImmutableMap(new HashMap()))); + assertEquals(ImmutableMap.EMPTY, new ImmutableMap<>(new HashMap<>())); } @Test diff --git a/core/src/test/java/com/uber/m3/util/ImmutableSetTest.java b/core/src/test/java/com/uber/m3/util/ImmutableSetTest.java index 3689019..890b757 100644 --- a/core/src/test/java/com/uber/m3/util/ImmutableSetTest.java +++ b/core/src/test/java/com/uber/m3/util/ImmutableSetTest.java @@ -50,7 +50,7 @@ public void setUp() { public void size() { assertEquals(4, set.size()); - set = new ImmutableSet<>(new HashSet(0)); + set = new ImmutableSet<>(new HashSet<>(0)); assertEquals(0, set.size()); } @@ -59,7 +59,7 @@ public void size() { public void isEmpty() { assertFalse(set.isEmpty()); - set = new ImmutableSet<>(new HashSet(0)); + set = new ImmutableSet<>(new HashSet<>(0)); assertTrue(set.isEmpty()); } @@ -119,7 +119,7 @@ public void containsAll() { @Test(expected = UnsupportedOperationException.class) public void addAll() { - set.addAll(new HashSet()); + set.addAll(new HashSet<>()); } @Test(expected = UnsupportedOperationException.class) @@ -141,18 +141,18 @@ public void clear() { public void equals() { ImmutableSet equalSet = new ImmutableSet<>(helperSet); - assertTrue(set.equals(equalSet)); + assertEquals(set, equalSet); assertEquals(set.hashCode(), equalSet.hashCode()); helperSet.add("zz"); ImmutableSet differentSet = new ImmutableSet<>(helperSet); - assertFalse(set.equals(differentSet)); + assertNotEquals(set, differentSet); assertNotEquals(set.hashCode(), differentSet.hashCode()); - assertFalse(set.equals(null)); - assertTrue(set.equals(set)); - assertFalse(set.equals(2)); + assertNotEquals(null, set); + assertEquals(set, set); + assertNotEquals(2, set); } @Test diff --git a/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporter.java b/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporter.java index 4d57b09..a9123d0 100644 --- a/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporter.java +++ b/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporter.java @@ -23,8 +23,8 @@ import com.uber.m3.tally.Buckets; import com.uber.m3.tally.Capabilities; import com.uber.m3.tally.CapableOf; -import com.uber.m3.util.Duration; import com.uber.m3.tally.StatsReporter; +import com.uber.m3.util.Duration; import java.util.Map; diff --git a/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporterExample.java b/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporterExample.java index eb2c41a..9b1352c 100644 --- a/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporterExample.java +++ b/example/src/main/java/com/uber/m3/tally/example/PrintStatsReporterExample.java @@ -21,13 +21,13 @@ package com.uber.m3.tally.example; import com.uber.m3.tally.Counter; -import com.uber.m3.tally.ScopeCloseException; -import com.uber.m3.util.Duration; import com.uber.m3.tally.Gauge; import com.uber.m3.tally.RootScopeBuilder; import com.uber.m3.tally.Scope; +import com.uber.m3.tally.ScopeCloseException; import com.uber.m3.tally.StatsReporter; import com.uber.m3.tally.Timer; +import com.uber.m3.util.Duration; /** * PrintStatsReporterExample usage with a PrintStatsReporter reporting synthetically emitted metrics diff --git a/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java b/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java index c307a00..6922e15 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/M3Reporter.java @@ -102,17 +102,17 @@ public class M3Reporter implements StatsReporter, AutoCloseable { private static final int NUM_PROCESSORS = 1; private static final ThreadLocal PAYLOAD_SIZE_ESTIMATOR = - ThreadLocal.withInitial(SerializedPayloadSizeEstimator::new); + ThreadLocal.withInitial(SerializedPayloadSizeEstimator::new); - private M3.Client client; + private final M3.Client client; - private Duration maxBufferingDelay; + private final Duration maxBufferingDelay; private final int payloadCapacity; - private String bucketIdTagName; - private String bucketTagName; - private String bucketValFmt; + private final String bucketIdTagName; + private final String bucketTagName; + private final String bucketValFmt; private final Set commonTags; @@ -125,11 +125,11 @@ public class M3Reporter implements StatsReporter, AutoCloseable { // This is a synchronization barrier to make sure that reporter // is being shutdown only after all of its processor had done so - private CountDownLatch shutdownLatch = new CountDownLatch(NUM_PROCESSORS); + private final CountDownLatch shutdownLatch = new CountDownLatch(NUM_PROCESSORS); - private TTransport transport; + private final TTransport transport; - private AtomicBoolean isShutdown = new AtomicBoolean(false); + private final AtomicBoolean isShutdown = new AtomicBoolean(false); // Use inner Builder class to construct an M3Reporter private M3Reporter(Builder builder) { @@ -238,8 +238,8 @@ public void close() { // to complete if (!shutdownLatch.await(MAX_PROCESSOR_WAIT_ON_CLOSE_MILLIS, TimeUnit.MILLISECONDS)) { LOG.warn( - "M3Reporter closing before Processors complete after waiting timeout of {}ms!", - MAX_PROCESSOR_WAIT_ON_CLOSE_MILLIS + "M3Reporter closing before Processors complete after waiting timeout of {}ms!", + MAX_PROCESSOR_WAIT_ON_CLOSE_MILLIS ); } } catch (InterruptedException e) { @@ -459,14 +459,14 @@ private void queueSizedMetric(SizedMetric sizedMetric) { try { metricQueue.put(sizedMetric); } catch (InterruptedException e) { - LOG.warn(String.format("Interrupted queueing metric: {}", sizedMetric.getMetric().getName())); + LOG.warn("Interrupted queueing metric: {}", sizedMetric.getMetric().getName()); } } private class Processor implements Runnable { private final List metricsBuffer = - new ArrayList<>(payloadCapacity / 10); + new ArrayList<>(payloadCapacity / 10); private Instant lastBufferFlushTimestamp = Instant.now(clock); @@ -528,7 +528,7 @@ private void process(SizedMetric sizedMetric) { private boolean elapsedMaxDelaySinceLastFlush() { return Instant.now(clock).isAfter( - lastBufferFlushTimestamp.plus(maxBufferingDelay.toMillis(), ChronoUnit.MILLIS) + lastBufferFlushTimestamp.plus(maxBufferingDelay.toMillis(), ChronoUnit.MILLIS) ); } @@ -551,9 +551,9 @@ private void flushBuffered() { try { client.emitMetricBatch( - new MetricBatch() - .setCommonTags(commonTags) - .setMetrics(metricsBuffer) + new MetricBatch() + .setCommonTags(commonTags) + .setMetrics(metricsBuffer) ); } catch (TException tException) { LOG.warn("Failed to flush metrics: " + tException.getMessage()); @@ -573,7 +573,7 @@ private void flushBuffered() { private static class SerializedPayloadSizeEstimator { private final TCalcTransport calculatingPhonyTransport = new TCalcTransport(); private final TProtocol calculatingPhonyProtocol = - new TCompactProtocol.Factory().getProtocol(calculatingPhonyTransport); + new TCompactProtocol.Factory().getProtocol(calculatingPhonyTransport); private final M3.Client phonyClient = new M3.Client(calculatingPhonyProtocol); @@ -608,7 +608,7 @@ public static class Builder { protected ExecutorService executor; // Non-generic EMPTY ImmutableMap will never contain any elements @SuppressWarnings("unchecked") - protected ImmutableMap commonTags = ImmutableMap.EMPTY; + protected ImmutableMap commonTags = (ImmutableMap) ImmutableMap.EMPTY; protected boolean includeHost = false; protected int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE; protected int maxPacketSizeBytes = DEFAULT_MAX_PACKET_SIZE; diff --git a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TCalcTransport.java b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TCalcTransport.java index 1bbf532..9a62597 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TCalcTransport.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TCalcTransport.java @@ -29,7 +29,7 @@ * Dummy transport used for calculating size of metrics only. */ public class TCalcTransport extends TTransport { - private AtomicInteger size = new AtomicInteger(0); + private final AtomicInteger size = new AtomicInteger(0); /** * Dummy override to satisfy interface. Not used for our purposes. diff --git a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TMultiUdpClient.java b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TMultiUdpClient.java index daa30e2..4da72c3 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TMultiUdpClient.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TMultiUdpClient.java @@ -30,7 +30,7 @@ * A Thrift transport that sends to multiple connections */ public class TMultiUdpClient extends TTransport implements AutoCloseable { - private TTransport[] transports; + private final TTransport[] transports; public TMultiUdpClient(SocketAddress[] socketAddresses) throws SocketException { if (socketAddresses == null || socketAddresses.length == 0) { diff --git a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpServer.java b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpServer.java index 0e6ca72..98e1563 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpServer.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpServer.java @@ -29,7 +29,7 @@ * A server for receiving data via Thrift UDP. */ public class TUdpServer extends TUdpTransport implements AutoCloseable { - private int timeoutMillis; + private final int timeoutMillis; /** * Constructs a UDP server with the given host and port. Defaults to zero timeout. diff --git a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpTransport.java b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpTransport.java index 894288c..076a274 100644 --- a/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpTransport.java +++ b/m3/src/main/java/com/uber/m3/tally/m3/thrift/TUdpTransport.java @@ -52,7 +52,7 @@ public abstract class TUdpTransport extends TTransport implements AutoCloseable private final Object receiveLock = new Object(); @GuardedBy("receiveLock") - private ByteBuffer receiveBuffer; + private final ByteBuffer receiveBuffer; // NOTE: This is used in tests TUdpTransport(SocketAddress socketAddress, DatagramSocket socket) { diff --git a/m3/src/test/java/com/uber/m3/tally/m3/M3ReporterTest.java b/m3/src/test/java/com/uber/m3/tally/m3/M3ReporterTest.java index b82c45b..1d7437a 100644 --- a/m3/src/test/java/com/uber/m3/tally/m3/M3ReporterTest.java +++ b/m3/src/test/java/com/uber/m3/tally/m3/M3ReporterTest.java @@ -50,9 +50,7 @@ import static org.junit.Assert.assertTrue; public class M3ReporterTest { - private static double EPSILON = 1e-9; - - private static SocketAddress socketAddress; + private static final double EPSILON = 1e-9; private static final ImmutableMap DEFAULT_TAGS = ImmutableMap.of( @@ -60,6 +58,8 @@ public class M3ReporterTest { "host", "test-host" ); + private static SocketAddress socketAddress; + @BeforeClass public static void setup() { try { @@ -74,12 +74,7 @@ public void reporter() throws InterruptedException { final MockM3Server server = new MockM3Server(3, socketAddress); M3Reporter reporter = null; - Thread serverThread = new Thread(new Runnable() { - @Override - public void run() { - server.serve(); - } - }); + Thread serverThread = new Thread(server::serve); try { serverThread.start(); @@ -231,12 +226,7 @@ public void builderMissingCommonTag() { public void reporterFinalFlush() throws InterruptedException { final MockM3Server server = new MockM3Server(1, socketAddress); - Thread serverThread = new Thread(new Runnable() { - @Override - public void run() { - server.serve(); - } - }); + Thread serverThread = new Thread(server::serve); serverThread.start(); @@ -260,12 +250,7 @@ public void run() { public void reporterAfterCloseNoThrow() throws InterruptedException { final MockM3Server server = new MockM3Server(0, socketAddress); - Thread serverThread = new Thread(new Runnable() { - @Override - public void run() { - server.serve(); - } - }); + Thread serverThread = new Thread(server::serve); try { serverThread.start(); @@ -288,12 +273,7 @@ public void run() { public void reporterHistogramDurations() throws InterruptedException { final MockM3Server server = new MockM3Server(2, socketAddress); - Thread serverThread = new Thread(new Runnable() { - @Override - public void run() { - server.serve(); - } - }); + Thread serverThread = new Thread(server::serve); serverThread.start(); @@ -386,12 +366,7 @@ public void run() { public void reporterHistogramValues() throws InterruptedException { final MockM3Server server = new MockM3Server(2, socketAddress); - Thread serverThread = new Thread(new Runnable() { - @Override - public void run() { - server.serve(); - } - }); + Thread serverThread = new Thread(server::serve); try { serverThread.start(); @@ -508,17 +483,17 @@ public void testSinglePacketPayloadOverflow() throws InterruptedException { // NOTE: We're using default max packet size M3Reporter reporter = new M3Reporter.Builder(socketAddress) - .service("test-service") - .commonTags( - ImmutableMap.of("env", "test") - ) - // Effectively disable time-based flushing to only keep - // size-based one - .maxProcessorWaitUntilFlushMillis(1_000_000) - .build(); + .service("test-service") + .commonTags( + ImmutableMap.of("env", "test") + ) + // Effectively disable time-based flushing to only keep + // size-based one + .maxProcessorWaitUntilFlushMillis(1_000_000) + .build(); ImmutableMap emptyTags = - new ImmutableMap.Builder(0).build(); + new ImmutableMap.Builder(0).build(); for (int i = 0; i < expectedMetricsCount; ++i) { // NOTE: The goal is to minimize the metric size, to make sure diff --git a/m3/src/test/java/com/uber/m3/tally/m3/MockM3Server.java b/m3/src/test/java/com/uber/m3/tally/m3/MockM3Server.java index f140081..863e502 100644 --- a/m3/src/test/java/com/uber/m3/tally/m3/MockM3Server.java +++ b/m3/src/test/java/com/uber/m3/tally/m3/MockM3Server.java @@ -40,9 +40,9 @@ public class MockM3Server { private final CountDownLatch expectedMetricsLatch; - private TProcessor processor; - private TTransport transport; - private MockM3Service service; + private final TProcessor processor; + private final TTransport transport; + private final MockM3Service service; public MockM3Server( int expectedMetricsCount, diff --git a/m3/src/test/java/com/uber/m3/tally/m3/SizedMetricTest.java b/m3/src/test/java/com/uber/m3/tally/m3/SizedMetricTest.java index 2a7287a..31a76a2 100644 --- a/m3/src/test/java/com/uber/m3/tally/m3/SizedMetricTest.java +++ b/m3/src/test/java/com/uber/m3/tally/m3/SizedMetricTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; public class SizedMetricTest { @Test @@ -40,7 +41,7 @@ public void simpleSizedMetric() { public void nullSizedMetric() { SizedMetric sizedMetric = new SizedMetric(null, 9); - assertEquals(sizedMetric.getMetric(), null); + assertNull(sizedMetric.getMetric()); assertEquals(sizedMetric.getSize(), 9); } diff --git a/m3/src/test/java/com/uber/m3/tally/m3/TCalcTransportTest.java b/m3/src/test/java/com/uber/m3/tally/m3/TCalcTransportTest.java index f656d0c..498726f 100644 --- a/m3/src/test/java/com/uber/m3/tally/m3/TCalcTransportTest.java +++ b/m3/src/test/java/com/uber/m3/tally/m3/TCalcTransportTest.java @@ -27,9 +27,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class TCalcTransportTest { - TCalcTransport transport; + private TCalcTransport transport; @Before public void setUp() { @@ -50,7 +51,7 @@ public void open() { transport.open(); } catch (TTransportException e) { // Should not reach here - assertTrue(false); + fail(); } } @@ -66,7 +67,7 @@ public void read() { assertEquals(0, transport.read(null, 0, 0)); } catch (TTransportException e) { // Should not reach here - assertTrue(false); + fail(); } } @@ -99,7 +100,7 @@ public void writeGetSizeReset() { assertEquals(0, transport.getSize()); } catch (TTransportException e) { // Should not reach here - assertTrue(false); + fail(); } } } diff --git a/m3/src/test/java/com/uber/m3/tally/m3/thrift/TUdpClientTest.java b/m3/src/test/java/com/uber/m3/tally/m3/thrift/TUdpClientTest.java index 8ada25b..8ddbe4b 100644 --- a/m3/src/test/java/com/uber/m3/tally/m3/thrift/TUdpClientTest.java +++ b/m3/src/test/java/com/uber/m3/tally/m3/thrift/TUdpClientTest.java @@ -32,7 +32,10 @@ import java.net.InetSocketAddress; import java.util.stream.Stream; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; public class TUdpClientTest { @@ -43,9 +46,9 @@ public void testWritingAfterFlushingSequence() throws TTransportException, IOExc TUdpClient client = createUdpClient(mockedSocket); byte[][] payloads = - Stream.of("0xDEEDDEED", "0xABBAABBA") - .map(p -> p.getBytes(Charsets.US_ASCII)) - .toArray(byte[][]::new); + Stream.of("0xDEEDDEED", "0xABBAABBA") + .map(p -> p.getBytes(Charsets.US_ASCII)) + .toArray(byte[][]::new); for (byte[] writtenBytes : payloads) { client.write(writtenBytes); @@ -64,7 +67,7 @@ private static void assertBytesWritten(DatagramSocket mockedSocket, byte[] expec ArgumentCaptor argCaptor = ArgumentCaptor.forClass(DatagramPacket.class); verify(mockedSocket, times(1)) - .send(argCaptor.capture()); + .send(argCaptor.capture()); DatagramPacket sentPacket = argCaptor.getValue(); @@ -75,8 +78,8 @@ private static void assertBytesWritten(DatagramSocket mockedSocket, byte[] expec Assert.assertEquals(expectedPayload.length, sentPacket.getLength()); Assert.assertEquals(0, sentPacket.getOffset()); Assert.assertEquals( - new String(expectedPayload, Charsets.US_ASCII), - new String(sentPacket.getData(), sentPacket.getOffset(), sentPacket.getLength(), Charsets.US_ASCII) + new String(expectedPayload, Charsets.US_ASCII), + new String(sentPacket.getData(), sentPacket.getOffset(), sentPacket.getLength(), Charsets.US_ASCII) ); } diff --git a/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdAssertingUdpServer.java b/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdAssertingUdpServer.java index 11e8df1..9ea0d36 100644 --- a/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdAssertingUdpServer.java +++ b/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdAssertingUdpServer.java @@ -29,10 +29,11 @@ import java.util.Set; public class StatsdAssertingUdpServer implements Runnable { - private final int TIMEOUT_MILLIS = 1000; - private final int RECEIVE_MAX_SIZE = 1024; + private static final int TIMEOUT_MILLIS = 1000; + private static final int RECEIVE_MAX_SIZE = 1024; + private final SocketAddress socketAddress; - private Set expectedStrs; + private final Set expectedStrs; StatsdAssertingUdpServer(String hostname, int port, Set expectedStrs) { this.expectedStrs = expectedStrs; diff --git a/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdReporter.java b/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdReporter.java index 32d7d50..809dabd 100644 --- a/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdReporter.java +++ b/statsd/src/main/java/com/uber/m3/tally/statsd/StatsdReporter.java @@ -36,9 +36,9 @@ public class StatsdReporter implements StatsReporter { private static final int DEFAULT_SAMPLE_RATE = 1; private static final int DEFAULT_HISTOGRAM_BUCKET_NAME_PRECISION = 6; - private StatsDClient statsdClient; - private double sampleRate; - private String bucketFmt; + private final StatsDClient statsdClient; + private final double sampleRate; + private final String bucketFmt; /** * Create a StatsD reporter diff --git a/statsd/src/test/java/com/uber/m3/tally/statsd/StatsdReporterTest.java b/statsd/src/test/java/com/uber/m3/tally/statsd/StatsdReporterTest.java index eddf17a..64d724b 100644 --- a/statsd/src/test/java/com/uber/m3/tally/statsd/StatsdReporterTest.java +++ b/statsd/src/test/java/com/uber/m3/tally/statsd/StatsdReporterTest.java @@ -34,7 +34,7 @@ import static org.junit.Assert.assertEquals; public class StatsdReporterTest { - private final int PORT = 4434; + private static final int PORT = 4434; private StatsDClient statsd; private StatsdReporter reporter;