Remove duplicate code in new metrics code

Refactor BufferMetricReader, CounterBuffers and GaugeBuffers to reduce
duplicate code.

Closes gh-3117
pull/3245/merge
izeye 10 years ago committed by Phillip Webb
parent 0d6c197c39
commit b47634176f

@ -49,28 +49,6 @@ public class BufferMetricReader implements MetricReader, PrefixMetricReader {
this.gauges = gauges;
}
@Override
public Iterable<Metric<?>> findAll(String prefix) {
final List<Metric<?>> metrics = new ArrayList<Metric<?>>();
this.counters.forEach(Pattern.compile(prefix + ".*").asPredicate(),
new BiConsumer<String, LongBuffer>() {
@Override
public void accept(String name, LongBuffer value) {
metrics.add(new Metric<Long>(name, value.getValue(), new Date(
value.getTimestamp())));
}
});
this.gauges.forEach(Pattern.compile(prefix + ".*").asPredicate(),
new BiConsumer<String, DoubleBuffer>() {
@Override
public void accept(String name, DoubleBuffer value) {
metrics.add(new Metric<Double>(name, value.getValue(), new Date(
value.getTimestamp())));
}
});
return metrics;
}
@Override
public Metric<?> findOne(final String name) {
LongBuffer buffer = this.counters.find(name);
@ -88,27 +66,40 @@ public class BufferMetricReader implements MetricReader, PrefixMetricReader {
@Override
public Iterable<Metric<?>> findAll() {
return findAll(this.all);
}
@Override
public Iterable<Metric<?>> findAll(String prefix) {
return findAll(Pattern.compile(prefix + ".*").asPredicate());
}
@Override
public long count() {
return this.counters.count() + this.gauges.count();
}
private Iterable<Metric<?>> findAll(Predicate<String> predicate) {
final List<Metric<?>> metrics = new ArrayList<Metric<?>>();
this.counters.forEach(this.all, new BiConsumer<String, LongBuffer>() {
this.counters.forEach(predicate, new BiConsumer<String, LongBuffer>() {
@Override
public void accept(String name, LongBuffer value) {
metrics.add(new Metric<Long>(name, value.getValue(), new Date(value
.getTimestamp())));
}
});
this.gauges.forEach(this.all, new BiConsumer<String, DoubleBuffer>() {
this.gauges.forEach(predicate, new BiConsumer<String, DoubleBuffer>() {
@Override
public void accept(String name, DoubleBuffer value) {
metrics.add(new Metric<Double>(name, value.getValue(), new Date(value
.getTimestamp())));
}
});
return metrics;
}
@Override
public long count() {
return this.counters.count() + this.gauges.count();
}
}

@ -52,12 +52,7 @@ public class CounterBuffers {
}
public void get(final String name, final Consumer<LongBuffer> consumer) {
read(name, new Consumer<LongBuffer>() {
@Override
public void accept(LongBuffer adder) {
consumer.accept(adder);
}
});
read(name, consumer);
}
public void increment(final String name, final long delta) {
@ -83,12 +78,7 @@ public class CounterBuffers {
}
private void read(final String name, final Consumer<LongBuffer> consumer) {
acceptInternal(name, new Consumer<LongBuffer>() {
@Override
public void accept(LongBuffer adder) {
consumer.accept(adder);
}
});
acceptInternal(name, consumer);
}
private void write(final String name, final Consumer<LongBuffer> consumer) {

@ -52,12 +52,7 @@ public class GaugeBuffers {
}
public void get(final String name, final Consumer<DoubleBuffer> consumer) {
acceptInternal(name, new Consumer<DoubleBuffer>() {
@Override
public void accept(DoubleBuffer value) {
consumer.accept(value);
}
});
acceptInternal(name, consumer);
}
public void set(final String name, final double value) {

Loading…
Cancel
Save