Skip to content

Commit

Permalink
Use java Objects.requireNotNull (#1129)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Apr 22, 2020
1 parent 8a0ec1d commit 6e7f34d
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package io.opentelemetry.exporters.prometheus;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricProducer;
import io.prometheus.client.Collector;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public final class PrometheusCollector extends Collector {
private final MetricProducer metricProducer;
Expand Down Expand Up @@ -71,7 +71,7 @@ public Builder setMetricProducer(MetricProducer metricProducer) {
* @return a new {@code Collector} based on the builder's values.
*/
public PrometheusCollector build() {
return new PrometheusCollector(Utils.checkNotNull(metricProducer, "metricProducer"));
return new PrometheusCollector(Objects.requireNonNull(metricProducer, "metricProducer"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.correlationcontext.CorrelationContextManager;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracerProvider;
import java.util.Objects;

public final class TraceShim {
private TraceShim() {}
Expand Down Expand Up @@ -53,8 +53,8 @@ public static io.opentracing.Tracer createTracerShim(
TracerProvider tracerProvider, CorrelationContextManager contextManager) {
return new TracerShim(
new TelemetryInfo(
getTracer(Utils.checkNotNull(tracerProvider, "tracerProvider")),
Utils.checkNotNull(contextManager, "contextManager"),
getTracer(Objects.requireNonNull(tracerProvider, "tracerProvider")),
Objects.requireNonNull(contextManager, "contextManager"),
OpenTelemetry.getPropagators()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@

package io.opentelemetry.sdk.correlationcontext;

import static io.opentelemetry.internal.Utils.checkNotNull;

import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.correlationcontext.CorrelationContext;
import io.opentelemetry.correlationcontext.Entry;
import io.opentelemetry.correlationcontext.EntryKey;
import io.opentelemetry.correlationcontext.EntryMetadata;
import io.opentelemetry.correlationcontext.EntryValue;
import io.opentelemetry.internal.Utils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

Expand All @@ -50,7 +48,8 @@ class CorrelationContextSdk implements CorrelationContext {
*/
private CorrelationContextSdk(
Map<? extends EntryKey, ? extends Entry> entries, CorrelationContext parent) {
this.entries = Collections.unmodifiableMap(new HashMap<>(checkNotNull(entries, "entries")));
this.entries =
Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(entries, "entries")));
this.parent = parent;
}

Expand Down Expand Up @@ -123,7 +122,7 @@ static class Builder implements CorrelationContext.Builder {

@Override
public CorrelationContext.Builder setParent(CorrelationContext parent) {
this.parent = Utils.checkNotNull(parent, "parent");
this.parent = Objects.requireNonNull(parent, "parent");
return this;
}

Expand All @@ -138,15 +137,17 @@ public CorrelationContext.Builder setNoParent() {
public CorrelationContext.Builder put(
EntryKey key, EntryValue value, EntryMetadata entryMetadata) {
entries.put(
checkNotNull(key, "key"),
Objects.requireNonNull(key, "key"),
Entry.create(
key, checkNotNull(value, "value"), checkNotNull(entryMetadata, "entryMetadata")));
key,
Objects.requireNonNull(value, "value"),
Objects.requireNonNull(entryMetadata, "entryMetadata")));
return this;
}

@Override
public CorrelationContext.Builder remove(EntryKey key) {
entries.remove(checkNotNull(key, "key"));
entries.remove(Objects.requireNonNull(key, "key"));
if (parent != null && parent.getEntryValue(key) != null) {
entries.put(key, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

abstract class AbstractInstrument implements Instrument {

Expand Down Expand Up @@ -104,7 +105,7 @@ abstract static class Builder<B extends AbstractInstrument.Builder<?>>
String name,
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState) {
Utils.checkNotNull(name, "name");
Objects.requireNonNull(name, "name");
Utils.checkArgument(
StringUtils.isValidMetricName(name) && name.length() <= NAME_MAX_LENGTH,
ERROR_MESSAGE_INVALID_NAME);
Expand All @@ -115,20 +116,20 @@ abstract static class Builder<B extends AbstractInstrument.Builder<?>>

@Override
public final B setDescription(String description) {
this.description = Utils.checkNotNull(description, "description");
this.description = Objects.requireNonNull(description, "description");
return getThis();
}

@Override
public final B setUnit(String unit) {
this.unit = Utils.checkNotNull(unit, "unit");
this.unit = Objects.requireNonNull(unit, "unit");
return getThis();
}

@Override
public final B setConstantLabels(Map<String, String> constantLabels) {
Utils.checkMapKeysNotNull(
Utils.checkNotNull(constantLabels, "constantLabels"), "constantLabel");
Objects.requireNonNull(constantLabels, "constantLabels"), "constantLabel");
this.constantLabels = Collections.unmodifiableMap(new HashMap<>(constantLabels));
return getThis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.metrics.DoubleObserver;
import io.opentelemetry.sdk.metrics.aggregator.Aggregator;
import io.opentelemetry.sdk.metrics.common.InstrumentValueType;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -61,7 +61,7 @@ List<MetricData> collectAll() {

@Override
public void setCallback(Callback<DoubleObserver.ResultDoubleObserver> metricUpdater) {
this.metricUpdater = Utils.checkNotNull(metricUpdater, "metricUpdater");
this.metricUpdater = Objects.requireNonNull(metricUpdater, "metricUpdater");
}

static final class Builder extends AbstractObserver.Builder<DoubleObserverSdk.Builder>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.metrics.LongObserver;
import io.opentelemetry.sdk.metrics.aggregator.Aggregator;
import io.opentelemetry.sdk.metrics.common.InstrumentValueType;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -61,7 +61,7 @@ List<MetricData> collectAll() {

@Override
public void setCallback(Callback<ResultLongObserver> metricUpdater) {
this.metricUpdater = Utils.checkNotNull(metricUpdater, "metricUpdater");
this.metricUpdater = Objects.requireNonNull(metricUpdater, "metricUpdater");
}

static final class Builder extends AbstractObserver.Builder<LongObserverSdk.Builder>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.metrics.MeterProvider;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
Expand All @@ -30,6 +29,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ private Builder() {}
* @return this
*/
public Builder setClock(@Nonnull Clock clock) {
Utils.checkNotNull(clock, "clock");
Objects.requireNonNull(clock, "clock");
this.clock = clock;
return this;
}
Expand All @@ -110,7 +110,7 @@ public Builder setClock(@Nonnull Clock clock) {
* @return this
*/
public Builder setResource(@Nonnull Resource resource) {
Utils.checkNotNull(resource, "resource");
Objects.requireNonNull(resource, "resource");
this.resource = resource;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -76,7 +77,7 @@ public static Resource getEmpty() {
* @since 0.1.0
*/
public static Resource create(Map<String, AttributeValue> attributes) {
checkAttributes(Utils.checkNotNull(attributes, "attributes"));
checkAttributes(Objects.requireNonNull(attributes, "attributes"));
return new AutoValue_Resource(Collections.unmodifiableMap(new LinkedHashMap<>(attributes)));
}

Expand Down Expand Up @@ -105,7 +106,7 @@ private static void checkAttributes(Map<String, AttributeValue> attributes) {
for (Entry<String, AttributeValue> entry : attributes.entrySet()) {
Utils.checkArgument(
isValidAndNotEmpty(entry.getKey()), "Attribute key" + ERROR_MESSAGE_INVALID_CHARS);
Utils.checkNotNull(entry.getValue(), "Attribute value" + ERROR_MESSAGE_INVALID_VALUE);
Objects.requireNonNull(entry.getValue(), "Attribute value" + ERROR_MESSAGE_INVALID_VALUE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package io.opentelemetry.sdk.trace;

import io.opentelemetry.internal.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Implementation of the {@code SpanProcessor} that simply forwards all received events to a list of
Expand All @@ -38,7 +38,7 @@ public final class MultiSpanProcessor implements SpanProcessor {
*/
public static SpanProcessor create(List<SpanProcessor> spanProcessorList) {
return new MultiSpanProcessor(
new ArrayList<>(Utils.checkNotNull(spanProcessorList, "spanProcessorList")));
new ArrayList<>(Objects.requireNonNull(spanProcessorList, "spanProcessorList")));
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;

/** {@link SpanBuilderSdk} is SDK implementation of {@link Span.Builder}. */
Expand Down Expand Up @@ -87,15 +88,15 @@ final class SpanBuilderSdk implements Span.Builder {

@Override
public Span.Builder setParent(Span parent) {
this.parent = Utils.checkNotNull(parent, "parent");
this.parent = Objects.requireNonNull(parent, "parent");
this.remoteParent = null;
this.parentType = ParentType.EXPLICIT_PARENT;
return this;
}

@Override
public Span.Builder setParent(SpanContext remoteParent) {
this.remoteParent = Utils.checkNotNull(remoteParent, "remoteParent");
this.remoteParent = Objects.requireNonNull(remoteParent, "remoteParent");
this.parent = null;
this.parentType = ParentType.EXPLICIT_REMOTE_PARENT;
return this;
Expand All @@ -111,7 +112,7 @@ public Span.Builder setNoParent() {

@Override
public Span.Builder setSpanKind(Kind spanKind) {
this.spanKind = Utils.checkNotNull(spanKind, "spanKind");
this.spanKind = Objects.requireNonNull(spanKind, "spanKind");
return this;
}

Expand All @@ -129,7 +130,7 @@ public Span.Builder addLink(SpanContext spanContext, Map<String, AttributeValue>

@Override
public Span.Builder addLink(Link link) {
Utils.checkNotNull(link, "link");
Objects.requireNonNull(link, "link");
totalNumberOfLinksAdded++;
// don't bother doing anything with any links beyond the max.
if (links.size() == traceConfig.getMaxNumberOfLinks()) {
Expand Down Expand Up @@ -167,7 +168,7 @@ public Span.Builder setAttribute(String key, boolean value) {

@Override
public Span.Builder setAttribute(String key, AttributeValue value) {
Utils.checkNotNull(key, "key");
Objects.requireNonNull(key, "key");
if (value == null
|| (value.getType().equals(AttributeValue.Type.STRING) && value.getStringValue() == null)) {
attributes.remove(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.opentelemetry.sdk.trace;

import io.opentelemetry.internal.Utils;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.internal.ComponentRegistry;
Expand All @@ -26,6 +25,7 @@
import io.opentelemetry.sdk.trace.config.TraceConfig;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracerProvider;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -147,7 +147,7 @@ public static class Builder {
* @return this
*/
public Builder setClock(Clock clock) {
Utils.checkNotNull(clock, "clock");
Objects.requireNonNull(clock, "clock");
this.clock = clock;
return this;
}
Expand All @@ -160,7 +160,7 @@ public Builder setClock(Clock clock) {
* @return this
*/
public Builder setIdsGenerator(IdsGenerator idsGenerator) {
Utils.checkNotNull(idsGenerator, "idsGenerator");
Objects.requireNonNull(idsGenerator, "idsGenerator");
this.idsGenerator = idsGenerator;
return this;
}
Expand All @@ -172,7 +172,7 @@ public Builder setIdsGenerator(IdsGenerator idsGenerator) {
* @return this
*/
public Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
Objects.requireNonNull(resource, "resource");
this.resource = resource;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -177,7 +178,7 @@ public static final class Builder {
private boolean sampled = true;

private Builder(SpanExporter spanExporter) {
this.spanExporter = Utils.checkNotNull(spanExporter, "spanExporter");
this.spanExporter = Objects.requireNonNull(spanExporter, "spanExporter");
}

// TODO: Consider to add support for constant Attributes and/or Resource.
Expand Down
Loading

0 comments on commit 6e7f34d

Please sign in to comment.