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

avoid unnecessary ArrayList creations; use Collections.singletonList … #906

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.prometheus.metrics.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -26,10 +25,10 @@ private ExporterFilterProperties(List<String> allowedNames, List<String> exclude
}

private ExporterFilterProperties(List<String> allowedNames, List<String> excludedNames, List<String> allowedPrefixes, List<String> excludedPrefixes, String prefix) {
this.allowedNames = allowedNames == null ? null : Collections.unmodifiableList(new ArrayList<>(allowedNames));
this.excludedNames = excludedNames == null ? null : Collections.unmodifiableList(new ArrayList<>(excludedNames));
this.allowedPrefixes = allowedPrefixes == null ? null : Collections.unmodifiableList(new ArrayList<>(allowedPrefixes));
this.excludedPrefixes = excludedPrefixes == null ? null : Collections.unmodifiableList(new ArrayList<>(excludedPrefixes));
this.allowedNames = allowedNames == null ? null : Collections.unmodifiableList(allowedNames);
this.excludedNames = excludedNames == null ? null : Collections.unmodifiableList(excludedNames);
this.allowedPrefixes = allowedPrefixes == null ? null : Collections.unmodifiableList(allowedPrefixes);
this.excludedPrefixes = excludedPrefixes == null ? null : Collections.unmodifiableList(excludedPrefixes);
validate(prefix);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.prometheus.metrics.model.registry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Predicate;

import static java.util.Collections.unmodifiableCollection;
Expand All @@ -23,10 +23,10 @@ public class MetricNameFilter implements Predicate<String> {
private final Collection<String> nameDoesNotStartWith;

private MetricNameFilter(Collection<String> nameIsEqualTo, Collection<String> nameIsNotEqualTo, Collection<String> nameStartsWith, Collection<String> nameDoesNotStartWith) {
this.nameIsEqualTo = unmodifiableCollection(new ArrayList<>(nameIsEqualTo));
this.nameIsNotEqualTo = unmodifiableCollection(new ArrayList<>(nameIsNotEqualTo));
this.nameStartsWith = unmodifiableCollection(new ArrayList<>(nameStartsWith));
this.nameDoesNotStartWith = unmodifiableCollection(new ArrayList<>(nameDoesNotStartWith));
this.nameIsEqualTo = unmodifiableCollection(nameIsEqualTo);
this.nameIsNotEqualTo = unmodifiableCollection(nameIsNotEqualTo);
this.nameStartsWith = unmodifiableCollection(nameStartsWith);
this.nameDoesNotStartWith = unmodifiableCollection(nameDoesNotStartWith);
}

@Override
Expand Down Expand Up @@ -107,7 +107,8 @@ private Builder() {
* @see #nameMustBeEqualTo(Collection)
*/
public Builder nameMustBeEqualTo(String... names) {
return nameMustBeEqualTo(Arrays.asList(names));
Collections.addAll(nameEqualTo, names);
return this;
}

/**
Expand All @@ -132,7 +133,8 @@ public Builder nameMustBeEqualTo(Collection<String> names) {
* @see #nameMustNotBeEqualTo(Collection)
*/
public Builder nameMustNotBeEqualTo(String... names) {
return nameMustNotBeEqualTo(Arrays.asList(names));
Collections.addAll(nameNotEqualTo, names);
return this;
}

/**
Expand All @@ -155,7 +157,8 @@ public Builder nameMustNotBeEqualTo(Collection<String> names) {
* @see #nameMustStartWith(Collection)
*/
public Builder nameMustStartWith(String... prefixes) {
return nameMustStartWith(Arrays.asList(prefixes));
Collections.addAll(nameStartsWith, prefixes);
return this;
}

/**
Expand All @@ -174,7 +177,8 @@ public Builder nameMustStartWith(Collection<String> prefixes) {
* @see #nameMustNotStartWith(Collection)
*/
public Builder nameMustNotStartWith(String... prefixes) {
return nameMustNotStartWith(Arrays.asList(prefixes));
Collections.addAll(nameDoesNotStartWith, prefixes);
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void clear(){
@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
List<String> labelNames = Arrays.asList("cache");
List<String> labelNames = Collections.singletonList("cache");

CounterMetricFamily cacheHitTotal = new CounterMetricFamily("caffeine_cache_hit_total",
"Cache hit totals", labelNames);
Expand Down Expand Up @@ -138,7 +138,7 @@ public List<MetricFamilySamples> collect() {
mfs.add(cacheLoadSummary);

for(Map.Entry<String, Cache> c: children.entrySet()) {
List<String> cacheName = Arrays.asList(c.getKey());
List<String> cacheName = Collections.singletonList(c.getKey());
CacheStats stats = c.getValue().stats();

try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static String getHelpMessage(String metricName, Metric metric) {
MetricFamilySamples fromCounter(String dropwizardName, Counter counter) {
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "", new ArrayList<String>(), new ArrayList<String>(),
new Long(counter.getCount()).doubleValue());
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, counter), Arrays.asList(sample));
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, counter), Collections.singletonList(sample));
}

/**
Expand All @@ -106,7 +106,7 @@ MetricFamilySamples fromGauge(String dropwizardName, Gauge gauge) {
}
MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "",
new ArrayList<String>(), new ArrayList<String>(), value);
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample));
return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Collections.singletonList(sample));
}

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ MetricFamilySamples fromMeter(String dropwizardName, Meter meter) {
new ArrayList<String>(),
meter.getCount());
return new MetricFamilySamples(sample.name, Type.COUNTER, getHelpMessage(dropwizardName, meter),
Arrays.asList(sample));
Collections.singletonList(sample));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void clear(){
@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
List<String> labelNames = Arrays.asList("cache");
List<String> labelNames = Collections.singletonList("cache");

CounterMetricFamily cacheHitTotal = new CounterMetricFamily("guava_cache_hit_total",
"Cache hit totals", labelNames);
Expand Down Expand Up @@ -121,7 +121,7 @@ public List<MetricFamilySamples> collect() {
mfs.add(cacheLoadSummary);

for(Map.Entry<String, Cache> c: children.entrySet()) {
List<String> cacheName = Arrays.asList(c.getKey());
List<String> cacheName = Collections.singletonList(c.getKey());
CacheStats stats = c.getValue().stats();

cacheHitTotal.addMetric(cacheName, stats.hitCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class HibernateStatisticsCollector extends Collector {

private static final List<String> LABEL_NAMES = Collections.singletonList("unit");

private static final List<String> LABEL_NAMES_PER_QUERY = Arrays.asList("unit", "query");
private static final List<String> LABEL_NAMES_PER_QUERY = Collections.unmodifiableList(Arrays.asList("unit", "query"));

private final Map<String, SessionFactory> sessionFactories = new ConcurrentHashMap<String, SessionFactory>();

Expand Down Expand Up @@ -476,7 +476,7 @@ public double getValue(Statistics statistics) {
private List<MetricFamilySamples> getPerQueryMetrics() {
List<MetricFamilySamples> metrics = new ArrayList<MetricFamilySamples>();

metrics.addAll(Arrays.asList(
Collections.addAll(metrics,

createCounterForQuery("hibernate_per_query_cache_hit_total",
"Global number of cache hits for query (getCacheHitCount)",
Expand Down Expand Up @@ -558,7 +558,7 @@ public double getValue(Statistics statistics, String query) {
}
}
)
));
);

return metrics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class JettyStatisticsCollector extends Collector {
private final StatisticsHandler statisticsHandler;
private static final List<String> EMPTY_LIST = new ArrayList<String>();
private static final List<String> EMPTY_LIST = Collections.emptyList();

public JettyStatisticsCollector(StatisticsHandler statisticsHandler) {
this.statisticsHandler = statisticsHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ private Set<String> parse(HttpServletRequestAdapter req) {
if (includedParam == null) {
return Collections.emptySet();
} else {
return new HashSet<String>(Arrays.asList(includedParam));
Set<String> result = new HashSet<String>();
Collections.addAll(result, includedParam);
return result;
}
}

Expand Down