Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8 code cleanup #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/uber/m3/tally/BucketPairImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/uber/m3/tally/CapableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/uber/m3/tally/CounterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/uber/m3/tally/CounterSnapshotImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
* Default implementation of a {@link CounterSnapshot}.
*/
class CounterSnapshotImpl implements CounterSnapshot {
private String name;
private ImmutableMap<String, String> tags;
private long value;
private final String name;
private final ImmutableMap<String, String> tags;
private final long value;

CounterSnapshotImpl(String name, ImmutableMap<String, String> tags, long value) {
this.name = name;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/com/uber/m3/tally/DurationBuckets.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Double[] asValues() {

@Override
public Duration[] asDurations() {
return buckets.toArray(new Duration[buckets.size()]);
return buckets.toArray(new Duration[0]);
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/uber/m3/tally/GaugeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/uber/m3/tally/GaugeSnapshotImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
* Default implementation of a {@link GaugeSnapshot}.
*/
class GaugeSnapshotImpl implements GaugeSnapshot {
private String name;
private ImmutableMap<String, String> tags;
private double value;
private final String name;
private final ImmutableMap<String, String> tags;
private final double value;

GaugeSnapshotImpl(String name, ImmutableMap<String, String> tags, double value) {
this.name = name;
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/com/uber/m3/tally/HistogramImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
* Default implementation of a {@link Histogram}.
*/
class HistogramImpl implements Histogram, StopwatchRecorder {
private Type type;
private String name;
private ImmutableMap<String, String> tags;
private Buckets specification;
private List<HistogramBucket> buckets;
private List<Double> lookupByValue;
private List<Duration> lookupByDuration;
private final Type type;
private final String name;
private final ImmutableMap<String, String> tags;
private final Buckets specification;
private final List<HistogramBucket> buckets;
private final List<Double> lookupByValue;
private final List<Duration> lookupByDuration;

HistogramImpl(
String name,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
* Default implementation of a {@link HistogramSnapshot}.
*/
class HistogramSnapshotImpl implements HistogramSnapshot {
private String name;
private ImmutableMap<String, String> tags;
private Map<Double, Long> values;
private Map<Duration, Long> durations;
private final String name;
private final ImmutableMap<String, String> tags;
private final Map<Double, Long> values;
private final Map<Duration, Long> durations;

HistogramSnapshotImpl(
String name,
Expand Down
62 changes: 17 additions & 45 deletions core/src/main/java/com/uber/m3/tally/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
* Default {@link Scope} implementation.
*/
class ScopeImpl implements Scope {
private StatsReporter reporter;
private String prefix;
private String separator;
private ImmutableMap<String, String> tags;
private Buckets defaultBuckets;
private final StatsReporter reporter;
private final String prefix;
private final String separator;
private final ImmutableMap<String, String> 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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -208,11 +180,11 @@ static String keyForPrefixedStringMap(String prefix, ImmutableMap<String, String
}

if (stringMap == null) {
stringMap = ImmutableMap.EMPTY;
stringMap = (ImmutableMap<String, String>) ImmutableMap.EMPTY;
}

Set<String> 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);
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/uber/m3/tally/SnapshotImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
* Default implementation of a {@link Snapshot}.
*/
class SnapshotImpl implements Snapshot {
Map<String, CounterSnapshot> counters = new ConcurrentHashMap<>();
Map<String, GaugeSnapshot> gauges = new ConcurrentHashMap<>();
Map<String, TimerSnapshot> timers = new ConcurrentHashMap<>();
Map<String, HistogramSnapshot> histograms = new ConcurrentHashMap<>();
private final Map<String, CounterSnapshot> counters = new ConcurrentHashMap<>();
private final Map<String, GaugeSnapshot> gauges = new ConcurrentHashMap<>();
private final Map<String, TimerSnapshot> timers = new ConcurrentHashMap<>();
private final Map<String, HistogramSnapshot> histograms = new ConcurrentHashMap<>();

@Override
public Map<String, CounterSnapshot> counters() {
Expand Down
42 changes: 21 additions & 21 deletions core/src/main/java/com/uber/m3/tally/StatsReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public interface StatsReporter extends BaseStatsReporter {
* @param value value to report
*/
void reportCounter(
String name,
Map<String, String> tags,
long value
String name,
Map<String, String> tags,
long value
);

/**
Expand All @@ -47,9 +47,9 @@ void reportCounter(
* @param value value to report
*/
void reportGauge(
String name,
Map<String, String> tags,
double value
String name,
Map<String, String> tags,
double value
);

/**
Expand All @@ -59,9 +59,9 @@ void reportGauge(
* @param interval interval to report
*/
void reportTimer(
String name,
Map<String, String> tags,
Duration interval
String name,
Map<String, String> tags,
Duration interval
);

/**
Expand All @@ -74,12 +74,12 @@ void reportTimer(
* @param samples samples to report
*/
void reportHistogramValueSamples(
String name,
Map<String, String> tags,
Buckets buckets,
double bucketLowerBound,
double bucketUpperBound,
long samples
String name,
Map<String, String> tags,
Buckets buckets,
double bucketLowerBound,
double bucketUpperBound,
long samples
);

/**
Expand All @@ -92,11 +92,11 @@ void reportHistogramValueSamples(
* @param samples samples to report
*/
void reportHistogramDurationSamples(
String name,
Map<String, String> tags,
Buckets buckets,
Duration bucketLowerBound,
Duration bucketUpperBound,
long samples
String name,
Map<String, String> tags,
Buckets buckets,
Duration bucketLowerBound,
Duration bucketUpperBound,
long samples
);
}
4 changes: 2 additions & 2 deletions core/src/main/java/com/uber/m3/tally/Stopwatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading